Skip to content

Commit

Permalink
Don't start event source until rails is ready.
Browse files Browse the repository at this point in the history
  • Loading branch information
TreyE committed Sep 10, 2024
1 parent 7fa9b28 commit 866d913
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby_version: ['2.6.3', '2.7.5', '3.0.5', '3.1.4', '3.2.2']
ruby_version: ['2.7.5', '3.0.5', '3.1.4', '3.2.2']
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
--format documentation
--color
--require spec_helper
--exclude-pattern "spec/rails_app/**/*"
3 changes: 3 additions & 0 deletions .rspec_rails_specs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--exclude-pattern "**/*"
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ gemspec
group :development, :test do
gem "rails", '>= 6.1.4'
gem "rspec-rails"
gem "parallel_tests"
gem "pry", platform: :mri, require: false
gem "pry-byebug", platform: :mri, require: false
gem 'rubocop'
Expand Down
7 changes: 6 additions & 1 deletion lib/event_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ def configure
yield(config)
end

def initialize!
def initialize!(force = false)
# For a reason we don't entirely understand, unless you immediately
# execute all of the methods together, event_source will fail to
# connect your subscribers.
return if @booted && !force
@booted = true
load_protocols
create_connections
load_async_api_resources
Expand Down
2 changes: 1 addition & 1 deletion lib/event_source/protocols/amqp_protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
require_relative 'amqp/contracts/contract'

Gem
.find_files('event_source/protocols/amqp/contracts/**/*.rb')
.find_files('event_source/protocols/amqp/contracts/**/*.rb', false)
.sort
.each { |f| require(f) }

Expand Down
8 changes: 5 additions & 3 deletions lib/event_source/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

module EventSource
# :nodoc:
class Railtie < Rails::Railtie

module Railtie
Rails::Application::Finisher.initializer "event_source.boot", after: :finisher_hook do
EventSource.initialize!
end
end
end
end
5 changes: 5 additions & 0 deletions spec/event_source/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ def call
# binding.pry
end

before :each do
EventSource.initialize!(true)
end

context '.event' do

let(:organization_params) do
{
hbx_id: '553234',
Expand Down
24 changes: 24 additions & 0 deletions spec/event_source/railtie_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "spec_helper"
require "parallel_tests"
require "parallel_tests/rspec/runner"

RSpec.describe EventSource, "railtie specs" do
it "runs the rails tests in the rails application context" do
ParallelTests.with_pid_file do
specs_run_result = ParallelTests::RSpec::Runner.run_tests(
[
"spec/rails_app/spec/railtie_spec.rb"
],
1,
1,
{
serialize_stdout: true,
test_options: ["-O", ".rspec_rails_specs", "--format", "documentation"]
}
)
if specs_run_result[:exit_status] != 0
fail(specs_run_result[:stdout] + "\n\n")
end
end
end
end
2 changes: 0 additions & 2 deletions spec/rails_app/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

Bundler.require(*Rails.groups)

require "event_source"

module RailsApp
class Application < Rails::Application
config.root = File.expand_path('../..', __FILE__)
Expand Down
4 changes: 4 additions & 0 deletions spec/rails_app/spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ENV['RAILS_ENV'] ||= 'test'
require 'bundler/setup'
require 'rails'
require_relative '../config/environment'
11 changes: 11 additions & 0 deletions spec/rails_app/spec/railtie_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require_relative './rails_helper'

RSpec.describe EventSource::Railtie do
it "runs when invoked" do
manager = EventSource::ConnectionManager.instance
connection = manager.connections_for(:amqp).first
expect(connection).not_to be_nil
end
end

0 comments on commit 866d913

Please sign in to comment.