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.
@@ -40,11 +40,15 @@ describe RightAMQP::BrokerClient do
40
40
  @serializer.should_receive(:load).with(@message).and_return(@packet).by_default
41
41
  @exceptions = flexmock("exceptions")
42
42
  @exceptions.should_receive(:track).never.by_default
43
+ @non_deliveries = flexmock("non-deliveries")
44
+ @non_deliveries.should_receive(:update).never.by_default
43
45
  @connection = flexmock("connection")
44
46
  @connection.should_receive(:connection_status).by_default
45
47
  flexmock(AMQP).should_receive(:connect).and_return(@connection).by_default
46
48
  @channel = flexmock("AMQP connection channel")
47
49
  @channel.should_receive(:connection).and_return(@connection).by_default
50
+ @channel.should_receive(:return_message).and_return(true).by_default
51
+ flexmock(MQ).should_receive(:new).and_return(@channel).by_default
48
52
  @identity = "rs-broker-localhost-5672"
49
53
  @address = {:host => "localhost", :port => 5672, :index => 0}
50
54
  @options = {}
@@ -62,8 +66,8 @@ describe RightAMQP::BrokerClient do
62
66
  it "should create a broker with AMQP connection for specified address" do
63
67
  @amqp.should_receive(:connect).with(hsh(:user => "user", :pass => "pass", :vhost => "vhost", :host => "localhost",
64
68
  :port => 5672, :insist => true, :reconnect_interval => 10)).and_return(@connection).once
65
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, {:user => "user",
66
- :pass => "pass", :vhost => "vhost", :insist => true,
69
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries,
70
+ {:user => "user", :pass => "pass", :vhost => "vhost", :insist => true,
67
71
  :reconnect_interval => 10})
68
72
  broker.host.should == "localhost"
69
73
  broker.port.should == 5672
@@ -77,16 +81,16 @@ describe RightAMQP::BrokerClient do
77
81
  end
78
82
 
79
83
  it "should update state from existing client for given broker" do
80
- existing = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
84
+ existing = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
81
85
  existing.__send__(:update_status, :disconnected)
82
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options, existing)
86
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options, existing)
83
87
  broker.summary.should == {:alias => "b0", :identity => @identity, :status => :connecting,
84
88
  :disconnects => 1, :failures => 0, :retries => 0}
85
89
  end
86
90
 
87
91
  it "should log an info message when it creates an AMQP connection" do
88
92
  @logger.should_receive(:info).with(/Connecting to broker/).once
89
- RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
93
+ RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
90
94
  end
91
95
 
92
96
  it "should log an error and set status to :failed if it fails to create an AMQP connection" do
@@ -95,19 +99,25 @@ describe RightAMQP::BrokerClient do
95
99
  @logger.should_receive(:info).once
96
100
  @logger.should_receive(:error).with(/Failed connecting/).once
97
101
  flexmock(MQ).should_receive(:new).with(@connection).and_raise(Exception)
98
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
102
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
99
103
  broker.summary.should == {:alias => "b0", :identity => @identity, :status => :failed,
100
104
  :disconnects => 0, :failures => 1, :retries => 0}
101
105
  end
102
106
 
103
107
  it "should set initialize connection status callback" do
104
108
  @connection.should_receive(:connection_status).once
105
- RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
109
+ RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
106
110
  end
107
111
 
108
- it "should set broker prefetch value if specified" do
112
+ it "should set broker prefetch value only if specified" do
109
113
  @channel.should_receive(:prefetch).with(1).once
110
- RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, {:prefetch => 1})
114
+ RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
115
+ RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, {:prefetch => 1})
116
+ end
117
+
118
+ it "should setup for message return" do
119
+ @channel.should_receive(:return_message).with(Proc).once
120
+ RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
111
121
  end
112
122
 
113
123
  end # when initializing connection
@@ -115,7 +125,7 @@ describe RightAMQP::BrokerClient do
115
125
  context "when subscribing" do
116
126
 
117
127
  before(:each) do
118
- @info = flexmock("info", :ack => true).by_default
128
+ @header = flexmock("header", :ack => true).by_default
119
129
  @serializer.should_receive(:load).with(@message).and_return(@packet).by_default
120
130
  @direct = flexmock("direct")
121
131
  @fanout = flexmock("fanout")
@@ -131,7 +141,7 @@ describe RightAMQP::BrokerClient do
131
141
  it "should subscribe queue to exchange" do
132
142
  @queue.should_receive(:bind).and_return(@bind).once
133
143
  @bind.should_receive(:subscribe).once
134
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
144
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
135
145
  broker.__send__(:update_status, :ready)
136
146
  broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}) {|_, _|}
137
147
  end
@@ -139,7 +149,7 @@ describe RightAMQP::BrokerClient do
139
149
  it "should subscribe queue to second exchange if specified" do
140
150
  @queue.should_receive(:bind).and_return(@bind).twice
141
151
  @bind.should_receive(:subscribe).once
142
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
152
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
143
153
  broker.__send__(:update_status, :ready)
144
154
  options = {:exchange2 => {:type => :fanout, :name => "exchange2", :options => {:durable => true}}}
145
155
  broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}, options) {|_, _|}
@@ -147,27 +157,27 @@ describe RightAMQP::BrokerClient do
147
157
 
148
158
  it "should subscribe queue to exchange when still connecting" do
149
159
  @bind.should_receive(:subscribe).once
150
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
160
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
151
161
  broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}) {|_, _|}
152
162
  end
153
163
 
154
164
  it "should subscribe queue to empty exchange if no exchange specified" do
155
165
  @queue.should_receive(:subscribe).once
156
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
166
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
157
167
  broker.__send__(:update_status, :ready)
158
168
  broker.subscribe({:name => "queue"}) {|b, p| p.should == nil}
159
169
  end
160
170
 
161
171
  it "should store queues for future reference" do
