right_agent 0.6.6 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/lib/right_agent/agent.rb +26 -25
  2. data/lib/right_agent/agent_config.rb +28 -2
  3. data/lib/right_agent/command/command_constants.rb +2 -2
  4. data/lib/right_agent/core_payload_types/executable_bundle.rb +3 -21
  5. data/lib/right_agent/core_payload_types/login_user.rb +19 -4
  6. data/lib/right_agent/core_payload_types/recipe_instantiation.rb +7 -1
  7. data/lib/right_agent/core_payload_types/right_script_instantiation.rb +7 -1
  8. data/lib/right_agent/dispatcher.rb +6 -19
  9. data/lib/right_agent/idempotent_request.rb +72 -17
  10. data/lib/right_agent/monkey_patches/ruby_patch.rb +0 -1
  11. data/lib/right_agent/monkey_patches.rb +0 -1
  12. data/lib/right_agent/operation_result.rb +27 -4
  13. data/lib/right_agent/packets.rb +47 -23
  14. data/lib/right_agent/platform/darwin.rb +33 -2
  15. data/lib/right_agent/platform/linux.rb +98 -2
  16. data/lib/right_agent/platform/windows.rb +41 -6
  17. data/lib/right_agent/platform.rb +11 -2
  18. data/lib/right_agent/scripts/agent_controller.rb +2 -1
  19. data/lib/right_agent/scripts/agent_deployer.rb +2 -2
  20. data/lib/right_agent/scripts/stats_manager.rb +7 -3
  21. data/lib/right_agent/sender.rb +45 -28
  22. data/lib/right_agent.rb +2 -5
  23. data/right_agent.gemspec +5 -3
  24. data/spec/agent_config_spec.rb +1 -1
  25. data/spec/agent_spec.rb +26 -20
  26. data/spec/core_payload_types/login_user_spec.rb +7 -3
  27. data/spec/idempotent_request_spec.rb +218 -48
  28. data/spec/operation_result_spec.rb +19 -0
  29. data/spec/packets_spec.rb +42 -1
  30. data/spec/platform/darwin.rb +11 -0
  31. data/spec/platform/linux.rb +23 -0
  32. data/spec/platform/linux_volume_manager_spec.rb +43 -43
  33. data/spec/platform/platform_spec.rb +35 -32
  34. data/spec/platform/windows.rb +11 -0
  35. data/spec/sender_spec.rb +21 -25
  36. metadata +47 -40
  37. data/lib/right_agent/broker_client.rb +0 -686
  38. data/lib/right_agent/ha_broker_client.rb +0 -1327
  39. data/lib/right_agent/monkey_patches/amqp_patch.rb +0 -274
  40. data/lib/right_agent/monkey_patches/ruby_patch/string_patch.rb +0 -107
  41. data/lib/right_agent/stats_helper.rb +0 -745
  42. data/spec/broker_client_spec.rb +0 -962
  43. data/spec/ha_broker_client_spec.rb +0 -1695
  44. data/spec/monkey_patches/amqp_patch_spec.rb +0 -100
  45. data/spec/monkey_patches/string_patch_spec.rb +0 -99
  46. data/spec/stats_helper_spec.rb +0 -686
