Skip to content

Commit

Permalink
feat: test bunny connection before start supervisor
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosgz committed Nov 5, 2024
1 parent 2f0906a commit 3277e3f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ RSpec/MultipleExpectations:
RSpec/ExampleLength:
Enabled: false

Rspec/MultipleMemoizedHelpers:
RSpec/MultipleMemoizedHelpers:
Enabled: false

RSpec/MessageSpies:
Expand Down
6 changes: 6 additions & 0 deletions lib/lepus/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def boot
end

setup_consumers
check_bunny_connection

run_process_callbacks(:boot) do
sync_std_streams
Expand All @@ -72,6 +73,11 @@ def setup_consumers
end
end

def check_bunny_connection
temp_bunny = Lepus.config.create_connection(suffix: "(boot-check)")
temp_bunny.close
end

def start_processes
configuration.configured_processes.each { |configured_process| start_process(configured_process) }
end
Expand Down
38 changes: 38 additions & 0 deletions spec/lepus/supervisor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Lepus::Supervisor do
after do
Lepus::ProcessRegistry.instance.clear
end

# rubocop:disable RSpec/AnyInstance
describe "#check_bunny_connection" do
subject(:conn_test) { supervisor.send(:check_bunny_connection) }

let(:config) { Lepus::Supervisor::Config.new(consumers: %w[TestConsumer]) }
let(:supervisor) { described_class.new(config) }

context "when the connection is successful" do
before do
allow_any_instance_of(Bunny::Session).to receive(:start).and_return(:ok)
end

it "does not raise an error" do
expect { conn_test }.not_to raise_error
end
end

context "when the connection is not successful" do
before do
allow_any_instance_of(Bunny::Session).to receive(:start).and_raise(Bunny::Exception)
end

it "raises an error" do
expect { conn_test }.to raise_error(Bunny::Exception)
end
end
end
# rubocop:enable RSpec/AnyInstance
end

0 comments on commit 3277e3f

Please sign in to comment.