spec/units/message_driver/client_spec.rb in message-driver-0.2.2 vs spec/units/message_driver/client_spec.rb in message-driver-0.3.0
- old
+ new
@@ -6,19 +6,23 @@
describe Client do
class TestPublisher
include Client
end
- let(:adapter) { Adapters::InMemoryAdapter.new({}) }
+ let(:logger) { MessageDriver.logger }
+ let(:broker_name) { Broker::DEFAULT_BROKER_NAME }
+ let!(:broker) { Broker.configure(broker_name, adapter: Adapters::InMemoryAdapter, logger: logger) }
+ let(:adapter) { broker.adapter }
let(:adapter_context) { adapter.new_context }
- let(:logger) { double(Logger).as_null_object }
- before do
- MessageDriver.configure(adapter: adapter, logger: logger)
- end
-
shared_examples "a Client" do
+ describe "#broker" do
+ it "returns the broker_name" do
+ expect(subject.broker_name).to eq(broker_name)
+ end
+ end
+
describe "#current_adapter_context" do
before { subject.clear_context }
it "returns an adapter_context" do
expect(subject.current_adapter_context).to be_a Adapters::ContextBase
@@ -35,11 +39,11 @@
end
end
end
context "with a given adapter_context" do
- around do |example|
+ around(:each) do |example|
subject.with_adapter_context(adapter_context, &example)
end
describe "#dynamic_destination" do
let(:dest_name) { "my_new_queue" }
@@ -64,11 +68,11 @@
adapter_context.should have_received(:create_destination).with(dest_name, {}, {})
end
end
describe "#publish" do
- let(:destination) { Broker.destination(:my_queue, "my_queue", exclusive: true) }
+ let(:destination) { broker.destination(:my_queue, "my_queue", exclusive: true) }
let(:body) { "my message" }
let(:headers) { {foo: :bar} }
let(:properties) { {bar: :baz} }
before do
adapter_context.stub(:publish)
@@ -101,11 +105,11 @@
end
end
describe "#pop_message" do
let(:expected) { double(MessageDriver::Message) }
- let(:destination) { Broker.destination(:my_queue, "my_queue", exclusive: true) }
+ let(:destination) { broker.destination(:my_queue, "my_queue", exclusive: true) }
let(:options) { {foo: :bar} }
before do
adapter_context.stub(:pop_message)
end
@@ -178,10 +182,11 @@
adapter_context.should have_received(:rollback_transaction)
end
context "and the the rollback raises an error" do
it "logs the error from the rollback and raises the original error" do
+ allow(logger).to receive(:error)
adapter_context.stub(:rollback_transaction).and_raise("rollback failed!")
expect {
subject.with_message_transaction do
raise "having a tough time"
end
@@ -230,10 +235,11 @@
adapter_context.should_not have_received(:begin_transaction)
adapter_context.should_not have_received(:commit_transaction)
adapter_context.should_not have_received(:rollback_transaction)
end
it "logs a warning" do
+ allow(logger).to receive(:debug)
expect { |blk|
subject.with_message_transaction(&blk)
}.to yield_control
expect(logger).to have_received(:debug).with("this adapter does not support transactions")
end
@@ -242,81 +248,45 @@
describe "#ack_message" do
let(:message) { double("message") }
let(:options) { {foo: :bar} }
before do
- adapter_context.stub(:ack_message)
+ allow(message).to receive(:ack)
end
- context "when the adapter supports client acks" do
- before do
- adapter_context.stub(:supports_client_acks?) { true }
- end
- it "calls #ack_message with the message" do
- subject.ack_message(message)
- adapter_context.should have_received(:ack_message).with(message, {})
- end
- it "passes the supplied options to ack_message" do
- subject.ack_message(message, options)
- adapter_context.should have_received(:ack_message).with(message, options)
- end
+ it "calls #ack on the message" do
+ subject.ack_message(message)
+ expect(message).to have_received(:ack).with({})
end
- context "when the adapter doesn't support client acks" do
- before do
- adapter_context.stub(:supports_client_acks?) { false }
- end
- it "doesn't call #ack_message" do
- subject.ack_message(message)
- adapter_context.should_not have_received(:ack_message)
- end
- it "logs a warning" do
- subject.ack_message(message)
- expect(logger).to have_received(:debug).with("this adapter does not support client acks")
- end
+ it "calls #ack on the message and passes the supplied options" do
+ subject.ack_message(message, options)
+ expect(message).to have_received(:ack).with(options)
end
end
describe "#nack_message" do
let(:message) { double("message") }
let(:options) { {foo: :bar} }
before do
- adapter_context.stub(:nack_message)
+ allow(message).to receive(:nack)
end
- context "when the adapter supports client acks" do
- before do
- adapter_context.stub(:supports_client_acks?) { true }
- end
- it "calls #nack_message with the message" do
- subject.nack_message(message)
- adapter_context.should have_received(:nack_message).with(message, {})
- end
- it "passes the supplied options to nack_message" do
- subject.nack_message(message, options)
- adapter_context.should have_received(:nack_message).with(message, options)
- end
+ it "calls #nack on the message" do
+ subject.nack_message(message)
+ expect(message).to have_received(:nack).with({})
end
- context "when the adapter doesn't support client acks" do
- before do
- adapter_context.stub(:supports_client_acks?) { false }
- end
- it "doesn't call #nack_message" do
- subject.nack_message(message)
- adapter_context.should_not have_received(:nack_message)
- end
- it "logs a warning" do
- subject.nack_message(message)
- expect(logger).to have_received(:debug).with("this adapter does not support client acks")
- end
+ it "calls #nack on the message and passes the supplied options" do
+ subject.nack_message(message, options)
+ expect(message).to have_received(:nack).with(options)
end
end
describe "#subscribe" do
- let(:destination) { Broker.destination(:my_queue, "my_queue", exclusive: true) }
+ let(:destination) { broker.destination(:my_queue, "my_queue", exclusive: true) }
let(:consumer_double) { lambda do |m| end }
before do
adapter_context.stub(:subscribe)
- Broker.consumer(:my_consumer, &consumer_double)
+ broker.consumer(:my_consumer, &consumer_double)
end
it "delegates to the adapter_context" do
adapter_context.should_receive(:subscribe).with(destination, {}) do |&blk|
expect(blk).to be(consumer_double)
@@ -359,11 +329,11 @@
end
end
end
describe "#subscribe_with" do
- let(:destination) { Broker.destination(:my_queue, "my_queue", exclusive: true) }
+ let(:destination) { broker.destination(:my_queue, "my_queue", exclusive: true) }
let(:consumer_double) { lambda do |m| end }
before do
adapter_context.stub(:subscribe)
end
@@ -409,8 +379,45 @@
end
context "when the module is used directly" do
subject { described_class }
it_behaves_like "a Client"
+ end
+
+ describe ".for_broker" do
+ let(:broker_name) { :my_cool_broker }
+ let(:client) { described_class.for_broker(broker_name) }
+ it "produces a module that extends #{described_class.name}" do
+ expect(client).to be_a Module
+ expect(client).to be_kind_of described_class
+ end
+
+ it "knows it's broker" do
+ expect(client.broker_name).to eq(broker_name)
+ expect(client.broker).to be(broker)
+ end
+
+ context "when the resulting module is used as an included module" do
+ subject! do
+ clz = Class.new
+ clz.send :include, client
+ clz.new
+ end
+ it_behaves_like "a Client"
+ end
+
+ context "when the resulting module is used directly" do
+ it_behaves_like "a Client" do
+ subject! { client }
+ end
+ end
+ end
+
+ describe ".[]" do
+ it "grabs the client for the given broker" do
+ expected = double("client")
+ allow(Broker).to receive(:client).with(:test_broker).and_return(expected)
+ expect(described_class[:test_broker]).to be expected
+ end
end
end
end