162
172
  @bind.should_receive(:subscribe).once
163
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
164
- broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"})
173
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
174
+ broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}) {|_, _|}
165
175
  broker.queues.should == [@queue]
166
176
  end
167
177
 
168
178
  it "should return true if subscribed successfully" do
169
- @bind.should_receive(:subscribe).and_yield(@info, @message).once
170
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
179
+ @bind.should_receive(:subscribe).and_yield(@header, @message).once
180
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
171
181
  result = broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"},
172
182
  RequestMock => true) {|b, p| p.should == @packet}
173
183
  result.should be_true
@@ -175,8 +185,8 @@ describe RightAMQP::BrokerClient do
175
185
 
176
186
  it "should return true if already subscribed and not try to resubscribe" do
177
187
  @queue.should_receive(:name).and_return("queue").once
178
- @bind.should_receive(:subscribe).and_yield(@info, @message).once
179
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
188
+ @bind.should_receive(:subscribe).and_yield(@header, @message).once
189
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
180
190
  result = broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"},
181
191
  RequestMock => true) {|b, p| p.should == @packet}
182
192
  result.should be_true
@@ -184,10 +194,9 @@ describe RightAMQP::BrokerClient do
184
194
  result.should be_true
185
195
  end
186
196
 
187
- it "should ack received message if requested" do
188
- @info.should_receive(:ack).once
189
- @bind.should_receive(:subscribe).and_yield(@info, @message).once
190
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
197
+ it "should enable message ack by subscribe caller if requested" do
198
+ @bind.should_receive(:subscribe).with({:ack => true}, Proc).and_yield(@header, @message).once
199
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
191
200
  broker.__send__(:update_status, :ready)
192
201
  result = broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"},
193
202
  :ack => true, RequestMock => true) {|b, p| p.should == @packet}
@@ -197,130 +206,188 @@ describe RightAMQP::BrokerClient do
197
206
  it "should return false if client not usable" do
198
207
  @queue.should_receive(:bind).and_return(@bind).never
199
208
  @bind.should_receive(:subscribe).never
200
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
209
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
201
210
  broker.__send__(:update_status, :disconnected)
202
- broker.subscribe({:name => "queue"}).should be_false
211
+ broker.subscribe({:name => "queue"}) { |_, _| }.should be_false
203
212
  end
204
-
205
- it "should receive message causing it to be unserialized and logged" do
213
+
214
+ it "should raise if no block supplied" do
215
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
216
+ lambda { broker.subscribe({:name => "queue"}) }.should raise_error(ArgumentError)
217
+ end
218
+
219
+ it "should receive message" do
206
220
  @logger.should_receive(:info).with(/Connecting/).once
207
221
  @logger.should_receive(:info).with(/Subscribing/).once
208
- @logger.should_receive(:info).with(/RECV/).once
209
- @serializer.should_receive(:load).with(@message).and_return(@packet).once
210
- @bind.should_receive(:subscribe).and_yield(@info, @message).once
211
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
222
+ @bind.should_receive(:subscribe).and_yield(@header, @message).once
223
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
224
+ subscribe_options = {RequestMock => nil}
225
+ flexmock(broker).should_receive(:receive).with("queue", @header, @message, subscribe_options, Proc).once
212
226
  broker.__send__(:update_status, :ready)
213
227
  broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"},
214
- RequestMock => nil) {|b, p| p.class.should == RequestMock}
228
+ subscribe_options) {|b, p| p.class.should == RequestMock}
215
229
  end
216
230
 
217
- it "should receive message and log exception if subscribe block fails" do
231
+ it "should receive message using fiber pool specified as subscribe option" do
218
232
  @logger.should_receive(:info).with(/Connecting/).once
219
233
  @logger.should_receive(:info).with(/Subscribing/).once
220
- @logger.should_receive(:error).with(/Failed executing block/).once
234
+ @bind.should_receive(:subscribe).and_yield(@header, @message).once
235
+ fiber_pool = flexmock("fiber pool")
236
+ fiber_pool.should_receive(:spawn).and_yield.once
237
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
238
+ subscribe_options = {RequestMock => nil, :fiber_pool => fiber_pool}
239
+ flexmock(broker).should_receive(:receive).with("queue", @header, @message, subscribe_options, Proc).once
240
+ broker.__send__(:update_status, :ready)
241
+ broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"},
242
+ subscribe_options) {|b, p| p.class.should == RequestMock}
243
+ end
244
+
245
+ it "should receive message using fiber pool specified as constructor option" do
246
+ @logger.should_receive(:info).with(/Connecting/).once
247
+ @logger.should_receive(:info).with(/Subscribing/).once
248
+ @bind.should_receive(:subscribe).and_yield(@header, @message).once
249
+ fiber_pool = flexmock("fiber pool")
250
+ fiber_pool.should_receive(:spawn).and_yield.once
251
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries,
252
+ @options.merge(:fiber_pool => fiber_pool))
253
+ subscribe_options = {RequestMock => nil}
254
+ flexmock(broker).should_receive(:receive).with("queue", @header, @message, subscribe_options, Proc).once
255
+ broker.__send__(:update_status, :ready)
256
+ broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"},
257
+ subscribe_options) {|b, p| p.class.should == RequestMock}
258
+ end
259
+
260
+ it "should receive message and log exception if subscribe block fails and then ack if option set" do
261
+ @logger.should_receive(:info).with(/Connecting/).once
262
+ @logger.should_receive(:info).with(/Subscribing/).once
263
+ @logger.should_receive(:error).with(/Failed receiving message/).once
221
264
  @exceptions.should_receive(:track).once
265
+ @non_deliveries.should_receive(:update).once
222
266
  @serializer.should_receive(:load).with(@message).and_return(@packet).once
