right_amqp 0.3.3 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -34,6 +34,7 @@ describe RightAMQP::HABrokerClient do
34
34
  before(:each) do
35
35
  setup_logger
36
36
  @exceptions = RightSupport::Stats::Exceptions
37
+ @non_deliveries = RightSupport::Stats::Activity
37
38
  @message = "message"
38
39
  @packet = flexmock("packet", :class => RequestMock, :to_s => true, :version => [12, 12]).by_default
39
40
  @serializer = flexmock("serializer")
@@ -75,6 +76,13 @@ describe RightAMQP::HABrokerClient do
75
76
  context.failed.should == []
76
77
  end
77
78
 
79
+ it "should store identity of failed brokers" do
80
+ context = RightAMQP::HABrokerClient::Context.new(@packet1, @options, @brokers)
81
+ context.record_failure("rs-broker-1-1")
82
+ context.record_failure("rs-broker-2-2")
83
+ context.failed.should == ["rs-broker-1-1", "rs-broker-2-2"]
84
+ end
85
+
78
86
  end
79
87
 
80
88
  describe "Caching" do
@@ -163,35 +171,42 @@ describe RightAMQP::HABrokerClient do
163
171
  @identity = "rs-broker-localhost-5672"
164
172
  @address = {:host => "localhost", :port => 5672, :index => 0}
165
173
  @broker = flexmock("broker_client", :identity => @identity, :usable? => true)
166
- @broker.should_receive(:return_message).by_default
167
174
  @broker.should_receive(:update_status).by_default
168
175
  flexmock(RightAMQP::BrokerClient).should_receive(:new).and_return(@broker).by_default
169
176
  end
170
177
 
171
178
  it "should create a broker client for default host and port" do
172
179
  flexmock(RightAMQP::BrokerClient).should_receive(:new).with(@identity, @address, @serializer,
173
- @exceptions, Hash, nil).and_return(@broker).once
180
+ @exceptions, @non_deliveries, Hash, nil).and_return(@broker).once
174
181
  ha = RightAMQP::HABrokerClient.new(@serializer)
175
182
  ha.brokers.should == [@broker]
176
183
  end
177
184
 
178
185
  it "should create broker clients for specified hosts and ports and assign index in order of creation" do
179
186
  address1 = {:host => "first", :port => 5672, :index => 0}
180
- broker1 = flexmock("broker_client1", :identity => "rs-broker-first-5672", :usable? => true, :return_message => true)
187
+ broker1 = flexmock("broker_client1", :identity => "rs-broker-first-5672", :usable? => true)
181
188
  flexmock(RightAMQP::BrokerClient).should_receive(:new).with("rs-broker-first-5672", address1, @serializer,
182
- @exceptions, Hash, nil).and_return(broker1).once
189
+ @exceptions, @non_deliveries, Hash, nil).and_return(broker1).once
183
190
  address2 = {:host => "second", :port => 5672, :index => 1}
184
- broker2 = flexmock("broker_client2", :identity => "rs-broker-second-5672", :usable? => true, :return_message => true)
191
+ broker2 = flexmock("broker_client2", :identity => "rs-broker-second-5672", :usable? => true)
185
192
  flexmock(RightAMQP::BrokerClient).should_receive(:new).with("rs-broker-second-5672", address2, @serializer,
186
- @exceptions, Hash, nil).and_return(broker2).once
193
+ @exceptions, @non_deliveries, Hash, nil).and_return(broker2).once
187
194
  ha = RightAMQP::HABrokerClient.new(@serializer, :host => "first, second", :port => 5672)
188
195
  ha.brokers.should == [broker1, broker2]
189
196
  end
190
197
 
