From 866d913b5d691f683cb908bd92c0344fd3ea3125 Mon Sep 17 00:00:00 2001 From: Trey Evans Date: Wed, 31 Jul 2024 15:22:46 -0400 Subject: [PATCH] Don't start event source until rails is ready. --- .github/workflows/rspec.yml | 2 +- .rspec | 1 + .rspec_rails_specs | 3 +++ Gemfile | 1 + lib/event_source.rb | 7 +++++- lib/event_source/protocols/amqp_protocol.rb | 2 +- lib/event_source/railtie.rb | 8 ++++--- spec/event_source/command_spec.rb | 5 +++++ spec/event_source/railtie_spec.rb | 24 +++++++++++++++++++++ spec/rails_app/config/application.rb | 2 -- spec/rails_app/spec/rails_helper.rb | 4 ++++ spec/rails_app/spec/railtie_spec.rb | 11 ++++++++++ 12 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 .rspec_rails_specs create mode 100644 spec/event_source/railtie_spec.rb create mode 100644 spec/rails_app/spec/rails_helper.rb create mode 100644 spec/rails_app/spec/railtie_spec.rb diff --git a/.github/workflows/rspec.yml b/.github/workflows/rspec.yml index 198903e4..aff19735 100644 --- a/.github/workflows/rspec.yml +++ b/.github/workflows/rspec.yml @@ -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 diff --git a/.rspec b/.rspec index 34c5164d..8433ecd9 100644 --- a/.rspec +++ b/.rspec @@ -1,3 +1,4 @@ --format documentation --color --require spec_helper +--exclude-pattern "spec/rails_app/**/*" diff --git a/.rspec_rails_specs b/.rspec_rails_specs new file mode 100644 index 00000000..791d0cba --- /dev/null +++ b/.rspec_rails_specs @@ -0,0 +1,3 @@ +--format documentation +--color +--exclude-pattern "**/*" diff --git a/Gemfile b/Gemfile index 55e26d2d..df419cb2 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/lib/event_source.rb b/lib/event_source.rb index 0956d8c6..42df869f 100644 --- a/lib/event_source.rb +++ b/lib/event_source.rb @@ -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 diff --git a/lib/event_source/protocols/amqp_protocol.rb b/lib/event_source/protocols/amqp_protocol.rb index c40594fe..5717a421 100644 --- a/lib/event_source/protocols/amqp_protocol.rb +++ b/lib/event_source/protocols/amqp_protocol.rb @@ -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) } diff --git a/lib/event_source/railtie.rb b/lib/event_source/railtie.rb index ea971274..77dcc66b 100644 --- a/lib/event_source/railtie.rb +++ b/lib/event_source/railtie.rb @@ -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 \ No newline at end of file +end diff --git a/spec/event_source/command_spec.rb b/spec/event_source/command_spec.rb index 7ad0f985..b44d0a6e 100644 --- a/spec/event_source/command_spec.rb +++ b/spec/event_source/command_spec.rb @@ -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', diff --git a/spec/event_source/railtie_spec.rb b/spec/event_source/railtie_spec.rb new file mode 100644 index 00000000..36fec6ad --- /dev/null +++ b/spec/event_source/railtie_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/rails_app/config/application.rb b/spec/rails_app/config/application.rb index bfe50a09..80441f98 100644 --- a/spec/rails_app/config/application.rb +++ b/spec/rails_app/config/application.rb @@ -7,8 +7,6 @@ Bundler.require(*Rails.groups) -require "event_source" - module RailsApp class Application < Rails::Application config.root = File.expand_path('../..', __FILE__) diff --git a/spec/rails_app/spec/rails_helper.rb b/spec/rails_app/spec/rails_helper.rb new file mode 100644 index 00000000..eb389f83 --- /dev/null +++ b/spec/rails_app/spec/rails_helper.rb @@ -0,0 +1,4 @@ +ENV['RAILS_ENV'] ||= 'test' +require 'bundler/setup' +require 'rails' +require_relative '../config/environment' diff --git a/spec/rails_app/spec/railtie_spec.rb b/spec/rails_app/spec/railtie_spec.rb new file mode 100644 index 00000000..7c93c25f --- /dev/null +++ b/spec/rails_app/spec/railtie_spec.rb @@ -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