@@ -1,962 +0,0 @@
1
- #
2
- # Copyright (c) 2009-2011 RightScale Inc
3
- #
4
- # Permission is hereby granted, free of charge, to any person obtaining
5
- # a copy of this software and associated documentation files (the
6
- # "Software"), to deal in the Software without restriction, including
7
- # without limitation the rights to use, copy, modify, merge, publish,
8
- # distribute, sublicense, and/or sell copies of the Software, and to
9
- # permit persons to whom the Software is furnished to do so, subject to
10
- # the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be
13
- # included in all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
-
23
- require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
24
-
25
- describe RightScale::BrokerClient do
26
-
27
- include FlexMock::ArgumentTypes
28
-
29
- before(:each) do
30
- @log = flexmock(RightScale::Log)
31
- @log.should_receive(:error).by_default.and_return { |m| raise RightScale::Log.format(*m) }
32
- @log.should_receive(:warning).by_default.and_return { |m| raise RightScale::Log.format(*m) }
33
- @log.should_receive(:info).by_default
34
- @serializer = flexmock("serializer")
35
- @exceptions = flexmock("exception_stats")
36
- @exceptions.should_receive(:track).never.by_default
37
- @connection = flexmock("connection")
38
- @connection.should_receive(:connection_status).by_default
39
- flexmock(AMQP).should_receive(:connect).and_return(@connection).by_default
40
- @channel = flexmock("AMQP connection channel")
41
- @channel.should_receive(:connection).and_return(@connection).by_default
42
- @identity = "rs-broker-localhost-5672"
43
- @address = {:host => "localhost", :port => 5672, :index => 0}
44
- @options = {}
45
- end
46
-
47
- context "when initializing connection" do
48
-
49
- before(:each) do
50
- @amqp = flexmock(AMQP)
51
- @amqp.should_receive(:connect).and_return(@connection).by_default
52
- @channel.should_receive(:prefetch).never.by_default
53
- flexmock(MQ).should_receive(:new).with(@connection).and_return(@channel).by_default
54
- @island = flexmock("island", :id => 2, :broker_hosts => "local_host").by_default
55
- end
56
-
57
- it "should create a broker with AMQP connection for specified address" do
58
- @amqp.should_receive(:connect).with(hsh(:user => "user", :pass => "pass", :vhost => "vhost", :host => "localhost",
59
- :port => 5672, :insist => true, :reconnect_interval => 10)).and_return(@connection).once
60
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, {:user => "user",
61
- :pass => "pass", :vhost => "vhost", :insist => true,
62
- :reconnect_interval => 10})
63
- broker.host.should == "localhost"
64
- broker.port.should == 5672
65
- broker.index.should == 0
66
- broker.queues.should == []
67
- broker.island_id.should be_nil
68
- broker.island_alias.should == ""
69
- broker.in_home_island.should be_true
70
- broker.summary.should == {:alias => "b0", :identity => @identity, :status => :connecting,
71
- :disconnects => 0, :failures => 0, :retries => 0}
72
- broker.usable?.should be_true
73
- broker.connected?.should be_false
74
- broker.failed?.should be_false
75
- end
76
-
77
- it "should recognize the home island" do
78
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions,
79
- {:home_island => 2}, @island)
80
- broker.host.should == "localhost"
81
- broker.port.should == 5672
82
- broker.index.should == 0
83
- broker.queues.should == []
84
- broker.island_id.should == 2
85
- broker.island_alias.should == "i2"
86
- broker.in_home_island.should be_true
87
- broker.summary.should == {:alias => "b0", :identity => @identity, :status => :connecting,
88
- :disconnects => 0, :failures => 0, :retries => 0}
89
- end
90
-
91
- it "should use island information when not home island" do
92
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions,
93
- {:home_island => 1}, @island)
94
- broker.host.should == "localhost"
95
- broker.port.should == 5672
96
- broker.index.should == 0
97
- broker.queues.should == []
98
- broker.island_id.should == 2
99
- broker.island_alias.should == "i2"
100
- broker.in_home_island.should be_false
101
- broker.summary.should == {:alias => "i2b0", :identity => @identity, :status => :connecting,
102
- :disconnects => 0, :failures => 0, :retries => 0}
103
- end
104
-
105
- it "should update state from existing client for given broker" do
106
- existing = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
107
- existing.__send__(:update_status, :disconnected)
108
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options, nil, existing)
109
- broker.summary.should == {:alias => "b0", :identity => @identity, :status => :connecting,
110
- :disconnects => 1, :failures => 0, :retries => 0}
111
- end
112
-
113
- it "should log an info message when it creates an AMQP connection" do
114
- @log.should_receive(:info).with(/Connecting to broker/).once
115
- RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
116
- end
117
-
118
- it "should log an error and set status to :failed if it fails to create an AMQP connection" do
119
- @exceptions.should_receive(:track).once
120
- @connection.should_receive(:close).once
121
- @log.should_receive(:info).once
122
- @log.should_receive(:error).with(/Failed connecting/, Exception, :trace).once
123
- flexmock(MQ).should_receive(:new).with(@connection).and_raise(Exception)
124
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
125
- broker.summary.should == {:alias => "b0", :identity => @identity, :status => :failed,
126
- :disconnects => 0, :failures => 1, :retries => 0}
127
- end
128
-
129
- it "should set initialize connection status callback" do
130
- @connection.should_receive(:connection_status).once
131
- RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
132
- end
133
-
134
- it "should set broker prefetch value if specified" do
135
- @channel.should_receive(:prefetch).with(1).once
136
- RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, {:prefetch => 1})
137
- end
138
-
139
- end # when initializing connection
140
-
141
- context "when subscribing" do
142
-
143
- before(:each) do
144
- @info = flexmock("info", :ack => true).by_default
145
- @message = flexmock("message")
146
- @packet = flexmock("packet", :class => RightScale::Request, :to_s => true, :version => [12, 12]).by_default
147
- @serializer.should_receive(:load).with(@message).and_return(@packet).by_default
148
- @direct = flexmock("direct")
149
- @fanout = flexmock("fanout")
150
- @bind = flexmock("bind")
151
- @queue = flexmock("queue")
152
- @queue.should_receive(:bind).and_return(@bind).by_default
153
- @channel.should_receive(:queue).and_return(@queue).by_default
154
- @channel.should_receive(:direct).and_return(@direct).by_default
155
- @channel.should_receive(:fanout).and_return(@fanout).by_default
156
- flexmock(MQ).should_receive(:new).with(@connection).and_return(@channel).by_default
157
- end
158
-
159
- it "should subscribe queue to exchange" do
160
- @queue.should_receive(:bind).and_return(@bind).once
161
- @bind.should_receive(:subscribe).once
162
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
163
- broker.__send__(:update_status, :ready)
164
- broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}) {|_, _|}
165
- end
166
-
167
- it "should subscribe queue to second exchange if specified" do
168
- @queue.should_receive(:bind).and_return(@bind).twice
169
- @bind.should_receive(:subscribe).once
170
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
171
- broker.__send__(:update_status, :ready)
172
- options = {:exchange2 => {:type => :fanout, :name => "exchange2", :options => {:durable => true}}}
173
- broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}, options) {|_, _|}
174
- end
175
-
176
- it "should subscribe queue to exchange when still connecting" do
177
- @bind.should_receive(:subscribe).once
178
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
179
- broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}) {|_, _|}
180
- end
181
-
182
- it "should subscribe queue to empty exchange if no exchange specified" do
183
- @queue.should_receive(:subscribe).once
184
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
185
- broker.__send__(:update_status, :ready)
186
- broker.subscribe({:name => "queue"}) {|b, p| p.should == nil}
187
- end
188
-
189
- it "should store queues for future reference" do
190
- @bind.should_receive(:subscribe).once
191
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
192
- broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"})
193
- broker.queues.should == [@queue]
194
- end
195
-
196
- it "should return true if subscribed successfully" do
197
- @bind.should_receive(:subscribe).and_yield(@message).once
198
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
199
- result = broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"},
200
- RightScale::Request => true) {|b, p| p.should == @packet}
201
- result.should be_true
202
- end
203
-
204
- it "should return true if already subscribed and not try to resubscribe" do
205
- @queue.should_receive(:name).and_return("queue").once
206
- @bind.should_receive(:subscribe).and_yield(@message).once
207
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
208
- result = broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"},
209
- RightScale::Request => true) {|b, p| p.should == @packet}
210
- result.should be_true
211
- result = broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}) {|_, _|}
212
- result.should be_true
213
- end
214
-
215
- it "should ack received message if requested" do
216
- @info.should_receive(:ack).once
217
- @bind.should_receive(:subscribe).and_yield(@info, @message).once
218
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
219
- broker.__send__(:update_status, :ready)
220
- result = broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"},
221
- :ack => true, RightScale::Request => true) {|b, p| p.should == @packet}
222
- result.should be_true
223
- end
224
-
225
- it "should return false if client not usable" do
226
- @queue.should_receive(:bind).and_return(@bind).never
227
- @bind.should_receive(:subscribe).never
228
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
229
- broker.__send__(:update_status, :disconnected)
230
- broker.subscribe({:name => "queue"}).should be_false
231
- end
232
-
233
- it "should receive message causing it to be unserialized and logged" do
234
- @log.should_receive(:info).with(/Connecting/).once
235
- @log.should_receive(:info).with(/Subscribing/).once
236
- @log.should_receive(:info).with(/RECV/).once
237
- @serializer.should_receive(:load).with(@message).and_return(@packet).once
238
- @bind.should_receive(:subscribe).and_yield(@message).once
239
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
240
- broker.__send__(:update_status, :ready)
241
- broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"},
242
- RightScale::Request => nil) {|b, p| p.class.should == RightScale::Request}
243
- end
244
-
245
- it "should receive message and log exception if subscribe block fails" do
246
- @log.should_receive(:info).with(/Connecting/).once
247
- @log.should_receive(:info).with(/Subscribing/).once
248
- @log.should_receive(:error).with(/Failed executing block/, Exception, :trace).once
249
- @exceptions.should_receive(:track).once
250
- @serializer.should_receive(:load).with(@message).and_return(@packet).once
251
- @bind.should_receive(:subscribe).and_yield(@message).once
252
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
253
- broker.__send__(:update_status, :ready)
254
- result = broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"},
255
- RightScale::Request => nil) {|b, p| raise Exception}
256
- result.should be_false
257
- end
258
-
259
- it "should ignore 'nil' message when using ack" do
260
- @log.should_receive(:level).and_return(:debug)
261
- @log.should_receive(:info).with(/Connecting/).once
262
- @log.should_receive(:info).with(/Subscribing/).once
263
- @log.should_receive(:debug).with(/nil message ignored/).once
264
- @bind.should_receive(:subscribe).and_yield(@info, "nil").once
265
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
266
- broker.__send__(:update_status, :ready)
267
- called = 0
268
- broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}, :ack => true) { |b, m| called += 1 }
269
- called.should == 0
270
- end
271
-
272
- it "should ignore 'nil' message when not using ack" do
273
- @log.should_receive(:level).and_return(:debug)
274
- @log.should_receive(:info).with(/Connecting/).once
275
- @log.should_receive(:info).with(/Subscribing/).once
276
- @log.should_receive(:debug).with(/nil message ignored/).once
277
- @bind.should_receive(:subscribe).and_yield("nil").once
278
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
279
- broker.__send__(:update_status, :ready)
280
- called = 0
281
- broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}) { |b, m| called += 1 }
282
- called.should == 0
283
- end
284
-
285
- it "should not unserialize the message if requested" do
286
- @log.should_receive(:info).with(/Connecting/).once
287
- @log.should_receive(:info).with(/Subscribing/).once
288
- @log.should_receive(:info).with(/^RECV/).never
289
- @bind.should_receive(:subscribe).and_yield(@message).once
290
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
291
- broker.__send__(:update_status, :ready)
292
- broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}, :no_unserialize => true) do |b, m|
293
- b.should == "rs-broker-localhost-5672"
294
- m.should == @message
295
- end
296
- end
297
-
298
- it "should log an error if a subscribe fails" do
299
- @log.should_receive(:info).with(/Connecting/).once
300
- @log.should_receive(:info).with(/RECV/).never
301
- @log.should_receive(:error).with(/Failed subscribing/, Exception, :trace).once
302
- @exceptions.should_receive(:track).once
303
- @bind.should_receive(:subscribe).and_raise(Exception)
304
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
305
- broker.__send__(:update_status, :ready)
306
- result = broker.subscribe({:name => "queue"}, {:type => :direct, :name => "exchange"}) {|b, p|}
307
- result.should be_false
308
- end
309
-
310
- end # when subscribing
311
-
312
- context "when receiving" do
313
-
314
- before(:each) do
315
- @message = flexmock("message")
316
- @packet = flexmock("packet", :class => RightScale::Request, :to_s => true, :version => [12, 12]).by_default
317
- @serializer.should_receive(:load).with(@message).and_return(@packet).once.by_default
318
- end
319
-
320
- it "should unserialize the message, log it, and return it" do
321
- @log.should_receive(:info).with(/Connecting/).once
322
- @log.should_receive(:info).with(/^RECV/).once
323
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
324
- broker.__send__(:receive, "queue", @message, RightScale::Request => nil).should == @packet
325
- end
326
-
327
- it "should log a warning if the message is not of the right type and return nil" do
328
- @log.should_receive(:warning).with(/Received invalid.*packet type/).once
329
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
330
- broker.__send__(:receive, "queue", @message).should be_nil
331
- end
332
-
333
- it "should show the category in the warning message if specified" do
334
- @log.should_receive(:warning).with(/Received invalid xxxx packet type/).once
335
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
336
- broker.__send__(:receive, "queue", @message, RightScale::Result => nil, :category => "xxxx")
337
- end
338
-
339
- it "should display broker alias in the log" do
340
- @log.should_receive(:info).with(/Connecting/).once
341
- @log.should_receive(:info).with(/^RECV b0 /).once
342
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
343
- broker.__send__(:receive, "queue", @message, RightScale::Request => nil)
344
- end
345
-
346
- it "should filter the packet display for :info level" do
347
- @log.should_receive(:info).with(/Connecting/).once
348
- @log.should_receive(:info).with(/^RECV.*TO YOU/).once
349
- @log.should_receive(:debug).with(/^RECV.*TO YOU/).never
350
- @packet.should_receive(:to_s).with([:to], :recv_version).and_return("TO YOU").once
351
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
352
- broker.__send__(:receive, "queue", @message, RightScale::Request => [:to])
353
- end
354
-
355
- it "should not filter the packet display for :debug level" do
356
- @log.should_receive(:level).and_return(:debug)
357
- @log.should_receive(:info).with(/Connecting/).once
358
- @log.should_receive(:info).with(/^RECV.*ALL/).never
359
- @log.should_receive(:info).with(/^RECV.*ALL/).once
360
- @packet.should_receive(:to_s).with(nil, :recv_version).and_return("ALL").once
361
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
362
- broker.__send__(:receive, "queue", @message, RightScale::Request => [:to])
363
- end
364
-
365
- it "should display additional data in log" do
366
- @log.should_receive(:info).with(/Connecting/).once
367
- @log.should_receive(:info).with(/^RECV.*More data/).once
368
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
369
- broker.__send__(:receive, "queue", @message, RightScale::Request => nil, :log_data => "More data")
370
- end
371
-
372
- it "should not log a message if requested not to" do
373
- @log.should_receive(:info).with(/Connecting/).once
374
- @log.should_receive(:info).with(/^RECV/).never
375
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
376
- broker.__send__(:receive, "queue", @message, RightScale::Request => nil, :no_log => true)
377
- end
378
-
379
- it "should not log a message if requested not to unless debug level" do
380
- @log.should_receive(:level).and_return(:debug)
381
- @log.should_receive(:info).with(/Connecting/).once
382
- @log.should_receive(:info).with(/^RECV/).once
383
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
384
- broker.__send__(:receive, "queue", @message, RightScale::Request => nil, :no_log => true)
385
- end
386
-
387
- it "should log an error if exception prevents normal logging and should then return nil" do
388
- @log.should_receive(:error).with(/Failed receiving from queue/, Exception, :trace).once
389
- @serializer.should_receive(:load).with(@message).and_raise(Exception).once
390
- @exceptions.should_receive(:track).once
391
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
392
- broker.__send__(:receive, "queue", @message).should be_nil
393
- end
394
-
395
- it "should make callback when there is a receive failure" do
396
- @log.should_receive(:error).with(/Failed receiving from queue/, Exception, :trace).once
397
- @serializer.should_receive(:load).with(@message).and_raise(Exception).once
398
- @exceptions.should_receive(:track).once
399
- called = 0
400
- callback = lambda { |msg, e| called += 1 }
401
- options = {:exception_on_receive_callback => callback}
402
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, options)
403
- broker.__send__(:receive, "queue", @message).should be_nil
404
- called.should == 1
405
- end
406
-
407
- it "should display RE-RECV if the message being received is a retry" do
408
- @log.should_receive(:info).with(/Connecting/).once
409
- @log.should_receive(:info).with(/^RE-RECV/).once
410
- @packet.should_receive(:tries).and_return(["try1"]).once
411
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
412
- broker.__send__(:receive, "queue", @message, RightScale::Request => nil).should == @packet
413
- end
414
-
415
- end # when receiving
416
-
417
- context "when unsubscribing" do
418
-
419
- before(:each) do
420
- @direct = flexmock("direct")
421
- @bind = flexmock("bind", :subscribe => true)
422
- @queue = flexmock("queue", :bind => @bind, :name => "queue1")
423
- @channel.should_receive(:queue).and_return(@queue).by_default
424
- @channel.should_receive(:direct).and_return(@direct).by_default
425
- flexmock(MQ).should_receive(:new).with(@connection).and_return(@channel).by_default
426
- end
427
-
428
- it "should unsubscribe a queue by name" do
429
- @queue.should_receive(:unsubscribe).once
430
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
431
- broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"})
432
- broker.unsubscribe(["queue1"])
433
- end
434
-
435
- it "should ignore unsubscribe if queue unknown" do
436
- @queue.should_receive(:unsubscribe).never
437
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
438
- broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"})
439
- broker.unsubscribe(["queue2"])
440
- end
441
-
442
- it "should activate block after unsubscribing if provided" do
443
- @queue.should_receive(:unsubscribe).and_yield.once
444
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
445
- broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"})
446
- called = 0
447
- broker.unsubscribe(["queue1"]) { called += 1 }
448
- called.should == 1
449
- end
450
-
451
- it "should ignore the request if client not usable" do
452
- @queue.should_receive(:unsubscribe).and_yield.never
453
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
454
- broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"})
455
- broker.__send__(:update_status, :disconnected)
456
- broker.unsubscribe(["queue1"])
457
- end
458
-
459
- it "should log an error if unsubscribe raises an exception and activate block if provided" do
460
- @log.should_receive(:error).with(/Failed unsubscribing/, Exception, :trace).once
461
- @queue.should_receive(:unsubscribe).and_raise(Exception).once
462
- @exceptions.should_receive(:track).once
463
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
464
- broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"})
465
- called = 0
466
- broker.unsubscribe(["queue1"]) { called += 1 }
467
- called.should == 1
468
- end
469
-
470
- end # when unsubscribing
471
-
472
- context "when declaring" do
473
-
474
- before(:each) do
475
- flexmock(MQ).should_receive(:new).with(@connection).and_return(@channel).by_default
476
- @channel.should_receive(:queues).and_return({}).by_default
477
- @channel.should_receive(:exchanges).and_return({}).by_default
478
- end
479
-
480
- it "should declare exchange and return true" do
481
- @channel.should_receive(:exchange).once
482
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
483
- broker.declare(:exchange, "x", :durable => true).should be_true
484
- end
485
-
486
- it "should delete the exchange or queue from the AMQP cache before declaring" do
487
- @channel.should_receive(:queue).once
488
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
489
- flexmock(broker).should_receive(:delete_amqp_resources).with(:queue, "queue").once
490
- broker.declare(:queue, "queue", :durable => true).should be_true
491
- end
492
-
493
- it "should log declaration" do
494
- @log.should_receive(:info).with(/Connecting/).once
495
- @log.should_receive(:info).with(/Declaring/).once
496
- @channel.should_receive(:queue).once
497
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
498
- broker.declare(:queue, "q").should be_true
499
- end
500
-
501
- it "should return false if client not usable" do
502
- @channel.should_receive(:exchange).never
503
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
504
- broker.__send__(:update_status, :disconnected)
505
- broker.declare(:exchange, "x", :durable => true).should be_false
506
-
507
- end
508
-
509
- it "should log an error if the declare fails and return false" do
510
- @log.should_receive(:info).with(/Connecting/).once
511
- @log.should_receive(:info).with(/Declaring/).once
512
- @log.should_receive(:error).with(/Failed declaring/, Exception, :trace).once
513
- @exceptions.should_receive(:track).once
514
- @channel.should_receive(:queue).and_raise(Exception).once
515
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
516
- broker.declare(:queue, "q").should be_false
517
- end
518
-
519
- end # when declaring
520
-
521
- context "when publishing" do
522
-
523
- before(:each) do
524
- @message = flexmock("message")
525
- @packet = flexmock("packet", :class => RightScale::Request, :to_s => true, :version => [12, 12]).by_default
526
- @direct = flexmock("direct")
527
- flexmock(MQ).should_receive(:new).with(@connection).and_return(@channel).by_default
528
- end
529
-
530
- it "should serialize message, publish it, and return true" do
531
- @channel.should_receive(:direct).with("exchange", :durable => true).and_return(@direct).once
532
- @direct.should_receive(:publish).with(@message, :persistent => true).once
533
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
534
- broker.__send__(:update_status, :ready)
535
- broker.publish({:type => :direct, :name => "exchange", :options => {:durable => true}},
536
- @packet, @message, :persistent => true).should be_true
537
- end
538
-
539
- it "should delete the exchange or queue from the AMQP cache if :declare specified" do
540
- @channel.should_receive(:direct).with("exchange", {:declare => true}).and_return(@direct)
541
- @direct.should_receive(:publish).with(@message, {})
542
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
543
- broker.__send__(:update_status, :ready)
544
- exchange = {:type => :direct, :name => "exchange", :options => {:declare => true}}
545
- flexmock(broker).should_receive(:delete_amqp_resources).with(:direct, "exchange").once
546
- broker.publish(exchange, @packet, @message).should be_true
547
- end
548
-
549
- it "should return false if client not connected" do
550
- @channel.should_receive(:direct).never
551
- @direct.should_receive(:publish).with(@message, :persistent => true).never
552
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
553
- broker.publish({:type => :direct, :name => "exchange", :options => {:durable => true}},
554
- @packet, @message, :persistent => true).should be_false
555
- end
556
-
557
- it "should log an error if the publish fails" do
558
- @log.should_receive(:error).with(/Failed publishing/, Exception, :trace).once
559
- @exceptions.should_receive(:track).once
560
- @channel.should_receive(:direct).and_raise(Exception)
561
- @direct.should_receive(:publish).with(@message, {}).never
562
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
563
- broker.__send__(:update_status, :ready)
564
- broker.publish({:type => :direct, :name => "exchange"}, @packet, @message).should be_false
565
- end
566
-
567
- it "should log that message is being sent with info about which broker used" do
568
- @log.should_receive(:info).with(/Connecting/).once
569
- @log.should_receive(:info).with(/^SEND b0/).once
570
- @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
571
- @direct.should_receive(:publish).with(@message, {}).once
572
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
573
- broker.__send__(:update_status, :ready)
574
- broker.publish({:type => :direct, :name => "exchange"}, @packet, @message).should be_true
575
- end
576
-
577
- it "should log broker choices for :debug level" do
578
- @log.should_receive(:level).and_return(:debug)
579
- @log.should_receive(:debug).with(/... publish options/).once
580
- @log.should_receive(:info).with(/Connecting/).once
581
- @log.should_receive(:info).with(/^SEND b0/).once
582
- @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
583
- @direct.should_receive(:publish).with(@message, {}).once
584
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
585
- broker.__send__(:update_status, :ready)
586
- broker.publish({:type => :direct, :name => "exchange"}, @packet, @message).should be_true
587
- end
588
-
589
- it "should not log a message if requested not to" do
590
- @log.should_receive(:info).with(/Connecting/).once
591
- @log.should_receive(:info).with(/^SEND/).never
592
- @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
593
- @direct.should_receive(:publish).with(@message, :no_log => true).once
594
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
595
- broker.__send__(:update_status, :ready)
596
- broker.publish({:type => :direct, :name => "exchange"}, @packet, @message, :no_log => true).should be_true
597
- end
598
-
599
- it "should not log a message if requested not to unless debug level" do
600
- @log.should_receive(:level).and_return(:debug)
601
- @log.should_receive(:info).with(/Connecting/).once
602
- @log.should_receive(:info).with(/^SEND/).once
603
- @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
604
- @direct.should_receive(:publish).with(@message, :no_log => true).once
605
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
606
- broker.__send__(:update_status, :ready)
607
- broker.publish({:type => :direct, :name => "exchange"}, @packet, @message, :no_log => true).should be_true
608
- end
609
-
610
- it "should display broker alias in the log" do
611
- @log.should_receive(:info).with(/Connecting/).once
612
- @log.should_receive(:info).with(/^SEND b0 /).once
613
- @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
614
- @direct.should_receive(:publish).with(@message, {}).once
615
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
616
- broker.__send__(:update_status, :ready)
617
- broker.publish({:type => :direct, :name => "exchange"}, @packet, @message).should be_true
618
- end
619
-
620
- it "should filter the packet display for :info level" do
621
- @log.should_receive(:info).with(/Connecting/).once
622
- @log.should_receive(:info).with(/^SEND.*TO YOU/).once
623
- @log.should_receive(:info).with(/^SEND.*TO YOU/).never
624
- @packet.should_receive(:to_s).with([:to], :send_version).and_return("TO YOU").once
625
- @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
626
- @direct.should_receive(:publish).with(@message, :log_filter => [:to]).once
627
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
628
- broker.__send__(:update_status, :ready)
629
- broker.publish({:type => :direct, :name => "exchange"}, @packet, @message, :log_filter => [:to]).should be_true
630
- end
631
-
632
- it "should not filter the packet display for :debug level" do
633
- @log.should_receive(:level).and_return(:debug)
634
- @log.should_receive(:info).with(/Connecting/).once
635
- @log.should_receive(:info).with(/^SEND.*ALL/).never
636
- @log.should_receive(:info).with(/^SEND.*ALL/).once
637
- @packet.should_receive(:to_s).with(nil, :send_version).and_return("ALL").once
638
- @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
639
- @direct.should_receive(:publish).with(@message, :log_filter => [:to]).once
640
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
641
- broker.__send__(:update_status, :ready)
642
- broker.publish({:type => :direct, :name => "exchange"}, @packet, @message, :log_filter => [:to]).should be_true
643
- end
644
-
645
- it "should display additional data in log" do
646
- @log.should_receive(:info).with(/Connecting/).once
647
- @log.should_receive(:info).with(/^SEND.*More data/).once
648
- @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
649
- @direct.should_receive(:publish).with(@message, :log_data => "More data").once
650
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
651
- broker.__send__(:update_status, :ready)
652
- broker.publish({:type => :direct, :name => "exchange"}, @packet, @message, :log_data => "More data").should be_true
653
- end
654
-
655
- it "should display RE-SEND if the message being sent is a retry" do
656
- @log.should_receive(:info).with(/Connecting/).once
657
- @log.should_receive(:info).with(/^RE-SEND/).once
658
- @packet = flexmock("packet", :class => RightScale::Request, :to_s => true, :tries => ["try1"], :version => [12, 12])
659
- @channel.should_receive(:direct).with("exchange", {}).and_return(@direct).once
660
- @direct.should_receive(:publish).with(@message, {}).once
661
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
662
- broker.__send__(:update_status, :ready)
663
- broker.publish({:type => :direct, :name => "exchange"}, @packet, @message).should be_true
664
- end
665
-
666
- end # when publishing
667
-
668
- context "when returning" do
669
-
670
- class MQ
671
- attr_accessor :connection, :on_return_message
672
-
673
- def initialize(connection)
674
- @connection = connection
675
- end
676
-
677
- def return_message(&blk)
678
- @on_return_message = blk
679
- end
680
- end
681
-
682
- before(:each) do
683
- @message = flexmock("message")
684
- @packet = flexmock("packet", :class => RightScale::Request, :to_s => true, :version => [12, 12]).by_default
685
- @info = flexmock("info", :reply_text => "NO_CONSUMERS", :exchange => "exchange", :routing_key => "routing_key").by_default
686
- @serializer.should_receive(:load).with(@message).and_return(@packet).by_default
687
- end
688
-
689
- it "should invoke block and log the return" do
690
- @log.should_receive(:info).with(/Connecting to broker/).once
691
- @log.should_receive(:debug).with(/RETURN b0/).once
692
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
693
- called = 0
694
- broker.return_message do |to, reason, message|
695
- called += 1
696
- to.should == "exchange"
697
- reason.should == "NO_CONSUMERS"
698
- message.should == @message
699
- end
700
- broker.instance_variable_get(:@channel).on_return_message.call(@info, @message)
701
- called.should == 1
702
- end
703
-
704
- it "should invoke block with routing key if exchange is empty" do
705
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
706
- called = 0
707
- broker.return_message do |to, reason, message|
708
- called += 1
709
- to.should == "routing_key"
710
- reason.should == "NO_CONSUMERS"
711
- message.should == @message
712
- end
713
- @info.should_receive(:exchange).and_return("")
714
- broker.instance_variable_get(:@channel).on_return_message.call(@info, @message)
715
- called.should == 1
716
- end
717
-
718
- it "should log an error if there is a failure while processing the return" do
719
- @log.should_receive(:error).with(/Failed return/, Exception, :trace).once
720
- @exceptions.should_receive(:track).once
721
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
722
- called = 0
723
- broker.return_message do |to, reason, message|
724
- called += 1
725
- raise Exception
726
- end
727
- broker.instance_variable_get(:@channel).on_return_message.call(@info, @message)
728
- called.should == 1
729
- end
730
-
731
- end # when returning
732
-
733
- context "when deleting" do
734
-
735
- before(:each) do
736
- @direct = flexmock("direct")
737
- @bind = flexmock("bind", :subscribe => true)
738
- @queue = flexmock("queue", :bind => @bind, :name => "queue1")
739
- @channel.should_receive(:queue).and_return(@queue).by_default
740
- @channel.should_receive(:direct).and_return(@direct).by_default
741
- flexmock(MQ).should_receive(:new).with(@connection).and_return(@channel).by_default
742
- end
743
-
744
- it "should delete the named queue and return true" do
745
- @queue.should_receive(:delete).once
746
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
747
- broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"})
748
- broker.queues.should == [@queue]
749
- broker.delete("queue1").should be_true
750
- broker.queues.should == []
751
- end
752
-
753
- it "should return false if the client is not usable" do
754
- @queue.should_receive(:delete).never
755
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
756
- broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"})
757
- broker.queues.should == [@queue]
758
- broker.__send__(:update_status, :disconnected)
759
- broker.delete("queue1").should be_false
760
- broker.queues.should == [@queue]
761
- end
762
-
763
- it "should log an error and return false if the delete fails" do
764
- @log.should_receive(:error).with(/Failed deleting queue/, Exception, :trace).once
765
- @exceptions.should_receive(:track).once
766
- @queue.should_receive(:delete).and_raise(Exception)
767
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
768
- broker.subscribe({:name => "queue1"}, {:type => :direct, :name => "exchange"})
769
- broker.queues.should == [@queue]
770
- broker.delete("queue1").should be_false
771
- end
772
-
773
- end # when deleteing
774
-
775
- context "when monitoring" do
776
-
777
- include RightScale::StatsHelper
778
-
779
- before(:each) do
780
- flexmock(MQ).should_receive(:new).with(@connection).and_return(@channel).by_default
781
- end
782
-
783
- it "should distinguish whether the client is usable based on whether connecting or connected" do
784
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
785
- broker.usable?.should be_true
786
- broker.__send__(:update_status, :ready)
787
- broker.usable?.should be_true
788
- broker.__send__(:update_status, :disconnected)
789
- broker.usable?.should be_false
790
- @log.should_receive(:error).with(/Failed to connect to broker b0/).once
791
- broker.__send__(:update_status, :failed)
792
- broker.usable?.should be_false
793
- end
794
-
795
- it "should distinguish whether the client is connected" do
796
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
797
- broker.connected?.should be_false
798
- broker.__send__(:update_status, :ready)
799
- broker.connected?.should be_true
800
- broker.__send__(:update_status, :disconnected)
801
- broker.connected?.should be_false
802
- @log.should_receive(:error).with(/Failed to connect to broker b0/).once
803
- broker.__send__(:update_status, :failed)
804
- broker.connected?.should be_false
805
- end
806
-
807
- it "should distinguish whether the client has failed" do
808
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
809
- broker.failed?.should be_false
810
- broker.__send__(:update_status, :ready)
811
- broker.failed?.should be_false
812
- broker.__send__(:update_status, :disconnected)
813
- broker.failed?.should be_false
814
- @log.should_receive(:error).with(/Failed to connect to broker b0/).once
815
- broker.__send__(:update_status, :failed)
816
- broker.failed?.should be_true
817
- end
818
-
819
- it "should give broker summary" do
820
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
821
- broker.summary.should == {:alias => "b0", :identity => @identity, :status => :connecting,
822
- :disconnects => 0, :failures => 0, :retries => 0}
823
- broker.__send__(:update_status, :ready)
824
- broker.summary.should == {:alias => "b0", :identity => @identity, :status => :connected,
825
- :disconnects => 0, :failures => 0, :retries => 0}
826
- @log.should_receive(:error).with(/Failed to connect to broker/).once
827
- broker.__send__(:update_status, :failed)
828
- broker.summary.should == {:alias => "b0", :identity => @identity, :status => :failed,
829
- :disconnects => 0, :failures => 1, :retries => 0}
830
- end
831
-
832
- it "should give broker statistics" do
833
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
834
- broker.stats.should == {"alias" => "b0", "identity" => "rs-broker-localhost-5672",
835
- "status" => "connecting", "disconnects" => nil, "disconnect last" => nil,
836
- "failures" => nil, "failure last" => nil, "retries" => nil}
837
- broker.__send__(:update_status, :ready)
838
- broker.stats.should == {"alias" => "b0", "identity" => "rs-broker-localhost-5672",
839
- "status" => "connected", "disconnects" => nil, "disconnect last" => nil,
840
- "failures" => nil, "failure last" => nil, "retries" => nil}
841
- @log.should_receive(:error).with(/Failed to connect to broker/).once
842
- broker.__send__(:update_status, :failed)
843
- broker.stats.should == {"alias" => "b0", "identity" => "rs-broker-localhost-5672",
844
- "status" => "failed", "disconnects" => nil, "disconnect last" => nil,
845
- "failures" => 1, "failure last" => {"elapsed" => 0}, "retries" => nil}
846
- end
847
-
848
- it "should make update status callback when status changes" do
849
- broker = nil
850
- called = 0
851
- connected_before = false
852
- callback = lambda { |b, c| called += 1; b.should == broker; c.should == connected_before }
853
- options = {:update_status_callback => callback}
854
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, options)
855
- broker.__send__(:update_status, :ready)
856
- broker.last_failed.should be_false
857
- called.should == 1
858
- connected_before = true
859
- broker.__send__(:update_status, :disconnected)
860
- broker.last_failed.should be_false
861
- broker.disconnects.total.should == 1
862
- called.should == 2
863
- broker.__send__(:update_status, :disconnected)
864
- broker.disconnects.total.should == 1
865
- called.should == 2
866
- @log.should_receive(:error).with(/Failed to connect to broker b0/).once
867
- connected_before = false
868
- broker.__send__(:update_status, :failed)
869
- broker.last_failed.should be_true
870
- called.should == 3
871
- end
872
-
873
- end # when monitoring
874
-
875
- context "when closing" do
876
-
877
- before(:each) do
878
- flexmock(MQ).should_receive(:new).with(@connection).and_return(@channel).by_default
879
- end
880
-
881
- it "should close broker connection and send status update" do
882
- @connection.should_receive(:close).and_yield.once
883
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
884
- flexmock(broker).should_receive(:update_status).once
885
- broker.close
886
- broker.status.should == :closed
887
- end
888
-
889
- it "should not propagate status update if requested not to" do
890
- @connection.should_receive(:close).and_yield.once
891
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
892
- flexmock(broker).should_receive(:update_status).never
893
- broker.close(propagate = false)
894
- end
895
-
896
- it "should set status to :failed if not a normal close" do
897
- @connection.should_receive(:close).and_yield.once
898
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
899
- broker.close(propagate = false, normal = false)
900
- broker.status.should == :failed
901
- end
902
-
903
- it "should log that closing connection" do
904
- @log.should_receive(:info).with(/Connecting/).once
905
- @log.should_receive(:info).with(/Closed connection to broker b0/).once
906
- @connection.should_receive(:close).and_yield.once
907
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
908
- broker.close
909
- end
910
-
911
- it "should not log if requested not to" do
912
- @log.should_receive(:info).with(/Connecting/).once
913
- @log.should_receive(:info).with(/Closed connection to broker b0/).never
914
- @connection.should_receive(:close).and_yield.once
915
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
916
- broker.close(propagate = true, normal = true, log = false)
917
- end
918
-
919
- it "should close broker connection and execute block if supplied" do
920
- @connection.should_receive(:close).and_yield.once
921
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
922
- called = 0
923
- broker.close { called += 1; broker.status.should == :closed }
924
- called.should == 1
925
- end
926
-
927
- it "should close broker connection when no block supplied" do
928
- @connection.should_receive(:close).and_yield.once
929
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
930
- broker.close
931
- end
932
-
933
- it "should not propagate status update if already closed" do
934
- @connection.should_receive(:close).never
935
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
936
- broker.__send__(:update_status, :closed)
937
- flexmock(broker).should_receive(:update_status).never
938
- broker.close
939
- end
940
-
941
- it "should change failed status to closed" do
942
- @log.should_receive(:error).with(/Failed to connect to broker/).once
943
- @connection.should_receive(:close).never
944
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
945
- broker.__send__(:update_status, :failed)
946
- flexmock(broker).should_receive(:update_status).never
947
- broker.close
948
- broker.status.should == :closed
949
- end
950
-
951
- it "should log an error if closing connection fails but still set status to :closed" do
952
- @log.should_receive(:error).with(/Failed to close broker b0/, Exception, :trace).once
953
- @exceptions.should_receive(:track).once
954
- @connection.should_receive(:close).and_raise(Exception)
955
- broker = RightScale::BrokerClient.new(@identity, @address, @serializer, @exceptions, @options)
956
- broker.close
957
- broker.status.should == :closed
958
- end
959
-
960
- end # when closing
961
-
962
- end # RightScale::BrokerClient