Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add broker#disconnect_channel #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/hutch/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,18 @@ def connect(options = {})
end

def disconnect
channel_broker.disconnect unless Thread.current[CHANNEL_BROKER_KEY].nil?
Thread.current[CHANNEL_BROKER_KEY] = nil
disconnect_channel
@connection.close if @connection
@connection = nil
@api_client = nil
@publisher = nil
end

def disconnect_channel
channel_broker.disconnect unless Thread.current[CHANNEL_BROKER_KEY].nil?
Thread.current[CHANNEL_BROKER_KEY] = nil
end

# Connect to RabbitMQ via AMQP
#
# This sets up the main connection and channel we use for talking to
Expand Down
64 changes: 64 additions & 0 deletions spec/hutch/broker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,70 @@
end
end

describe '#disconnect' do
let(:channel_broker) { instance_double('Hutch::ChannelBroker', disconnect: nil) }
let(:connection) { instance_double('Hutch::Adapter', close: nil) }
before { allow(Hutch::ChannelBroker).to receive(:new).and_return(channel_broker) }
before { allow(channel_broker).to receive(:disconnect) }

context 'when no connection' do
it 'skips disconnect channel' do
expect(channel_broker).not_to receive(:disconnect)
broker.disconnect

expect(broker.connection).to be nil
end
end

context 'when channel broker absent' do
before { broker.instance_variable_set(:@connection, connection) }
before { allow(broker.connection).to receive(:close) }

it 'closes connection' do
expect(channel_broker).not_to receive(:disconnect)
expect(broker.connection).to receive(:close)
broker.disconnect
expect(broker.connection).to be nil
end
end

context 'when channel broker present' do
before { broker.instance_variable_set(:@connection, connection) }
before { broker.send(:channel_broker) }

it 'disconnects the channel' do
expect(broker.connection).to receive(:close)
expect(channel_broker).to receive(:disconnect)
broker.disconnect
expect(broker.connection).to be nil
end
end
end

describe '#disconnect_channel' do
let(:channel_broker) { instance_double('Hutch::ChannelBroker', disconnect: nil) }
before { allow(Hutch::ChannelBroker).to receive(:new).and_return(channel_broker) }
before { allow(channel_broker).to receive(:disconnect) }

context 'when channel broker absent' do
it 'does not try to create channel broker' do
expect(Hutch::ChannelBroker).to_not receive(:new)
broker.disconnect_channel
expect(Thread.current[Hutch::Broker::CHANNEL_BROKER_KEY]).to be nil
end
end

context 'when channel broker present' do
before { broker.send(:channel_broker) }

it 'disconnects the channel' do
expect(channel_broker).to receive(:disconnect)
broker.disconnect_channel
expect(Thread.current[Hutch::Broker::CHANNEL_BROKER_KEY]).to be nil
end
end
end

describe '#set_up_amqp_connection' do
it 'opens a connection, channel and declares an exchange' do
expect(broker).to receive(:open_connection!).ordered
Expand Down