191
- it "should setup to receive returned messages from each usable broker client" do
192
- @broker.should_receive(:return_message).twice
193
- flexmock(RightAMQP::BrokerClient).should_receive(:new).and_return(@broker).twice
194
- RightAMQP::HABrokerClient.new(@serializer, :host => "first, second", :port => 5672)
198
+ it "should setup to receive status updates from each broker client" do
199
+ broker = flexmock("broker_client", :identity => "rs-broker-first-5672")
200
+ flexmock(RightAMQP::BrokerClient).should_receive(:new).with("rs-broker-first-5672", Hash, @serializer,
201
+ @exceptions, @non_deliveries, on { |arg| arg[:update_status_callback].is_a?(Proc) }, nil).and_return(broker).once
202
+ RightAMQP::HABrokerClient.new(@serializer, :host => "first", :port => 5672)
203
+ end
204
+
205
+ it "should setup to receive returned messages from each broker client" do
206
+ broker = flexmock("broker_client", :identity => "rs-broker-first-5672")
207
+ flexmock(RightAMQP::BrokerClient).should_receive(:new).with("rs-broker-first-5672", Hash, @serializer,
208
+ @exceptions, @non_deliveries, on { |arg| arg[:return_message_callback].is_a?(Proc) }, nil).and_return(broker).once
209
+ RightAMQP::HABrokerClient.new(@serializer, :host => "first", :port => 5672)
195
210
  end
196
211
 
197
212
  end # when initializing
@@ -282,24 +297,24 @@ describe RightAMQP::HABrokerClient do
282
297
  before(:each) do
283
298
  @address1 = {:host => "first", :port => 5672, :index => 0}
284
299
  @identity1 = "rs-broker-first-5672"
285
- @broker1 = flexmock("broker_client1", :identity => @identity1, :usable? => true, :return_message => true,
300
+ @broker1 = flexmock("broker_client1", :identity => @identity1, :usable? => true,
286
301
  :alias => "b0", :host => "first", :port => 5672, :index => 0)
287
302
  flexmock(RightAMQP::BrokerClient).should_receive(:new).with(@identity1, @address1, @serializer,
288
- @exceptions, Hash, nil).and_return(@broker1).by_default
303
+ @exceptions, @non_deliveries, Hash, nil).and_return(@broker1).by_default
289
304
 
290
305
  @address2 = {:host => "second", :port => 5672, :index => 1}
291
306
  @identity2 = "rs-broker-second-5672"
292
- @broker2 = flexmock("broker_client2", :identity => @identity2, :usable? => true, :return_message => true,
307
+ @broker2 = flexmock("broker_client2", :identity => @identity2, :usable? => true,
293
308
  :alias => "b1", :host => "second", :port => 5672, :index => 1)
294
309
  flexmock(RightAMQP::BrokerClient).should_receive(:new).with(@identity2, @address2, @serializer,
295
- @exceptions, Hash, nil).and_return(@broker2).by_default
310
+ @exceptions, @non_deliveries, Hash, nil).and_return(@broker2).by_default
296
311
 
297
312
  @address3 = {:host => "third", :port => 5672, :index => 2}
298
313
  @identity3 = "rs-broker-third-5672"
299
- @broker3 = flexmock("broker_client3", :identity => @identity3, :usable? => true, :return_message => true,
314
+ @broker3 = flexmock("broker_client3", :identity => @identity3, :usable? => true,
300
315
  :alias => "b2", :host => "third", :port => 5672, :index => 2)
301
316
  flexmock(RightAMQP::BrokerClient).should_receive(:new).with(@identity3, @address3, @serializer,
302
- @exceptions, Hash, nil).and_return(@broker3).by_default
317
+ @exceptions, @non_deliveries, Hash, nil).and_return(@broker3).by_default
303
318
  end
304
319
 
305
320
  it "should use host and port to uniquely identity broker in AgentIdentity format" do
@@ -399,9 +414,8 @@ describe RightAMQP::HABrokerClient do
399
414
  eval("@broker#{k}.should_receive(:usable?).and_return(true).by_default")
400
415
  eval("@broker#{k}.should_receive(:connected?).and_return(true).by_default")
401
416
  eval("@broker#{k}.should_receive(:subscribe).and_return(true).by_default")
402
- eval("@broker#{k}.should_receive(:return_message).and_return(true).by_default")
403
417
  eval("flexmock(RightAMQP::BrokerClient).should_receive(:new).with(@identity#{k}, @address#{k}, " +
404
- "@serializer, @exceptions, Hash, nil).and_return(@broker#{k}).by_default")
418
+ "@serializer, @exceptions, @non_deliveries, Hash, nil).and_return(@broker#{k}).by_default")
405
419
  end