223
- @bind.should_receive(:subscribe).and_yield(@info, @message).once
224
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
267
+ @header.should_receive(:ack).once
268
+ @bind.should_receive(:subscribe).and_yield(@header, @message).once
269
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
225
270
  broker.__send__(:update_status, :ready)
226
271
  result = broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"},
227
- RequestMock => nil) {|b, p| raise Exception}
272
+ :ack => true, RequestMock => nil) {|b, p| raise Exception}
228
273
  result.should be_false
229
274
  end
230
275
 
231
- it "should ignore 'nil' message when using ack" do
232
- @logger.should_receive(:level).and_return(:debug)
276
+ it "should log an error if a subscribe fails" do
233
277
  @logger.should_receive(:info).with(/Connecting/).once
234
- @logger.should_receive(:info).with(/Subscribing/).once
235
- @logger.should_receive(:debug).with(/nil message ignored/).once
236
- @bind.should_receive(:subscribe).and_yield(@info, "nil").once
237
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
278
+ @logger.should_receive(:info).with(/RECV/).never
279
+ @logger.should_receive(:error).with(/Failed subscribing/).once
280
+ @exceptions.should_receive(:track).once
281
+ @bind.should_receive(:subscribe).and_raise(Exception)
282
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
238
283
  broker.__send__(:update_status, :ready)
284
+ result = broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}) {|_, _|}
285
+ result.should be_false
286
+ end
287
+
288
+ end # when subscribing
289
+
290
+ context "when receiving" do
291
+
292
+ before(:each) do
293
+ @header = flexmock("header", :ack => true).by_default
294
+ @subscribe_options = {}
295
+ end
296
+
297
+ it "should unserialize the message by default" do
298
+ @logger.should_receive(:info).with(/Connecting/).once
299
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
300
+ flexmock(broker).should_receive(:unserialize).and_return(@packet).once
301
+ broker.__send__(:receive, "queue", @header, @message, @subscribe_options) {|_, _|}
302
+ end
303
+
304
+ it "should not unserialize the message if so requested" do
305
+ @logger.should_receive(:info).with(/Connecting/).once
306
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
307
+ flexmock(broker).should_receive(:unserialize).never
308
+ @subscribe_options = {:no_unserialize => true}
309
+ broker.__send__(:receive, "queue", @header, @message, @subscribe_options) {|_, _|}
310
+ end
311
+
312
+ it "should make callback to deliver message" do
313
+ @logger.should_receive(:info).with(/Connecting/).once
314
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
315
+ flexmock(broker).should_receive(:unserialize).and_return(@packet).once
316
+ called = 0
317
+ broker.__send__(:receive, "queue", @header, @message, @subscribe_options) { |b, m| called += 1 }
318
+ called.should == 1
319
+ end
320
+
321
+ it "should not make callback if received message is nil but then ack if option set" do
322
+ @logger.should_receive(:info).with(/Connecting/).once
323
+ @header.should_receive(:ack).once
324
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
325
+ flexmock(broker).should_receive(:unserialize).and_return(nil).once
326
+ @subscribe_options = {:ack => true}
239
327
  called = 0
240
- broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}, :ack => true) { |b, m| called += 1 }
328
+ broker.__send__(:receive, "queue", @header, @message, @subscribe_options) { |b, m| called += 1 }
241
329
  called.should == 0
242
330
  end
243
331
 
244
- it "should ignore 'nil' message when not using ack" do
245
- @logger.should_receive(:level).and_return(:debug)
332
+ it "should ignore 'nil' message but then ack if option set" do
246
333
  @logger.should_receive(:info).with(/Connecting/).once
247
- @logger.should_receive(:info).with(/Subscribing/).once
248
334
  @logger.should_receive(:debug).with(/nil message ignored/).once
249
- @bind.should_receive(:subscribe).and_yield(@info, "nil").once
250
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
251
- broker.__send__(:update_status, :ready)
335
+ @header.should_receive(:ack).once
336
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
337
+ @subscribe_options = {:ack => true}
252
338
  called = 0
253
- broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}) { |b, m| called += 1 }
339
+ broker.__send__(:receive, "queue", @header, "nil", @subscribe_options) { |b, m| called += 1 }
254
340
  called.should == 0
255
341
  end
256
342
 
257
- it "should not unserialize the message if requested" do
343
+ it "should ignore 'nil' message when ack option not set" do
258
344
  @logger.should_receive(:info).with(/Connecting/).once
259
- @logger.should_receive(:info).with(/Subscribing/).once
260
- @logger.should_receive(:info).with(/^RECV/).never
261
- @bind.should_receive(:subscribe).and_yield(@info, @message).once
262
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
263
- broker.__send__(:update_status, :ready)
264
- broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}, :no_unserialize => true) do |b, m|
265
- b.should == "rs-broker-localhost-5672"
266
- m.should == @message
267
- end
345
+ @logger.should_receive(:debug).with(/nil message ignored/).once
346
+ @header.should_receive(:ack).never
347
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
348
+ broker.__send__(:receive, "queue", @header, "nil", @subscribe_options) {|_, _|}
268
349
  end
269
350
 
270
351
  it "should pass header with message if callback requires it" do
271
352
  @logger.should_receive(:info).with(/Connecting/).once
272
- @logger.should_receive(:info).with(/Subscribing/).once
273
- @logger.should_receive(:info).with(/^RECV/).never
274
- @bind.should_receive(:subscribe).and_yield(@info, @message).once
275
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
276
- broker.__send__(:update_status, :ready)
277
- broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}, :no_unserialize => true) do |b, m, h|
353
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
354
+ flexmock(broker).should_receive(:unserialize).never
355
+ @subscribe_options = {:no_unserialize => true}
356
+ broker.__send__(:receive, "queue", @header, @message, @subscribe_options) do |b, m, h|
278
357
  b.should == "rs-broker-localhost-5672"
279
358
  m.should == @message
280
- h.should == @info
359
+ h.should == @header
281
360
  end
282
361
  end
283
362
 
