spec/unit/receiver_spec.rb in pwwka-0.19.0 vs spec/unit/receiver_spec.rb in pwwka-0.20.0

- old
+ new

@@ -1,17 +1,46 @@ require 'spec_helper.rb' describe Pwwka::Receiver do - describe "#new" do - let(:channel_connector) { double(Pwwka::ChannelConnector)} + describe "#new" do + let(:handler_klass) { double('HandlerKlass') } + let(:channel_connector) { double(Pwwka::ChannelConnector, topic_exchange: topic_exchange, channel: channel)} + let(:topic_exchange) { double("topic exchange") } + let(:channel) { double('channel', queue: queue) } + let(:queue) { double('queue') } + let(:queue_name) { 'test_queue_name' } + subject { + described_class.subscribe( + handler_klass, + queue_name + ) + } + before do - allow(Pwwka::ChannelConnector).to receive(:new).with(prefetch: nil, connection_name: "c: test_queue_name").and_return(channel_connector) - allow(channel_connector).to receive(:channel) - allow(channel_connector).to receive(:topic_exchange) + allow(Pwwka::ChannelConnector).to receive(:new).and_return(channel_connector) + allow(handler_klass).to receive(:handle!) + allow(channel_connector).to receive(:connection_close) + allow(queue).to receive(:bind) + allow(queue).to receive(:subscribe).and_yield({}, {}, '{}') end - it "should set the connection_name" do - Pwwka::Receiver.new("test_queue_name", "test.routing.key") + it 'sets the correct connection_name' do + subject + expect(Pwwka::ChannelConnector).to have_received(:new).with(prefetch: nil, connection_name: "c: #{queue_name}") + end + + it 'closes the conenction on an error' do + error = 'oh no' + allow(handler_klass).to receive(:handle!).and_raise(error) + begin; subject; rescue; end + expect(channel_connector).to have_received(:connection_close) + end + + it 'logs on interrupt' do + allow(handler_klass).to receive(:handle!).and_raise(Interrupt) + allow(described_class).to receive(:info) + begin; subject; rescue; end + expect(described_class).to have_received(:info).with(/Interrupting queue #{queue_name}/) end end end