406
420
  end
407
421
 
@@ -412,7 +426,7 @@ describe RightAMQP::HABrokerClient do
412
426
  ha.brokers.size.should == 1
413
427
  ha.brokers[0].alias == "b0"
414
428
  flexmock(RightAMQP::BrokerClient).should_receive(:new).with(@identity2, @address2, @serializer,
415
- @exceptions, Hash, nil).and_return(@broker2).once
429
+ @exceptions, @non_deliveries, Hash, nil).and_return(@broker2).once
416
430
  res = ha.connect("second", 5672, 1)
417
431
  res.should be_true
418
432
  ha.brokers.size.should == 2
@@ -425,7 +439,7 @@ describe RightAMQP::HABrokerClient do
425
439
  @broker1.should_receive(:usable?).and_return(false)
426
440
  @broker1.should_receive(:close).and_return(true).once
427
441
  flexmock(RightAMQP::BrokerClient).should_receive(:new).with(@identity1, @address1, @serializer,
428
- @exceptions, Hash, ha.brokers[0]).and_return(@broker1).once
442
+ @exceptions, @non_deliveries, Hash, ha.brokers[0]).and_return(@broker1).once
429
443
  res = ha.connect("first", 5672, 0)
430
444
  res.should be_true
431
445
  ha.brokers.size.should == 2
@@ -440,7 +454,7 @@ describe RightAMQP::HABrokerClient do
440
454
  @broker1.should_receive(:status).and_return(:connected).once
441
455
  @broker1.should_receive(:close).and_return(true).never
442
456
  flexmock(RightAMQP::BrokerClient).should_receive(:new).with(@identity1, @address1, @serializer,
443
- @exceptions, Hash, ha.brokers[0]).and_return(@broker1).never
457
+ @exceptions, @non_deliveries, Hash, ha.brokers[0]).and_return(@broker1).never
444
458
  res = ha.connect("first", 5672, 0)
445
459
  res.should be_false
446
460
  ha.brokers.size.should == 2
@@ -454,7 +468,7 @@ describe RightAMQP::HABrokerClient do
454
468
  ha.brokers.size.should == 2
455
469
  @broker1.should_receive(:close).and_return(true).once
456
470
  flexmock(RightAMQP::BrokerClient).should_receive(:new).with(@identity1, @address1, @serializer,
457
- @exceptions, Hash, ha.brokers[0]).and_return(@broker1).once
471
+ @exceptions, @non_deliveries, Hash, ha.brokers[0]).and_return(@broker1).once
458
472
  res = ha.connect("first", 5672, 0, nil, force = true)
459
473
  res.should be_true
460
474
  ha.brokers.size.should == 2
@@ -467,7 +481,7 @@ describe RightAMQP::HABrokerClient do
467
481
  ha.brokers.size.should == 2
468
482
  @broker1.should_receive(:close).and_return(true).once
469
483
  flexmock(RightAMQP::BrokerClient).should_receive(:new).with(@identity3, @address3.merge(:index => 0),
470
- @serializer, @exceptions, Hash, nil).and_return(@broker3).once
484
+ @serializer, @exceptions, @non_deliveries, Hash, nil).and_return(@broker3).once
471
485
  res = ha.connect("third", 5672, 0)
472
486
  res.should be_true
473
487
  ha.brokers.size.should == 2
@@ -755,34 +769,6 @@ describe RightAMQP::HABrokerClient do
755
769
  @broker3.should_receive(:publish).and_return(true).by_default
756
770
  end
757
771
 