284
- it "should log an error if a subscribe fails" do
285
- @logger.should_receive(:info).with(/Connecting/).once
286
- @logger.should_receive(:info).with(/RECV/).never
287
- @logger.should_receive(:error).with(/Failed subscribing/).once
288
- @exceptions.should_receive(:track).once
289
- @bind.should_receive(:subscribe).and_raise(Exception)
290
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
291
- broker.__send__(:update_status, :ready)
292
- result = broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}) {|b, p|}
293
- result.should be_false
294
- end
295
-
296
- end # when subscribing
363
+ end # when receiving
297
364
 
298
- context "when receiving" do
365
+ context "when unserializing" do
299
366
 
300
367
  it "should unserialize the message, log it, and return it" do
301
368
  @logger.should_receive(:info).with(/Connecting/).once
302
369
  @logger.should_receive(:info).with(/^RECV/).once
303
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
304
- broker.__send__(:receive, "queue", @message, RequestMock => nil).should == @packet
370
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
371
+ broker.__send__(:unserialize, "queue", @message, RequestMock => nil).should == @packet
305
372
  end
306
373
 
307
- it "should log a warning if the message is not of the right type and return nil" do
308
- @logger.should_receive(:warning).with(/Received invalid.*packet type/).once
309
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
310
- broker.__send__(:receive, "queue", @message).should be_nil
374
+ it "should log an error if the message is not of the right type and return nil" do
375
+ @logger.should_receive(:error).with(/Received invalid.*packet type/).once
376
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
377
+ broker.__send__(:unserialize, "queue", @message).should be_nil
311
378
  end
312
379
 
313
- it "should show the category in the warning message if specified" do
314
- @logger.should_receive(:warning).with(/Received invalid xxxx packet type/).once
315
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
316
- broker.__send__(:receive, "queue", @message, ResultMock => nil, :category => "xxxx")
380
+ it "should show the category in the error message if specified" do
381
+ @logger.should_receive(:error).with(/Received invalid xxxx packet type/).once
382
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
383
+ broker.__send__(:unserialize, "queue", @message, ResultMock => nil, :category => "xxxx")
317
384
  end
318
385
 
319
386
  it "should display broker alias in the log" do
320
387
  @logger.should_receive(:info).with(/Connecting/).once
321
388
  @logger.should_receive(:info).with(/^RECV b0 /).once
322
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
323
- broker.__send__(:receive, "queue", @message, RequestMock => nil)
389
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
390
+ broker.__send__(:unserialize, "queue", @message, RequestMock => nil)
324
391
  end
325
392
 
326
393
  it "should filter the packet display for :info level" do
@@ -328,8 +395,8 @@ describe RightAMQP::BrokerClient do
328
395
  @logger.should_receive(:info).with(/^RECV.*TO YOU/).once
329
396
  @logger.should_receive(:debug).with(/^RECV.*TO YOU/).never
330
397
  @packet.should_receive(:to_s).with([:to], :recv_version).and_return("TO YOU").once
331
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
332
- broker.__send__(:receive, "queue", @message, RequestMock => [:to])
398
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
399
+ broker.__send__(:unserialize, "queue", @message, RequestMock => [:to])
333
400
  end
334
401
 
335
402
  it "should not filter the packet display for :debug level" do
@@ -338,49 +405,51 @@ describe RightAMQP::BrokerClient do
338
405
  @logger.should_receive(:info).with(/^RECV.*ALL/).never
339
406
  @logger.should_receive(:info).with(/^RECV.*ALL/).once
340
407
  @packet.should_receive(:to_s).with(nil, :recv_version).and_return("ALL").once
341
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
342
- broker.__send__(:receive, "queue", @message, RequestMock => [:to])
408
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
409
+ broker.__send__(:unserialize, "queue", @message, RequestMock => [:to])
343
410
  end
344
411
 
345
412
  it "should display additional data in log" do
346
413
  @logger.should_receive(:info).with(/Connecting/).once
347
414
  @logger.should_receive(:info).with(/^RECV.*More data/).once
348
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
349
- broker.__send__(:receive, "queue", @message, RequestMock => nil, :log_data => "More data")
415
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
416
+ broker.__send__(:unserialize, "queue", @message, RequestMock => nil, :log_data => "More data")
350
417
  end
351
418
 
352
419
  it "should not log a message if requested not to" do
353
420
  @logger.should_receive(:info).with(/Connecting/).once
354
421
  @logger.should_receive(:info).with(/^RECV/).never
355
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
356
- broker.__send__(:receive, "queue", @message, RequestMock => nil, :no_log => true)
422
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
423
+ broker.__send__(:unserialize, "queue", @message, RequestMock => nil, :no_log => true)
357
424
  end
358
425
 
359
426
  it "should not log a message if requested not to unless debug level" do
360
427
  @logger.should_receive(:level).and_return(:debug)
361
428
  @logger.should_receive(:info).with(/Connecting/).once
362
429
  @logger.should_receive(:info).with(/^RECV/).once
363
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
364
- broker.__send__(:receive, "queue", @message, RequestMock => nil, :no_log => true)
430
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
431
+ broker.__send__(:unserialize, "queue", @message, RequestMock => nil, :no_log => true)
365
432
  end
366
433
 
367
434
  it "should log an error if exception prevents normal logging and should then return nil" do
368
- @logger.should_receive(:error).with(/Failed receiving from queue/).once
435
+ @logger.should_receive(:error).with(/Failed unserializing message from queue/).once
369
436
  @serializer.should_receive(:load).with(@message).and_raise(Exception).once
370
437
  @exceptions.should_receive(:track).once
371
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
372
- broker.__send__(:receive, "queue", @message).should be_nil
438
+ @non_deliveries.should_receive(:update).once
439
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
440
+ broker.__send__(:unserialize, "queue", @message).should be_nil
373
441
  end
374
442
 