758
- it "should invoke return block" do
759
- ha = RightAMQP::HABrokerClient.new(@serializer, :host => "first, second, third")
760
- @broker1.should_receive(:return_message).and_yield("exchange", "NO_CONSUMERS", @message).once
761
- called = 0
762
- ha.return_message do |id, reason, message, to, context|
763
- called += 1
764
- id.should == @identity1
765
- reason.should == "NO_CONSUMERS"
766
- message.should == @message
767
- to.should == "exchange"
768
- end
769
- called.should == 1
770
- end
771
-
772
- it "should record failure in message context if there is message context" do
773
- ha = RightAMQP::HABrokerClient.new(@serializer, :host => "first, second, third")
774
- ha.publish({:type => :direct, :name => "exchange", :options => {:durable => true}},
775
- @packet, :mandatory => true).should == [@identity1]
776
- @broker1.should_receive(:return_message).and_yield("exchange", "NO_CONSUMERS", @message).once
777
- ha.return_message do |id, reason, message, to, context|
778
- id.should == @identity1
779
- reason.should == "NO_CONSUMERS"
780
- message.should == @message
781
- to.should == "exchange"
782
- end
783
- ha.instance_variable_get(:@published).fetch(@message).failed.should == [@identity1]
784
- end
785
-
786
772
  context "when non-delivery" do
787
773
 
788
774
  it "should store non-delivery block for use by return handler" do
@@ -799,83 +785,79 @@ describe RightAMQP::HABrokerClient do
799
785
  before(:each) do
800
786
  @options = {}
801
787
  @brokers = [@identity1, @identity2]
788
+ @ha = RightAMQP::HABrokerClient.new(@serializer, :host => "first, second")
802
789
  @context = RightAMQP::HABrokerClient::Context.new(@packet, @options, @brokers)
790
+ flexmock(@ha.instance_variable_get(:@published)).should_receive(:fetch).with(@message).and_return(@context).by_default
803
791
  end
804
792
 
805
793
  it "should republish using a broker not yet tried if possible and log that re-routing" do
806
794
  @logger.should_receive(:info).with(/RE-ROUTE/).once
807
795
  @logger.should_receive(:info).with(/RETURN reason/).once
808
- ha = RightAMQP::HABrokerClient.new(@serializer, :host => "first, second")
809
- @context.record_failure(@identity1)
810
796
  @broker2.should_receive(:publish).and_return(true).once
811
- ha.__send__(:handle_return, @identity1, "reason", @message, "to", @context)
797
+ @context.record_failure(@identity1)
798
+ @ha.__send__(:handle_return, @identity1, "to", "reason", @message)
812
799
  end
813
800
 
814
801
  it "should republish to same broker without mandatory if message is persistent and no other brokers available" do
815
802
  @logger.should_receive(:info).with(/RE-ROUTE/).once
816
803
  @logger.should_receive(:info).with(/RETURN reason/).once
817
- ha = RightAMQP::HABrokerClient.new(@serializer, :host => "first, second")
818
804
  @context.record_failure(@identity1)
819
805
  @context.record_failure(@identity2)
820
806
  @packet.should_receive(:persistent).and_return(true)
821
807
  @broker1.should_receive(:publish).and_return(true).once
822
- ha.__send__(:handle_return, @identity2, "NO_CONSUMERS", @message, "to", @context)
808
+ @ha.__send__(:handle_return, @identity2, "to", "NO_CONSUMERS", @message)
823
809
  end
824
810
 
825
811
  it "should republish to same broker without mandatory if message is one-way and no other brokers available" do
826
812
  @logger.should_receive(:info).with(/RE-ROUTE/).once
827
813
  @logger.should_receive(:info).with(/RETURN reason/).once
828
- ha = RightAMQP::HABrokerClient.new(@serializer, :host => "first, second")
829
814
  @context.record_failure(@identity1)
830
815
  @context.record_failure(@identity2)
831
816
  @packet.should_receive(:one_way).and_return(true)
832
817
  @broker1.should_receive(:publish).and_return(true).once
833
- ha.__send__(:handle_return, @identity2, "NO_CONSUMERS", @message, "to", @context)
818
+ @ha.__send__(:handle_return, @identity2, "to", "NO_CONSUMERS", @message)
834
819
  end
835
820
 
836
821
  it "should update status to :stopping if message returned because access refused" do
837
822
  @logger.should_receive(:info).with(/RE-ROUTE/).once
838
823
  @logger.should_receive(:info).with(/RETURN reason/).once
839
- ha = RightAMQP::HABrokerClient.new(@serializer, :host => "first, second")
840
824
  @context.record_failure(@identity1)
841
825
  @broker2.should_receive(:publish).and_return(true).once