375
443
  it "should make callback when there is a receive failure" do
376
- @logger.should_receive(:error).with(/Failed receiving from queue/).once
444
+ @logger.should_receive(:error).with(/Failed unserializing message from queue/).once
377
445
  @serializer.should_receive(:load).with(@message).and_raise(Exception).once
378
446
  @exceptions.should_receive(:track).once
447
+ @non_deliveries.should_receive(:update).once
379
448
  called = 0
380
449
  callback = lambda { |msg, e| called += 1 }
381
450
  options = {:exception_on_receive_callback => callback}
382
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, options)
383
- broker.__send__(:receive, "queue", @message).should be_nil
451
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, options)
452
+ broker.__send__(:unserialize, "queue", @message).should be_nil
384
453
  called.should == 1
385
454
  end
386
455
 
@@ -388,11 +457,11 @@ describe RightAMQP::BrokerClient do
388
457
  @logger.should_receive(:info).with(/Connecting/).once
389
458
  @logger.should_receive(:info).with(/^RE-RECV/).once
390
459
  @packet.should_receive(:tries).and_return(["try1"]).once
391
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
392
- broker.__send__(:receive, "queue", @message, RequestMock => nil).should == @packet
460
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
461
+ broker.__send__(:unserialize, "queue", @message, RequestMock => nil).should == @packet
393
462
  end
394
463
 
395
- end # when receiving
464
+ end # when unserializing
396
465
 
397
466
  context "when unsubscribing" do
398
467
 
@@ -407,22 +476,22 @@ describe RightAMQP::BrokerClient do
407
476
 
408
477
  it "should unsubscribe a queue by name" do
409
478
  @queue.should_receive(:unsubscribe).once
410
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
411
- broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"})
479
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
480
+ broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"}) {|_, _|}
412
481
  broker.unsubscribe(["queue1"])
413
482
  end
414
483
 
415
484
  it "should ignore unsubscribe if queue unknown" do
416
485
  @queue.should_receive(:unsubscribe).never
417
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
418
- broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"})
486
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
487
+ broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"}) {|_, _|}
419
488
  broker.unsubscribe(["queue2"])
420
489
  end
421
490
 
422
491
  it "should activate block after unsubscribing if provided" do
423
492
  @queue.should_receive(:unsubscribe).and_yield.once
424
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
425
- broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"})
493
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
494
+ broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"}) {|_, _|}
426
495
  called = 0
427
496
  broker.unsubscribe(["queue1"]) { called += 1 }
428
497
  called.should == 1
@@ -430,8 +499,8 @@ describe RightAMQP::BrokerClient do
430
499
 
431
500
  it "should ignore the request if client not usable" do
432
501
  @queue.should_receive(:unsubscribe).and_yield.never
433
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
434
- broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"})
502
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
503
+ broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"}) {|_, _|}
435
504
  broker.__send__(:update_status, :disconnected)
436
505
  broker.unsubscribe(["queue1"])
437
506
  end
@@ -440,8 +509,8 @@ describe RightAMQP::BrokerClient do
440
509
  @logger.should_receive(:error).with(/Failed unsubscribing/).once
441
510
  @queue.should_receive(:unsubscribe).and_raise(Exception).once
442
511
  @exceptions.should_receive(:track).once
443
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
444
- broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"})
512
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
513
+ broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"}) {|_, _|}
445
514
  called = 0
446
515
  broker.unsubscribe(["queue1"]) { called += 1 }
447
516
  called.should == 1
@@ -459,13 +528,13 @@ describe RightAMQP::BrokerClient do
459
528
 
460
529
  it "should declare exchange and return true" do
461
530
  @channel.should_receive(:exchange).once
462
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
531
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
463
532
  broker.declare(:exchange, "x", :durable => true).should be_true
464
533
  end
465
534
 
466
535
  it "should delete the exchange or queue from the AMQP cache before declaring" do
467
536
  @channel.should_receive(:queue).once
468
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
537
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
469
538
  flexmock(broker).should_receive(:delete_amqp_resources).with(:queue, "queue").once
470
539
  broker.declare(:queue, "queue", :durable => true).should be_true
471
540
  end
@@ -474,13 +543,13 @@ describe RightAMQP::BrokerClient do
474
543
  @logger.should_receive(:info).with(/Connecting/).once
475
544
  @logger.should_receive(:info).with(/Declaring/).once
476
545
  @channel.should_receive(:queue).once
477
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
546
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
478
547
  broker.declare(:queue, "q").should be_true
479
548
  end
480
549
 
481
550
  it "should return false if client not usable" do
482
551
  @channel.should_receive(:exchange).never
483
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
552
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
484
553
  broker.__send__(:update_status, :disconnected)
485
554
  broker.declare(:exchange, "x", :durable => true).should be_false
486
555
 
@@ -492,7 +561,7 @@ describe RightAMQP::BrokerClient do
492
561
  @logger.should_receive(:error).with(/Failed declaring/).once
493
562
  @exceptions.should_receive(:track).once
494
563
  @channel.should_receive(:queue).and_raise(Exception).once
495
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
564
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
496
565
  broker.declare(:queue, "q").should be_false
497
566
  end
498
567
 
@@ -508,7 +577,7 @@ describe RightAMQP::BrokerClient do
508
577
  it "should serialize message, publish it, and return true" do
509
578
  @channel.should_receive(:direct).with("exchange", :durable => true).and_return(@direct).once
510
579
  @direct.should_receive(:publish).with(@message, :persistent => true).once
511
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
580
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
512
581
  broker.__send__(:update_status, :ready)
513
582
  broker.publish({:type => :direct, :name => "exchange", :options => {:durable => true}},
514
583
  @packet, @message, :persistent => true).should be_true
@@ -517,7 +586,7 @@ describe RightAMQP::BrokerClient do
517
586
  it "should delete the exchange or queue from the AMQP cache if :declare specified" do