842
826
  @broker1.should_receive(:update_status).with(:stopping).and_return(true).once
843
- ha.__send__(:handle_return, @identity1, "ACCESS_REFUSED", @message, "to", @context)
827
+ @ha.__send__(:handle_return, @identity1, "to", "ACCESS_REFUSED", @message)
844
828
  end
845
829
 
846
830
  it "should log info and make non-delivery call even if persistent when returned because of no queue" do
847
831
  @logger.should_receive(:info).with(/NO ROUTE/).once
848
832
  @logger.should_receive(:info).with(/RETURN reason/).once
849
- ha = RightAMQP::HABrokerClient.new(@serializer, :host => "first, second")
850
833
  called = 0
851
- ha.non_delivery { |reason, type, token, from, to| called += 1 }
834
+ @ha.non_delivery { |reason, type, token, from, to| called += 1 }
852
835
  @context.record_failure(@identity1)
853
836
  @context.record_failure(@identity2)
854
837
  @packet.should_receive(:persistent).and_return(true)
855
838
  @broker1.should_receive(:publish).and_return(true).never
856
839
  @broker2.should_receive(:publish).and_return(true).never
857
- ha.__send__(:handle_return, @identity2, "NO_QUEUE", @message, "to", @context)
840
+ @ha.__send__(:handle_return, @identity2, "to", "NO_QUEUE", @message)
858
841
  called.should == 1
859
842
  end
860
843
 
861
844
  it "should log info and make non-delivery call if no route can be found" do
862
845
  @logger.should_receive(:info).with(/NO ROUTE/).once
863
846
  @logger.should_receive(:info).with(/RETURN reason/).once
864
- ha = RightAMQP::HABrokerClient.new(@serializer, :host => "first, second")
865
847
  called = 0
866
- ha.non_delivery { |reason, type, token, from, to| called += 1 }
848
+ @ha.non_delivery { |reason, type, token, from, to| called += 1 }
867
849
  @context.record_failure(@identity1)
868
850
  @context.record_failure(@identity2)
869
851
  @broker1.should_receive(:publish).and_return(true).never
870
852
  @broker2.should_receive(:publish).and_return(true).never
871
- ha.__send__(:handle_return, @identity2, "any reason", @message, "to", @context)
853
+ @ha.__send__(:handle_return, @identity2, "to", "any reason", @message)
872
854
  called.should == 1
873
855
  end
874
856
 
875
857
  it "should log info if no message context available for re-routing it" do
876
858
  @logger.should_receive(:info).with(/Dropping/).once
877
- ha = RightAMQP::HABrokerClient.new(@serializer, :host => "first, second")
878
- ha.__send__(:handle_return, @identity2, "any reason", @message, "to", nil)
859
+ flexmock(@ha.instance_variable_get(:@published)).should_receive(:fetch).with(@message).and_return(nil).once
860
+ @ha.__send__(:handle_return, @identity2, "to", "any reason", @message)
879
861
  end
880
862
 
881
863
  end
@@ -999,7 +981,6 @@ describe RightAMQP::HABrokerClient do
999
981
  @broker.should_receive(:usable?).and_return(true).by_default
1000
982
  @broker.should_receive(:connected?).and_return(true).by_default
1001
983
  @broker.should_receive(:subscribe).and_return(true).by_default
1002
- @broker.should_receive(:return_message).and_return(true).by_default
1003
984
  flexmock(RightAMQP::BrokerClient).should_receive(:new).and_return(@broker).by_default
1004
985
  @broker1.should_receive(:failed?).and_return(false).by_default
1005
986
  @broker2.should_receive(:failed?).and_return(false).by_default
@@ -1083,6 +1064,7 @@ describe RightAMQP::HABrokerClient do
1083
1064
  ha.stats.should == {"brokers" => ["stats1", "stats2", "stats3"],
1084
1065
  "exceptions" => nil,
1085
1066
  "heartbeat" => nil,
1067
+ "non-deliveries" => nil,
1086
1068
  "returns" => nil}
1087
1069
  end
1088
1070
 
@@ -1205,7 +1187,7 @@ describe RightAMQP::HABrokerClient do
1205
1187
 
1206
1188
  it "should provide connection status callback only once when one-off is requested" do
1207
1189
  flexmock(RightAMQP::BrokerClient).should_receive(:new).with(@identity, @address, @serializer,
1208
- @exceptions, Hash, nil).and_return(@broker).once
1190
+ @exceptions, @non_deliveries, Hash, nil).and_return(@broker).once
1209
1191
  ha = RightAMQP::HABrokerClient.new(@serializer)
1210
1192
  called = 0
1211
1193
  ha.connection_status(:one_off => 10) { |_| called += 1 }
@@ -1221,7 +1203,7 @@ describe RightAMQP::HABrokerClient do
1221
1203
  flexmock(EM::Timer).should_receive(:new).and_return(@timer).once
1222
1204
  @timer.should_receive(:cancel).once
1223
1205
  flexmock(RightAMQP::BrokerClient).should_receive(:new).with(@identity, @address, @serializer,
1224
- @exceptions, Hash, nil).and_return(@broker).once
1206
+ @exceptions, @non_deliveries, Hash, nil).and_return(@broker).once
1225
1207
  ha = RightAMQP::HABrokerClient.new(@serializer)
1226
1208
  called = 0
1227
1209
  ha.connection_status(:one_off => 10) { |_| called += 1 }
@@ -1233,7 +1215,7 @@ describe RightAMQP::HABrokerClient do
1233
1215
  flexmock(EM::Timer).should_receive(:new).and_return(@timer).and_yield.once
1234
1216
  @timer.should_receive(:cancel).never
1235
1217
  flexmock(RightAMQP::BrokerClient).should_receive(:new).with(@identity, @address, @serializer,
1236
- @exceptions, Hash, nil).and_return(@broker).once
1218
+ @exceptions, @non_deliveries, Hash, nil).and_return(@broker).once
1237
1219
  ha = RightAMQP::HABrokerClient.new(@serializer)
1238
1220
  called = 0
1239
1221
  ha.connection_status(:one_off => 10) { |status| called += 1; status.should == :timeout }
@@ -1242,7 +1224,7 @@ describe RightAMQP::HABrokerClient do
1242
1224
 
1243
1225
  it "should be able to have multiple connection status callbacks" do
1244
1226
  flexmock(RightAMQP::BrokerClient).should_receive(:new).with(@identity, @address, @serializer,
1245
- @exceptions, Hash, nil).and_return(@broker).once
1227
+ @exceptions, @non_deliveries, Hash, nil).and_return(@broker).once
1246
1228
  ha = RightAMQP::HABrokerClient.new(@serializer)
1247
1229
  called1 = 0
1248
1230
  called2 = 0
data/spec/spec_helper.rb CHANGED
@@ -37,7 +37,7 @@ module RightAMQP
37
37
 
38
38
  module SpecHelper
39
39
 
40
- # Setup mocking of logger such that need to override :error and :warning
40
+ # Setup mocking of logger such that need to override :error and :warn
41
41
  # in specs that are expected to require use of these methods
42
42
  # Do not mock :exception because that gets eaten by Log::Mixin and results
43
43
  # in :error call
@@ -46,7 +46,7 @@ module RightAMQP
46
46
  @logger.should_receive(:level).and_return(:info).by_default
47
47
  @logger.should_receive(:exception).by_default.and_return { |m| raise m }
48
48
  @logger.should_receive(:error).by_default.and_return { |m| raise m }
49
- @logger.should_receive(:warning).by_default.and_return { |m| raise m }
49
+ @logger.should_receive(:warn).by_default.and_return { |m| raise m }
50
50
  @logger.should_receive(:info).by_default
51
51
  @logger.should_receive(:debug).by_default
52
52
  RightSupport::Log::Mixin.default_logger = @logger
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: right_amqp
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.3
5
+ version: 0.5.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Lee Kirchhoff
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-10-30 00:00:00 Z
13
+ date: 2012-09-27 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: right_support
@@ -108,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
108
  requirements:
109
109
  - - ">="
110
110
  - !ruby/object:Gem::Version
111
- hash: 3853339962188716916
111
+ hash: -2478325882145006722
112
112
  segments:
113
113
  - 0
114
114
  version: "0"