518
587
  @channel.should_receive(:direct).with("exchange", {:declare => true}).and_return(@direct)
519
588
  @direct.should_receive(:publish).with(@message, {})
520
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
589
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
521
590
  broker.__send__(:update_status, :ready)
522
591
  exchange = {:type => :direct, :name => "exchange", :options => {:declare => true}}
523
592
  flexmock(broker).should_receive(:delete_amqp_resources).with(:direct, "exchange").once
@@ -527,7 +596,7 @@ describe RightAMQP::BrokerClient do
527
596
  it "should return false if client not connected" do
528
597
  @channel.should_receive(:direct).never
529
598
  @direct.should_receive(:publish).with(@message, :persistent => true).never
530
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
599
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
531
600
  broker.publish({:type => :direct, :name => "exchange", :options => {:durable => true}},
532
601
  @packet, @message, :persistent => true).should be_false
533
602
  end
@@ -535,9 +604,10 @@ describe RightAMQP::BrokerClient do
535
604
  it "should log an error if the publish fails" do
536
605
  @logger.should_receive(:error).with(/Failed publishing/).once
537
606
  @exceptions.should_receive(:track).once
607
+ @non_deliveries.should_receive(:update).once
538
608
  @channel.should_receive(:direct).and_raise(Exception)
539
609
  @direct.should_receive(:publish).with(@message, {}).never
540
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
610
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
541
611
  broker.__send__(:update_status, :ready)
542
612
  broker.publish({:type => :direct, :name => "exchange"}, @packet, @message).should be_false
543
613
  end
@@ -547,7 +617,7 @@ describe RightAMQP::BrokerClient do
547
617
  @logger.should_receive(:info).with(/^SEND b0/).once
548
618
  @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
549
619
  @direct.should_receive(:publish).with(@message, {}).once
550
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
620
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
551
621
  broker.__send__(:update_status, :ready)
552
622
  broker.publish({:type => :direct, :name => "exchange"}, @packet, @message).should be_true
553
623
  end
@@ -558,7 +628,7 @@ describe RightAMQP::BrokerClient do
558
628
  @logger.should_receive(:info).with(/^SEND b0.*publish options/).once
559
629
  @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
560
630
  @direct.should_receive(:publish).with(@message, {}).once
561
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
631
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
562
632
  broker.__send__(:update_status, :ready)
563
633
  broker.publish({:type => :direct, :name => "exchange"}, @packet, @message).should be_true
564
634
  end
@@ -568,7 +638,7 @@ describe RightAMQP::BrokerClient do
568
638
  @logger.should_receive(:info).with(/^SEND/).never
569
639
  @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
570
640
  @direct.should_receive(:publish).with(@message, :no_log => true).once
571
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
641
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
572
642
  broker.__send__(:update_status, :ready)
573
643
  broker.publish({:type => :direct, :name => "exchange"}, @packet, @message, :no_log => true).should be_true
574
644
  end
@@ -579,7 +649,7 @@ describe RightAMQP::BrokerClient do
579
649
  @logger.should_receive(:info).with(/^SEND/).once
580
650
  @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
581
651
  @direct.should_receive(:publish).with(@message, :no_log => true).once
582
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
652
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
583
653
  broker.__send__(:update_status, :ready)
584
654
  broker.publish({:type => :direct, :name => "exchange"}, @packet, @message, :no_log => true).should be_true
585
655
  end
@@ -589,7 +659,7 @@ describe RightAMQP::BrokerClient do
589
659
  @logger.should_receive(:info).with(/^SEND b0 /).once
590
660
  @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
591
661
  @direct.should_receive(:publish).with(@message, {}).once
592
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
662
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
593
663
  broker.__send__(:update_status, :ready)
594
664
  broker.publish({:type => :direct, :name => "exchange"}, @packet, @message).should be_true
595
665
  end
@@ -601,7 +671,7 @@ describe RightAMQP::BrokerClient do
601
671
  @packet.should_receive(:to_s).with([:to], :send_version).and_return("TO YOU").once
602
672
  @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
603
673
  @direct.should_receive(:publish).with(@message, :log_filter => [:to]).once
604
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
674
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
605
675
  broker.__send__(:update_status, :ready)
606
676
  broker.publish({:type => :direct, :name => "exchange"}, @packet, @message, :log_filter => [:to]).should be_true
607
677
  end
@@ -614,7 +684,7 @@ describe RightAMQP::BrokerClient do
614
684
  @packet.should_receive(:to_s).with(nil, :send_version).and_return("ALL").once
615
685
  @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
616
686
  @direct.should_receive(:publish).with(@message, :log_filter => [:to]).once
617
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
687
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
618
688
  broker.__send__(:update_status, :ready)
619
689
  broker.publish({:type => :direct, :name => "exchange"}, @packet, @message, :log_filter => [:to]).should be_true
620
690
  end
@@ -624,7 +694,7 @@ describe RightAMQP::BrokerClient do
624
694
  @logger.should_receive(:info).with(/^SEND.*More data/).once
625
695
  @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
626
696
  @direct.should_receive(:publish).with(@message, :log_data => "More data").once
627
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
697
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
628
698
  broker.__send__(:update_status, :ready)
629
699
  broker.publish({:type => :direct, :name => "exchange"}, @packet, @message, :log_data => "More data").should be_true
630
700
  end
@@ -635,7 +705,7 @@ describe RightAMQP::BrokerClient do
635
705
  @packet.should_receive(:tries).and_return(["try1"]).once
636
706
  @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
637
707
  @direct.should_receive(:publish).with(@message, {}).once
638
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
708
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
639
709
  broker.__send__(:update_status, :ready)
640
710
  broker.publish({:type => :direct, :name => "exchange"}, @packet, @message).should be_true
641
711
  end
@@ -644,64 +714,56 @@ describe RightAMQP::BrokerClient do
644
714
 
645
715
  context "when returning" do
646
716
 
647
- class MQ
648
- attr_accessor :connection, :on_return_message
649
-
650
- def initialize(connection)
651
- @connection = connection
652
- end
653
-
654
- def return_message(&blk)
655
- @on_return_message = blk
656
- end
657
- end
658
-
659
717
  before(:each) do
660
- @info = flexmock("info", :reply_text => "NO_CONSUMERS", :exchange => "exchange", :routing_key => "routing_key").by_default
718
+ @header = flexmock("header", :reply_text => "NO_CONSUMERS", :exchange => "exchange", :routing_key => "routing_key").by_default
661
719
  end
662
720
 
663
- it "should invoke block and log the return" do
721
+ it "should make callback" do
664
722
  @logger.should_receive(:info).with(/Connecting to broker/).once
665
- @logger.should_receive(:debug).with(/RETURN b0/).once
666
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
723
+ @logger.should_receive(:debug).with(/RETURN b0 for exchange because NO_CONSUMERS/).once
667
724
  called = 0
668
- broker.return_message do |to, reason, message|
725
+ callback = lambda do |identity, to, reason, message|
669
726
  called += 1
727
+ identity.should == @identity
670
728
  to.should == "exchange"
671
729
  reason.should == "NO_CONSUMERS"
672
730
  message.should == @message
673
731
  end
674
- broker.instance_variable_get(:@channel).on_return_message.call(@info, @message)
732
+ @options = {:return_message_callback => callback }
733
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
734
+ broker.__send__(:handle_return, @header, @message).should be_true
675
735
  called.should == 1
676
736
  end
677
737
 
678
- it "should invoke block with routing key if exchange is empty" do
679
- @logger.should_receive(:debug)
680
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
681
- called = 0
682
- broker.return_message do |to, reason, message|
683
- called += 1
684
- to.should == "routing_key"
685
- reason.should == "NO_CONSUMERS"
686
- message.should == @message
687
- end
688
- @info.should_receive(:exchange).and_return("")
689
- broker.instance_variable_get(:@channel).on_return_message.call(@info, @message)
690
- called.should == 1
738
+ it "should log the return as info if there is no callback" do
739
+ @logger.should_receive(:info).with(/Connecting to broker/)
740
+ @logger.should_receive(:info).with("RETURN b0 for exchange because NO_CONSUMERS").once
741
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
742
+ broker.__send__(:handle_return, @header, @message).should be_true
743
+ end
744
+
745
+ it "should log the return as debug if there is a callback" do
746
+ @logger.should_receive(:debug).with("RETURN b0 for exchange because NO_CONSUMERS").once
747
+ @options = {:return_message_callback => lambda { |i, t, r, m| } }
748
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
749
+ broker.__send__(:handle_return, @header, @message).should be_true
750
+ end
751
+
752
+ it "should log the return with routing key if exchange is empty" do
753
+ @logger.should_receive(:info).with(/Connecting to broker/)
754
+ @logger.should_receive(:info).with("RETURN b0 for routing_key because NO_CONSUMERS").once
755
+ @header.should_receive(:exchange).and_return("")
756
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
757
+ broker.__send__(:handle_return, @header, @message).should be_true
691
758
  end
692
759
 
693
760
  it "should log an error if there is a failure while processing the return" do
761
+ @logger.should_receive(:debug).with("RETURN b0 for exchange because NO_CONSUMERS")
694
762
  @logger.should_receive(:error).with(/Failed return/).once
695
- @logger.should_receive(:debug)
696
763
  @exceptions.should_receive(:track).once
697
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
698
- called = 0
699
- broker.return_message do |to, reason, message|
700
- called += 1
701
- raise Exception
702
- end
703
- broker.instance_variable_get(:@channel).on_return_message.call(@info, @message)
704
- called.should == 1
764
+ @options = {:return_message_callback => lambda { |i, t, r, m| raise Exception } }
765
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
766
+ broker.__send__(:handle_return, @header, @message).should be_true
705
767
  end
706
768
 
707
769
  end # when returning
@@ -719,8 +781,8 @@ describe RightAMQP::BrokerClient do
719
781
 
720
782
  it "should delete the named queue and return true" do
721
783
  @queue.should_receive(:delete).once
722
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
723
- broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"})
784
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
785
+ broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"}) {|_, _|}
724
786
  broker.queues.should == [@queue]
725
787
  broker.delete("queue1").should be_true
726
788
  broker.queues.should == []
@@ -728,8 +790,8 @@ describe RightAMQP::BrokerClient do
728
790
 
729
791
  it "should return false if the client is not usable" do
730
792
  @queue.should_receive(:delete).never
731
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
732
- broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"})
793
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
794
+ broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"}) {|_, _|}
733
795
  broker.queues.should == [@queue]
734
796
  broker.__send__(:update_status, :disconnected)
735
797
  broker.delete("queue1").should be_false
@@ -740,8 +802,8 @@ describe RightAMQP::BrokerClient do
740
802
  @logger.should_receive(:error).with(/Failed deleting queue/).once
741
803
  @exceptions.should_receive(:track).once
742
804
  @queue.should_receive(:delete).and_raise(Exception)
743
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
744
- broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"})
805
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
806
+ broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"}) {|_, _|}
745
807
  broker.queues.should == [@queue]
746
808
  broker.delete("queue1").should be_false
747
809
  end
@@ -755,7 +817,7 @@ describe RightAMQP::BrokerClient do
755
817
  end
756
818
 
757
819
  it "should distinguish whether the client is usable based on whether connecting or connected" do
758
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
820
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
759
821
  broker.usable?.should be_true
760
822
  broker.__send__(:update_status, :ready)
761
823
  broker.usable?.should be_true
@@ -767,7 +829,7 @@ describe RightAMQP::BrokerClient do
767
829
  end
768
830
 
769
831
  it "should distinguish whether the client is connected" do
770
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
832
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
771
833
  broker.connected?.should be_false
772
834
  broker.__send__(:update_status, :ready)
773
835
  broker.connected?.should be_true
@@ -779,7 +841,7 @@ describe RightAMQP::BrokerClient do
779
841
  end
780
842
 
781
843
  it "should distinguish whether the client has failed" do
782
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
844
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
783
845
  broker.failed?.should be_false
784
846
  broker.__send__(:update_status, :ready)
785
847
  broker.failed?.should be_false
@@ -791,7 +853,7 @@ describe RightAMQP::BrokerClient do
791
853
  end
792
854
 
793
855
  it "should give broker summary" do
794
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
856
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
795
857
  broker.summary.should == {:alias => "b0", :identity => @identity, :status => :connecting,
796
858
  :disconnects => 0, :failures => 0, :retries => 0}
797
859
  broker.__send__(:update_status, :ready)
@@ -804,7 +866,7 @@ describe RightAMQP::BrokerClient do
804
866
  end
805
867
 
806
868
  it "should give broker statistics" do
807
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
869
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
808
870
  broker.stats.should == {"alias" => "b0", "identity" => "rs-broker-localhost-5672",
809
871
  "status" => "connecting", "disconnects" => nil, "disconnect last" => nil,
810
872
  "failures" => nil, "failure last" => nil, "retries" => nil}
@@ -825,17 +887,17 @@ describe RightAMQP::BrokerClient do
825
887
  connected_before = false
826
888
  callback = lambda { |b, c| called += 1; b.should == broker; c.should == connected_before }
827
889
  options = {:update_status_callback => callback}
828
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, options)
890
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, options)
829
891
  broker.__send__(:update_status, :ready)
830
892
  broker.last_failed.should be_false
831
893
  called.should == 1
832
894
  connected_before = true
833
895
  broker.__send__(:update_status, :disconnected)
834
896
  broker.last_failed.should be_false
835
- broker.disconnects.total.should == 1
897
+ broker.disconnect_stats.total.should == 1
836
898
  called.should == 2
837
899
  broker.__send__(:update_status, :disconnected)
838
- broker.disconnects.total.should == 1
900
+ broker.disconnect_stats.total.should == 1
839
901
  called.should == 2
840
902
  @logger.should_receive(:error).with(/Failed to connect to broker b0/).once
841
903
  connected_before = false
@@ -854,7 +916,7 @@ describe RightAMQP::BrokerClient do
854
916
 
855
917
  it "should close broker connection and send status update" do
856
918
  @connection.should_receive(:close).and_yield.once
857
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
919
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
858
920
  flexmock(broker).should_receive(:update_status).once
859
921
  broker.close
860
922
  broker.status.should == :closed
@@ -862,14 +924,14 @@ describe RightAMQP::BrokerClient do
862
924
 
863
925
  it "should not propagate status update if requested not to" do
864
926
  @connection.should_receive(:close).and_yield.once
865
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
927
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
866
928
  flexmock(broker).should_receive(:update_status).never
867
929
  broker.close(propagate = false)
868
930
  end
869
931
 
870
932
  it "should set status to :failed if not a normal close" do
871
933
  @connection.should_receive(:close).and_yield.once
872
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
934
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
873
935
  broker.close(propagate = false, normal = false)
874
936
  broker.status.should == :failed
875
937
  end
@@ -878,7 +940,7 @@ describe RightAMQP::BrokerClient do
878
940
  @logger.should_receive(:info).with(/Connecting/).once
879
941
  @logger.should_receive(:info).with(/Closed connection to broker b0/).once
880
942
  @connection.should_receive(:close).and_yield.once
881
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
943
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
882
944
  broker.close
883
945
  end
884
946
 
@@ -886,13 +948,13 @@ describe RightAMQP::BrokerClient do
886
948
  @logger.should_receive(:info).with(/Connecting/).once
887
949
  @logger.should_receive(:info).with(/Closed connection to broker b0/).never
888
950
  @connection.should_receive(:close).and_yield.once
889
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
951
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
890
952
  broker.close(propagate = true, normal = true, log = false)
891
953
  end
892
954
 
893
955
  it "should close broker connection and execute block if supplied" do
894
956
  @connection.should_receive(:close).and_yield.once
895
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
957
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
896
958
  called = 0
897
959
  broker.close { called += 1; broker.status.should == :closed }
898
960
  called.should == 1
@@ -900,13 +962,13 @@ describe RightAMQP::BrokerClient do
900
962
 
901
963
  it "should close broker connection when no block supplied" do
902
964
  @connection.should_receive(:close).and_yield.once
903
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
965
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
904
966
  broker.close
905
967
  end
906
968
 
907
969
  it "should not propagate status update if already closed" do
908
970
  @connection.should_receive(:close).never
909
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
971
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
910
972
  broker.__send__(:update_status, :closed)
911
973
  flexmock(broker).should_receive(:update_status).never
912
974
  broker.close
@@ -915,7 +977,7 @@ describe RightAMQP::BrokerClient do
915
977
  it "should change failed status to closed" do
916
978
  @logger.should_receive(:error).with(/Failed to connect to broker/).once
917
979
  @connection.should_receive(:close).never
918
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
980
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
919
981
  broker.__send__(:update_status, :failed)
920
982
  flexmock(broker).should_receive(:update_status).never
921
983
  broker.close
@@ -926,7 +988,7 @@ describe RightAMQP::BrokerClient do
926
988
  @logger.should_receive(:error).with(/Failed to close broker b0/).once
927
989
  @exceptions.should_receive(:track).once
928
990
  @connection.should_receive(:close).and_raise(Exception)
929
- broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
991
+ broker = RightAMQP::BrokerClient.new(@identity, @address, @serializer, @exceptions, @non_deliveries, @options)
930
992
  broker.close
931
993
  broker.status.should == :closed
932
994
  end