right_agent 0.5.1 → 0.5.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/lib/right_agent.rb +3 -13
  2. data/lib/right_agent/actors/agent_manager.rb +78 -4
  3. data/lib/right_agent/agent.rb +81 -4
  4. data/lib/right_agent/agent_config.rb +17 -1
  5. data/lib/right_agent/agent_tags_manager.rb +2 -2
  6. data/lib/right_agent/broker_client.rb +32 -34
  7. data/lib/right_agent/command/agent_manager_commands.rb +16 -0
  8. data/lib/right_agent/command/command_constants.rb +0 -9
  9. data/lib/right_agent/dispatcher.rb +6 -3
  10. data/lib/right_agent/ha_broker_client.rb +63 -14
  11. data/lib/right_agent/log.rb +1 -1
  12. data/lib/right_agent/minimal.rb +43 -0
  13. data/lib/right_agent/monkey_patches/amqp_patch.rb +91 -182
  14. data/lib/right_agent/packets.rb +10 -5
  15. data/lib/right_agent/platform.rb +8 -0
  16. data/lib/right_agent/platform/darwin.rb +14 -0
  17. data/lib/right_agent/platform/linux.rb +23 -0
  18. data/lib/right_agent/platform/windows.rb +31 -0
  19. data/lib/right_agent/scripts/agent_controller.rb +16 -8
  20. data/lib/right_agent/scripts/agent_deployer.rb +6 -0
  21. data/lib/right_agent/scripts/log_level_manager.rb +4 -5
  22. data/lib/right_agent/scripts/stats_manager.rb +9 -1
  23. data/lib/right_agent/sender.rb +623 -371
  24. data/lib/right_agent/stats_helper.rb +15 -1
  25. data/lib/right_agent/tracer.rb +1 -1
  26. data/right_agent.gemspec +14 -15
  27. data/spec/agent_config_spec.rb +9 -0
  28. data/spec/agent_spec.rb +154 -18
  29. data/spec/broker_client_spec.rb +171 -170
  30. data/spec/dispatcher_spec.rb +24 -8
  31. data/spec/ha_broker_client_spec.rb +55 -33
  32. data/spec/monkey_patches/amqp_patch_spec.rb +12 -0
  33. data/spec/packets_spec.rb +2 -0
  34. data/spec/sender_spec.rb +140 -69
  35. data/spec/stats_helper_spec.rb +5 -0
  36. metadata +54 -53
@@ -96,6 +96,7 @@ describe "RightScale::Dispatcher" do
96
96
  @agent = flexmock("Agent", :identity => "agent", :broker => @broker, :registry => @registry, :options => {}).by_default
97
97
  @dispatcher = RightScale::Dispatcher.new(@agent)
98
98
  @dispatcher.em = EMMock
99
+ @response_queue = RightScale::Dispatcher::RESPONSE_QUEUE
99
100
  end
100
101
 
101
102
  describe "Dispatched cache" do
@@ -199,6 +200,17 @@ describe "RightScale::Dispatcher" do
199
200
  res.results.should == ['hello', 'you']
200
201
  end
201
202
 
203
+ it "should publish result of request to response queue" do
204
+ req = RightScale::Request.new('/foo', 'you', :token => 'token')
205
+ req.reply_to = "rs-mapper-1-1"
206
+ @broker.should_receive(:publish).with(hsh(:name => @response_queue),
207
+ on {|arg| arg.class == RightScale::Result &&
208
+ arg.to == "rs-mapper-1-1" &&
209
+ arg.results == ['hello', 'you']},
210
+ hsh(:persistent => true, :mandatory => true)).once
211
+ res = @dispatcher.dispatch(req)
212
+ end
213
+
202
214
  it "should handle custom prefixes" do
203
215
  @registry.register(Foo.new, 'umbongo')
204
216
  req = RightScale::Request.new('/umbongo/bar', 'you')
@@ -257,13 +269,15 @@ describe "RightScale::Dispatcher" do
257
269
  it "should send non-delivery result if Request is rejected because its time-to-live has expired" do
258
270
  flexmock(Time).should_receive(:now).and_return(Time.at(1000000)).by_default
259
271
  flexmock(RightScale::Log).should_receive(:info).once.with(on {|arg| arg =~ /REJECT EXPIRED/})
260
- @broker.should_receive(:publish).with(Hash, on {|arg| arg.class == RightScale::Result &&
261
- arg.results.non_delivery? &&
262
- arg.results.content == RightScale::OperationResult::TTL_EXPIRATION},
272
+ @broker.should_receive(:publish).with(hsh(:name => @response_queue),
273
+ on {|arg| arg.class == RightScale::Result &&
274
+ arg.to == @response_queue &&
275
+ arg.results.non_delivery? &&
276
+ arg.results.content == RightScale::OperationResult::TTL_EXPIRATION},
263
277
  hsh(:persistent => true, :mandatory => true)).once
264
278
  @dispatcher = RightScale::Dispatcher.new(@agent)
265
279
  @dispatcher.em = EMMock
266
- req = RightScale::Request.new('/foo/bar', 'you', :expires_at => @now.to_i + 8)
280
+ req = RightScale::Request.new('/foo/bar', 'you', {:reply_to => @response_queue, :expires_at => @now.to_i + 8})
267
281
  flexmock(Time).should_receive(:now).and_return(@now += 10)
268
282
  @dispatcher.dispatch(req).should be_nil
269
283
  end
@@ -271,13 +285,15 @@ describe "RightScale::Dispatcher" do
271
285
  it "should send error result instead of non-delivery if agent does not know about non-delivery" do
272
286
  flexmock(Time).should_receive(:now).and_return(Time.at(1000000)).by_default
273
287
  flexmock(RightScale::Log).should_receive(:info).once.with(on {|arg| arg =~ /REJECT EXPIRED/})
274
- @broker.should_receive(:publish).with(Hash, on {|arg| arg.class == RightScale::Result &&
275
- arg.results.error? &&
276
- arg.results.content =~ /Could not deliver/},
288
+ @broker.should_receive(:publish).with(hsh(:name => @response_queue),
289
+ on {|arg| arg.class == RightScale::Result &&
290
+ arg.to == "rs-mapper-1-1" &&
291
+ arg.results.error? &&
292
+ arg.results.content =~ /Could not deliver/},
277
293
  hsh(:persistent => true, :mandatory => true)).once
278
294
  @dispatcher = RightScale::Dispatcher.new(@agent)
279
295
  @dispatcher.em = EMMock
280
- req = RightScale::Request.new('/foo/bar', 'you', {:expires_at => @now.to_i + 8}, [12, 13])
296
+ req = RightScale::Request.new('/foo/bar', 'you', {:reply_to => "rs-mapper-1-1", :expires_at => @now.to_i + 8}, [12, 13])
281
297
  flexmock(Time).should_receive(:now).and_return(@now += 10)
282
298
  @dispatcher.dispatch(req).should be_nil
283
299
  end
@@ -27,9 +27,10 @@ describe RightScale::HABrokerClient do
27
27
  include FlexMock::ArgumentTypes
28
28
 
29
29
  before(:each) do
30
- flexmock(RightScale::Log).should_receive(:error).by_default.and_return { |m| raise RightScale::Log.format(*m) }
31
- flexmock(RightScale::Log).should_receive(:warning).by_default.and_return { |m| raise RightScale::Log.format(*m) }
32
- flexmock(RightScale::Log).should_receive(:info).by_default
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
33
34
  end
34
35
 
35
36
  describe "Context" do
@@ -595,7 +596,7 @@ describe RightScale::HABrokerClient do
595
596
  end
596
597
 
597
598
  it "should not do anything except log a message if asked to reconnect an already connected broker client" do
598
- flexmock(RightScale::Log).should_receive(:info).with(/Ignored request to reconnect/).once
599
+ @log.should_receive(:info).with(/Ignored request to reconnect/).once
599
600
  ha = RightScale::HABrokerClient.new(@serializer, :host => "a, b")
600
601
  ha.brokers.size.should == 2
601
602
  @broker_a.should_receive(:status).and_return(:connected).once
@@ -610,7 +611,7 @@ describe RightScale::HABrokerClient do
610
611
  end
611
612
 
612
613
  it "should reconnect already connected broker client if force specified" do
613
- flexmock(RightScale::Log).should_receive(:info).with(/Ignored request to reconnect/).never
614
+ @log.should_receive(:info).with(/Ignored request to reconnect/).never
614
615
  ha = RightScale::HABrokerClient.new(@serializer, :host => "a, b")
615
616
  ha.brokers.size.should == 2
616
617
  @broker_a.should_receive(:close).and_return(true).once
@@ -623,6 +624,21 @@ describe RightScale::HABrokerClient do
623
624
  ha.brokers[1].alias == "b1"
624
625
  end
625
626
 
627
+ it "should be able to change host and port of an existing broker client" do
628
+ ha = RightScale::HABrokerClient.new(@serializer, :host => "a, b")
629
+ ha.brokers.size.should == 2
630
+ @broker_a.should_receive(:close).and_return(true).once
631
+ flexmock(RightScale::BrokerClient).should_receive(:new).with(@identity_c, @address_c.merge(:index => 0),
632
+ @serializer, @exceptions, Hash, nil, nil).and_return(@broker_c).once
633
+ res = ha.connect("c", 5672, 0)
634
+ res.should be_true
635
+ ha.brokers.size.should == 2
636
+ ha.brokers[0].alias == "b0"
637
+ ha.brokers[0].identity == @address_c
638
+ ha.brokers[1].alias == "b1"
639
+ ha.brokers[1].identity == @address_b
640
+ end
641
+
626
642
  it "should slot broker client into specified priority position when at end of list" do
627
643
  ha = RightScale::HABrokerClient.new(@serializer, :host => "a, b")
628
644
  ha.brokers.size.should == 2
@@ -646,7 +662,7 @@ describe RightScale::HABrokerClient do
646
662
  end
647
663
 
648
664
  it "should slot broker client into nex priority position if specified priority would leave a gap" do
649
- flexmock(RightScale::Log).should_receive(:info).with(/Reduced priority setting for broker/).once
665
+ @log.should_receive(:info).with(/Reduced priority setting for broker/).once
650
666
  ha = RightScale::HABrokerClient.new(@serializer, :host => "a")
651
667
  ha.brokers.size.should == 1
652
668
  res = ha.connect("c", 5672, 2, 2)
@@ -668,11 +684,6 @@ describe RightScale::HABrokerClient do
668
684
  ha.brokers[1].alias == "b1"
669
685
  end
670
686
 
671
- it "should raise an exception if try to change host and port of an existing broker client" do
672
- ha = RightScale::HABrokerClient.new(@serializer, :host => "a, b")
673
- lambda { ha.connect("c", 5672, 0) }.should raise_error(Exception, /Not allowed to change host or port/)
674
- end
675
-
676
687
  end # connecting
677
688
 
678
689
  context "updating connection" do
@@ -925,7 +936,7 @@ describe RightScale::HABrokerClient do
925
936
  end
926
937
 
927
938
  it "should log an error if a selected broker is unknown but still publish with any remaining brokers" do
928
- flexmock(RightScale::Log).should_receive(:error).with(/Invalid broker identity "rs-broker-fifth-5672"/).once
939
+ @log.should_receive(:error).with(/Invalid broker identity "rs-broker-fifth-5672"/).once
929
940
  ha = RightScale::HABrokerClient.new(@serializer, :islands => @islands, :home_island => @home)
930
941
  ha.publish({:type => :direct, :name => "exchange"}, @packet,
931
942
  :brokers =>["rs-broker-fifth-5672", @identity1]).should == [@identity1]
@@ -1025,8 +1036,8 @@ describe RightScale::HABrokerClient do
1025
1036
  end
1026
1037
 
1027
1038
  it "should republish using a broker not yet tried if possible and log that re-routing" do
1028
- flexmock(RightScale::Log).should_receive(:info).with(/RE-ROUTE/).once
1029
- flexmock(RightScale::Log).should_receive(:info).with(/RETURN reason/).once
1039
+ @log.should_receive(:info).with(/RE-ROUTE/).once
1040
+ @log.should_receive(:info).with(/RETURN reason/).once
1030
1041
  ha = RightScale::HABrokerClient.new(@serializer, :islands => @islands, :home_island => @home)
1031
1042
  @context.record_failure(@identity3)
1032
1043
  @broker4.should_receive(:publish).and_return(true).once
@@ -1034,8 +1045,8 @@ describe RightScale::HABrokerClient do
1034
1045
  end
1035
1046
 
1036
1047
  it "should republish to same broker without mandatory if message is persistent and no other brokers available" do
1037
- flexmock(RightScale::Log).should_receive(:info).with(/RE-ROUTE/).once
1038
- flexmock(RightScale::Log).should_receive(:info).with(/RETURN reason/).once
1048
+ @log.should_receive(:info).with(/RE-ROUTE/).once
1049
+ @log.should_receive(:info).with(/RETURN reason/).once
1039
1050
  ha = RightScale::HABrokerClient.new(@serializer, :islands => @islands, :home_island => @home)
1040
1051
  @context.record_failure(@identity3)
1041
1052
  @context.record_failure(@identity4)
@@ -1045,8 +1056,8 @@ describe RightScale::HABrokerClient do
1045
1056
  end
1046
1057
 
1047
1058
  it "should republish to same broker without mandatory if message is one-way and no other brokers available" do
1048
- flexmock(RightScale::Log).should_receive(:info).with(/RE-ROUTE/).once
1049
- flexmock(RightScale::Log).should_receive(:info).with(/RETURN reason/).once
1059
+ @log.should_receive(:info).with(/RE-ROUTE/).once
1060
+ @log.should_receive(:info).with(/RETURN reason/).once
1050
1061
  ha = RightScale::HABrokerClient.new(@serializer, :islands => @islands, :home_island => @home)
1051
1062
  @context.record_failure(@identity3)
1052
1063
  @context.record_failure(@identity4)
@@ -1056,8 +1067,8 @@ describe RightScale::HABrokerClient do
1056
1067
  end
1057
1068
 
1058
1069
  it "should update status to :stopping if message returned because access refused" do
1059
- flexmock(RightScale::Log).should_receive(:info).with(/RE-ROUTE/).once
1060
- flexmock(RightScale::Log).should_receive(:info).with(/RETURN reason/).once
1070
+ @log.should_receive(:info).with(/RE-ROUTE/).once
1071
+ @log.should_receive(:info).with(/RETURN reason/).once
1061
1072
  ha = RightScale::HABrokerClient.new(@serializer, :islands => @islands, :home_island => @home)
1062
1073
  @context.record_failure(@identity3)
1063
1074
  @broker4.should_receive(:publish).and_return(true).once
@@ -1066,8 +1077,8 @@ describe RightScale::HABrokerClient do
1066
1077
  end
1067
1078
 
1068
1079
  it "should log info and make non-delivery call even if persistent when returned because of no queue" do
1069
- flexmock(RightScale::Log).should_receive(:info).with(/NO ROUTE/).once
1070
- flexmock(RightScale::Log).should_receive(:info).with(/RETURN reason/).once
1080
+ @log.should_receive(:info).with(/NO ROUTE/).once
1081
+ @log.should_receive(:info).with(/RETURN reason/).once
1071
1082
  ha = RightScale::HABrokerClient.new(@serializer, :islands => @islands, :home_island => @home)
1072
1083
  called = 0
1073
1084
  ha.non_delivery { |reason, type, token, from, to| called += 1 }
@@ -1081,8 +1092,8 @@ describe RightScale::HABrokerClient do
1081
1092
  end
1082
1093
 
1083
1094
  it "should log info and make non-delivery call if no route can be found" do
1084
- flexmock(RightScale::Log).should_receive(:info).with(/NO ROUTE/).once
1085
- flexmock(RightScale::Log).should_receive(:info).with(/RETURN reason/).once
1095
+ @log.should_receive(:info).with(/NO ROUTE/).once
1096
+ @log.should_receive(:info).with(/RETURN reason/).once
1086
1097
  ha = RightScale::HABrokerClient.new(@serializer, :islands => @islands, :home_island => @home)
1087
1098
  called = 0
1088
1099
  ha.non_delivery { |reason, type, token, from, to| called += 1 }
@@ -1095,7 +1106,7 @@ describe RightScale::HABrokerClient do
1095
1106
  end
1096
1107
 
1097
1108
  it "should log info if no message context available for re-routing it" do
1098
- flexmock(RightScale::Log).should_receive(:info).with(/Dropping/).once
1109
+ @log.should_receive(:info).with(/Dropping/).once
1099
1110
  ha = RightScale::HABrokerClient.new(@serializer, :islands => @islands, :home_island => @home)
1100
1111
  ha.__send__(:handle_return, @identity4, "any reason", @message, "to", nil)
1101
1112
  end
@@ -1126,12 +1137,22 @@ describe RightScale::HABrokerClient do
1126
1137
  ha.delete("queue").should == [@identity4, @identity2]
1127
1138
  end
1128
1139
 
1140
+ it "should delete queue from cache on all usable broker clients and return their identities" do
1141
+ ha = RightScale::HABrokerClient.new(@serializer, :islands => @islands, :home_island => @home)
1142
+ @broker1.should_receive(:usable?).and_return(false)
1143
+ @broker1.should_receive(:delete_amqp_resources).never
1144
+ @broker2.should_receive(:delete_amqp_resources).and_return(true).once
1145
+ @broker3.should_receive(:delete_amqp_resources).and_return(true).once
1146
+ @broker4.should_receive(:delete_amqp_resources).and_return(true).once
1147
+ ha.delete_amqp_resources("queue").should == [@identity3, @identity4, @identity2]
1148
+ end
1149
+
1129
1150
  end # deleting
1130
1151
 
1131
1152
  context "removing" do
1132
1153
 
1133
1154
  it "should remove broker client after disconnecting and pass identity to block" do
1134
- flexmock(RightScale::Log).should_receive(:info).with(/Removing/).once
1155
+ @log.should_receive(:info).with(/Removing/).once
1135
1156
  @broker2.should_receive(:close).with(true, true, false).once
1136
1157
  ha = RightScale::HABrokerClient.new(@serializer, :islands => @islands, :home_island => @home)
1137
1158
  identity = nil
@@ -1146,7 +1167,7 @@ describe RightScale::HABrokerClient do
1146
1167
  end
1147
1168
 
1148
1169
  it "should remove broker when no block supplied but still return a result" do
1149
- flexmock(RightScale::Log).should_receive(:info).with(/Removing/).once
1170
+ @log.should_receive(:info).with(/Removing/).once
1150
1171
  @broker2.should_receive(:close).once
1151
1172
  ha = RightScale::HABrokerClient.new(@serializer, :islands => @islands, :home_island => @home)
1152
1173
  result = ha.remove("second", 5672)
@@ -1159,7 +1180,7 @@ describe RightScale::HABrokerClient do
1159
1180
  end
1160
1181
 
1161
1182
  it "should remove last broker if requested" do
1162
- flexmock(RightScale::Log).should_receive(:info).with(/Removing/).times(4)
1183
+ @log.should_receive(:info).with(/Removing/).times(4)
1163
1184
  @broker1.should_receive(:close).once
1164
1185
  @broker2.should_receive(:close).once
1165
1186
  @broker3.should_receive(:close).once
@@ -1184,7 +1205,7 @@ describe RightScale::HABrokerClient do
1184
1205
  end
1185
1206
 
1186
1207
  it "should return nil and not execute block if broker is unknown" do
1187
- flexmock(RightScale::Log).should_receive(:info).with(/Ignored request to remove/).once
1208
+ @log.should_receive(:info).with(/Ignored request to remove/).once
1188
1209
  ha = RightScale::HABrokerClient.new(@serializer, :islands => @islands, :home_island => @home)
1189
1210
  ha.remove("fifth", 5672).should be_nil
1190
1211
  ha.brokers.size.should == 4
@@ -1331,23 +1352,24 @@ describe RightScale::HABrokerClient do
1331
1352
  @broker4.should_receive(:stats).and_return("stats4")
1332
1353
  ha.stats.should == {"brokers" => ["stats3", "stats4", "stats1", "stats2"],
1333
1354
  "exceptions" => nil,
1355
+ "heartbeat" => nil,
1334
1356
  "returns" => nil}
1335
1357
  end
1336
1358
 
1337
1359
  it "should log broker client status update if there is a change" do
1338
- flexmock(RightScale::Log).should_receive(:info).with(/Broker b0 is now connected/).once
1360
+ @log.should_receive(:info).with(/Broker b0 is now connected/).once
1339
1361
  ha = RightScale::HABrokerClient.new(@serializer, :islands => @islands, :home_island => @home)
1340
1362
  ha.__send__(:update_status, @broker3, false)
1341
1363
  end
1342
1364
 
1343
1365
  it "should not log broker client status update if there is no change" do
1344
- flexmock(RightScale::Log).should_receive(:info).with(/Broker b0 is now connected/).never
1366
+ @log.should_receive(:info).with(/Broker b0 is now connected/).never
1345
1367
  ha = RightScale::HABrokerClient.new(@serializer, :islands => @islands, :home_island => @home)
1346
1368
  ha.__send__(:update_status, @broker3, true)
1347
1369
  end
1348
1370
 
1349
1371
  it "should log broker client status update when become disconnected" do
1350
- flexmock(RightScale::Log).should_receive(:info).with(/Broker b0 is now disconnected/).once
1372
+ @log.should_receive(:info).with(/Broker b0 is now disconnected/).once
1351
1373
  ha = RightScale::HABrokerClient.new(@serializer, :islands => @islands, :home_island => @home)
1352
1374
  @broker3.should_receive(:status).and_return(:disconnected)
1353
1375
  @broker3.should_receive(:connected?).and_return(false)
@@ -1630,7 +1652,7 @@ describe RightScale::HABrokerClient do
1630
1652
  end
1631
1653
 
1632
1654
  it "should close all broker connections even if encounter an exception" do
1633
- flexmock(RightScale::Log).should_receive(:error).with(/Failed to close/, Exception, :trace).once
1655
+ @log.should_receive(:error).with(/Failed to close/, Exception, :trace).once
1634
1656
  @broker1.should_receive(:close).and_return(true).and_yield.once
1635
1657
  @broker2.should_receive(:close).and_raise(Exception).once
1636
1658
  @broker3.should_receive(:close).and_return(true).and_yield.once
@@ -95,6 +95,18 @@ describe AMQP::Client do
95
95
  end
96
96
  end
97
97
 
98
+ context 'and an EM reconnect failure' do
99
+ it 'should log an error and schedule another reconnect' do
100
+ @sut.settings[:reconnect_delay] = true
101
+
102
+ flexmock(RightScale::Log).should_receive(:error).with(/Exception caught during AMQP reconnect/, Exception, :trace).once
103
+ flexmock(EM).should_receive(:reconnect).and_raise(Exception)
104
+ flexmock(EM).should_receive(:add_timer).with(5, Proc).once
105
+
106
+ @sut.reconnect()
107
+ end
108
+ end
109
+
98
110
  end
99
111
 
100
112
  end
@@ -263,6 +263,7 @@ describe "Packet: Push" do
263
263
  packet.from.should == packet2.from
264
264
  packet.token.should == packet2.token
265
265
  packet.tries.should == packet2.tries
266
+ packet.confirm.should == packet2.confirm
266
267
  packet.expires_at.should == packet2.expires_at
267
268
  packet.recv_version.should == packet2.recv_version
268
269
  packet.send_version.should == packet2.send_version
@@ -276,6 +277,7 @@ describe "Packet: Push" do
276
277
  packet.from.should == packet2.from
277
278
  packet.token.should == packet2.token
278
279
  packet.tries.should == packet2.tries
280
+ packet.confirm.should == packet2.confirm
279
281
  packet.expires_at.should == packet2.expires_at
280
282
  packet.recv_version.should == packet2.recv_version
281
283
  packet.send_version.should == packet2.send_version
@@ -59,6 +59,8 @@ describe RightScale::Sender do
59
59
  describe "when monitoring broker connectivity" do
60
60
  before(:each) do
61
61
  flexmock(EM).should_receive(:next_tick).and_yield.by_default
62
+ @now = Time.at(1000000)
63
+ flexmock(Time).should_receive(:now).and_return(@now).by_default
62
64
  @broker = flexmock("Broker", :subscribe => true, :publish => ["broker"], :connected? => true,
63
65
  :identity_parts => ["host", 123, 0, 0, nil]).by_default
64
66
  @agent = flexmock("Agent", :identity => "agent", :broker => @broker, :options => {:ping_interval => 0}).by_default
@@ -79,7 +81,8 @@ describe RightScale::Sender do
79
81
  @agent.should_receive(:options).and_return(:ping_interval => 1000)
80
82
  flexmock(EM::Timer).should_receive(:new).with(1000, Proc).and_return(@timer).once
81
83
  instance = RightScale::Sender.new(@agent)
82
- flexmock(instance).should_receive(:restart_inactivity_timer).once
84
+ flexmock(Time).should_receive(:now).and_return(@now + 61)
85
+ flexmock(instance.connectivity_checker).should_receive(:restart_inactivity_timer).once
83
86
  instance.message_received
84
87
  instance.message_received
85
88
  end
@@ -89,11 +92,47 @@ describe RightScale::Sender do
89
92
  flexmock(EM::Timer).should_receive(:new).and_return(@timer).once.by_default
90
93
  RightScale::Sender.new(@agent)
91
94
  instance = RightScale::Sender.instance
95
+ flexmock(Time).should_receive(:now).and_return(@now + 61)
92
96
  flexmock(EM::Timer).should_receive(:new).and_return(@timer).and_yield.once
93
- flexmock(instance).should_receive(:check_connection).once
97
+ flexmock(instance.connectivity_checker).should_receive(:check).once
94
98
  instance.message_received
95
99
  end
96
100
 
101
+ it "should check connectivity by sending mapper ping" do
102
+ @agent.should_receive(:options).and_return(:ping_interval => 1000)
103
+ flexmock(EM::Timer).should_receive(:new).and_return(@timer).twice
104
+ RightScale::Sender.new(@agent)
105
+ instance = RightScale::Sender.instance
106
+ broker_id = "rs-broker-1-1"
107
+ flexmock(instance).should_receive(:publish).with(on do |request|
108
+ request.type.should == "/mapper/ping";
109
+ request.from.should == "agent"
110
+ end, [broker_id]).and_return([broker_id]).once
111
+ instance.connectivity_checker.check(broker_id)
112
+ instance.pending_requests.size.should == 1
113
+ end
114
+
115
+ it "should not check connectivity if terminating" do
116
+ @agent.should_receive(:options).and_return(:ping_interval => 1000)
117
+ flexmock(EM::Timer).should_receive(:new).and_return(@timer).once
118
+ RightScale::Sender.new(@agent)
119
+ instance = RightScale::Sender.instance
120
+ flexmock(instance).should_receive(:publish).never
121
+ instance.terminate
122
+ instance.connectivity_checker.check
123
+ end
124
+
125
+ it "should not check connectivity if not connected to broker" do
126
+ @agent.should_receive(:options).and_return(:ping_interval => 1000)
127
+ flexmock(EM::Timer).should_receive(:new).and_return(@timer).once
128
+ RightScale::Sender.new(@agent)
129
+ broker_id = "rs-broker-1-1"
130
+ @broker.should_receive(:connected?).with(broker_id).and_return(false)
131
+ instance = RightScale::Sender.instance
132
+ flexmock(instance).should_receive(:publish).never
133
+ instance.connectivity_checker.check(broker_id)
134
+ end
135
+
97
136
  it "should ignore messages received if ping disabled" do
98
137
  @agent.should_receive(:options).and_return(:ping_interval => 0)
99
138
  flexmock(EM::Timer).should_receive(:new).never
@@ -107,8 +146,9 @@ describe RightScale::Sender do
107
146
  flexmock(EM::Timer).should_receive(:new).and_return(@timer).once.by_default
108
147
  RightScale::Sender.new(@agent)
109
148
  instance = RightScale::Sender.instance
149
+ flexmock(Time).should_receive(:now).and_return(@now + 61)
110
150
  flexmock(EM::Timer).should_receive(:new).and_return(@timer).and_yield.once
111
- flexmock(instance).should_receive(:check_connection).and_raise(Exception)
151
+ flexmock(instance.connectivity_checker).should_receive(:check).and_raise(Exception)
112
152
  instance.message_received
113
153
  end
114
154
 
@@ -118,18 +158,18 @@ describe RightScale::Sender do
118
158
  broker_id = "rs-broker-localhost-5672"
119
159
  @broker.should_receive(:identity_parts).with(broker_id).and_return(["localhost", 5672, 0, 0, nil]).once
120
160
  @agent.should_receive(:connect).with("localhost", 5672, 0, 0, true).once
121
- old_ping_timeout = RightScale::Sender::PING_TIMEOUT
161
+ old_ping_timeout = RightScale::Sender::ConnectivityChecker::PING_TIMEOUT
122
162
  begin
123
- RightScale::Sender.const_set(:PING_TIMEOUT, 0.5)
163
+ RightScale::Sender::ConnectivityChecker.const_set(:PING_TIMEOUT, 0.5)
124
164
  EM.run do
125
165
  EM.add_timer(1) { EM.stop }
126
166
  RightScale::Sender.new(@agent)
127
167
  instance = RightScale::Sender.instance
128
168
  flexmock(instance).should_receive(:publish).with(RightScale::Request, nil).and_return([broker_id])
129
- instance.__send__(:check_connection)
169
+ instance.connectivity_checker.check
130
170
  end
131
171
  ensure
132
- RightScale::Sender.const_set(:PING_TIMEOUT, old_ping_timeout)
172
+ RightScale::Sender::ConnectivityChecker.const_set(:PING_TIMEOUT, old_ping_timeout)
133
173
  end
134
174
  end
135
175
  end
@@ -138,6 +178,7 @@ describe RightScale::Sender do
138
178
  before(:each) do
139
179
  @timer = flexmock("timer")
140
180
  flexmock(EM::Timer).should_receive(:new).and_return(@timer)
181
+ flexmock(Time).should_receive(:now).and_return(Time.at(1000000)).by_default
141
182
  @broker = flexmock("Broker", :subscribe => true, :publish => true).by_default
142
183
  @agent = flexmock("Agent", :identity => "agent", :broker => @broker).by_default
143
184
  @agent.should_receive(:options).and_return({}).by_default
@@ -205,6 +246,7 @@ describe RightScale::Sender do
205
246
  push.persistent.should be_false
206
247
  push.from.should == 'agent'
207
248
  push.target.should be_nil
249
+ push.confirm.should be_nil
208
250
  push.expires_at.should == 0
209
251
  end, hsh(:persistent => false, :mandatory => true)).once
210
252
  @instance.send_push('/welcome/aboard', 'iZac')
@@ -217,27 +259,40 @@ describe RightScale::Sender do
217
259
  @instance.initialize_offline_queue
218
260
  @broker.should_receive(:publish).never
219
261
  @instance.enable_offline_mode
220
- @instance.instance_variable_get(:@queueing_mode).should == :offline
262
+ @instance.offline_handler.mode.should == :offline
221
263
  @instance.send_push('/welcome/aboard', 'iZac')
222
- @instance.instance_variable_get(:@queue).size.should == 1
264
+ @instance.offline_handler.queue.size.should == 1
223
265
  end
224
266
 
225
267
  it "should store the response handler if given" do
226
268
  response_handler = lambda {}
227
269
  flexmock(RightScale::AgentIdentity).should_receive(:generate).and_return('abc').once
270
+ @broker.should_receive(:publish).with(hsh(:name => "request"), on do |push|
271
+ push.confirm.should == true
272
+ end, hsh(:persistent => false, :mandatory => true)).once
228
273
  @instance.send_push('/welcome/aboard', 'iZac', &response_handler)
229
- @instance.pending_requests['abc'][:response_handler].should == response_handler
274
+ @instance.pending_requests['abc'].response_handler.should == response_handler
230
275
  end
231
276
 
232
277
  it "should store the request receive time if there is a response handler" do
233
278
  response_handler = lambda {}
234
279
  flexmock(RightScale::AgentIdentity).should_receive(:generate).and_return('abc').once
235
- flexmock(Time).should_receive(:now).and_return(Time.at(1000000)).by_default
236
- @instance.request_age.should be_nil
280
+ @instance.pending_requests.kind(RightScale::Sender::PendingRequests::PUSH_KINDS).youngest_age.should be_nil
237
281
  @instance.send_push('/welcome/aboard', 'iZac', &response_handler)
238
- @instance.pending_requests['abc'][:receive_time].should == Time.at(1000000)
282
+ @instance.pending_requests['abc'].receive_time.should == Time.at(1000000)
239
283
  flexmock(Time).should_receive(:now).and_return(Time.at(1000100))
240
- @instance.request_age.should == 100
284
+ @instance.pending_requests.kind(RightScale::Sender::PendingRequests::PUSH_KINDS).youngest_age.should == 100
285
+ end
286
+
287
+ it "should eventually remove push from pending requests if no response received" do
288
+ response_handler = lambda {}
289
+ flexmock(RightScale::AgentIdentity).should_receive(:generate).and_return('abc', 'xyz').twice
290
+ @instance.send_push('/welcome/aboard', 'iZac', &response_handler)
291
+ @instance.pending_requests['abc'].should_not be_nil
292
+ flexmock(Time).should_receive(:now).and_return(Time.at(1000121))
293
+ @instance.send_push('/welcome/aboard', 'iZac', &response_handler)
294
+ @instance.pending_requests['xyz'].should_not be_nil
295
+ @instance.pending_requests['abc'].should be_nil
241
296
  end
242
297
  end
243
298
 
@@ -266,6 +321,7 @@ describe RightScale::Sender do
266
321
  push.persistent.should be_true
267
322
  push.from.should == 'agent'
268
323
  push.target.should be_nil
324
+ push.confirm.should be_nil
269
325
  push.expires_at.should == 0
270
326
  end, hsh(:persistent => true, :mandatory => true)).once
271
327
  @instance.send_persistent_push('/welcome/aboard', 'iZac')
@@ -282,19 +338,22 @@ describe RightScale::Sender do
282
338
  it "should store the response handler if given" do
283
339
  response_handler = lambda {}
284
340
  flexmock(RightScale::AgentIdentity).should_receive(:generate).and_return('abc').once
341
+ @broker.should_receive(:publish).with(hsh(:name => "request"), on do |push|
342
+ push.confirm.should == true
343
+ end, hsh(:persistent => true, :mandatory => true)).once
285
344
  @instance.send_persistent_push('/welcome/aboard', 'iZac', &response_handler)
286
- @instance.pending_requests['abc'][:response_handler].should == response_handler
345
+ @instance.pending_requests['abc'].response_handler.should == response_handler
287
346
  end
288
347
 
289
348
  it "should store the request receive time if there is a response handler" do
290
349
  response_handler = lambda {}
291
350
  flexmock(RightScale::AgentIdentity).should_receive(:generate).and_return('abc').once
292
351
  flexmock(Time).should_receive(:now).and_return(Time.at(1000000)).by_default
293
- @instance.request_age.should be_nil
352
+ @instance.pending_requests.kind(RightScale::Sender::PendingRequests::PUSH_KINDS).youngest_age.should be_nil
294
353
  @instance.send_persistent_push('/welcome/aboard', 'iZac', &response_handler)
295
- @instance.pending_requests['abc'][:receive_time].should == Time.at(1000000)
354
+ @instance.pending_requests['abc'].receive_time.should == Time.at(1000000)
296
355
  flexmock(Time).should_receive(:now).and_return(Time.at(1000100))
297
- @instance.request_age.should == 100
356
+ @instance.pending_requests.kind(RightScale::Sender::PendingRequests::PUSH_KINDS).youngest_age.should == 100
298
357
  end
299
358
  end
300
359
 
@@ -396,17 +455,17 @@ describe RightScale::Sender do
396
455
  response_handler = lambda {}
397
456
  flexmock(RightScale::AgentIdentity).should_receive(:generate).and_return('abc').once
398
457
  @instance.send_retryable_request('/welcome/aboard', 'iZac', &response_handler)
399
- @instance.pending_requests['abc'][:response_handler].should == response_handler
458
+ @instance.pending_requests['abc'].response_handler.should == response_handler
400
459
  end
401
460
 
402
461
  it "should store the request receive time" do
403
462
  flexmock(RightScale::AgentIdentity).should_receive(:generate).and_return('abc').once
404
463
  flexmock(Time).should_receive(:now).and_return(Time.at(1000000)).by_default
405
- @instance.request_age.should be_nil
464
+ @instance.pending_requests.kind(RightScale::Sender::PendingRequests::REQUEST_KINDS).youngest_age.should be_nil
406
465
  @instance.send_retryable_request('/welcome/aboard', 'iZac')
407
- @instance.pending_requests['abc'][:receive_time].should == Time.at(1000000)
466
+ @instance.pending_requests['abc'].receive_time.should == Time.at(1000000)
408
467
  flexmock(Time).should_receive(:now).and_return(Time.at(1000100))
409
- @instance.request_age.should == 100
468
+ @instance.pending_requests.kind(RightScale::Sender::PendingRequests::REQUEST_KINDS).youngest_age.should == 100
410
469
  end
411
470
 
412
471
  it 'should queue the request if in offline mode and :offline_queueing enabled' do
@@ -416,9 +475,9 @@ describe RightScale::Sender do
416
475
  @instance.initialize_offline_queue
417
476
  @broker.should_receive(:publish).never
418
477
  @instance.enable_offline_mode
419
- @instance.instance_variable_get(:@queueing_mode).should == :offline
478
+ @instance.offline_handler.mode.should == :offline
420
479
  @instance.send_retryable_request('/welcome/aboard', 'iZac')
421
- @instance.instance_variable_get(:@queue).size.should == 1
480
+ @instance.offline_handler.queue.size.should == 1
422
481
  end
423
482
 
424
483
  it "should dump the pending requests" do
@@ -489,7 +548,7 @@ describe RightScale::Sender do
489
548
  @agent.should_receive(:options).and_return({:retry_timeout => 0.3, :retry_interval => 0.1})
490
549
  RightScale::Sender.new(@agent)
491
550
  @instance = RightScale::Sender.instance
492
- flexmock(@instance).should_receive(:check_connection).once
551
+ flexmock(@instance.connectivity_checker).should_receive(:check).once
493
552
  @broker.should_receive(:publish).and_return(@broker_ids).twice
494
553
  @instance.send_retryable_request('/welcome/aboard', 'iZac') do |response|
495
554
  result = RightScale::OperationResult.from_results(response)
@@ -514,7 +573,7 @@ describe RightScale::Sender do
514
573
  @agent.should_receive(:options).and_return({:retry_timeout => 0.6, :retry_interval => 0.1})
515
574
  RightScale::Sender.new(@agent)
516
575
  @instance = RightScale::Sender.instance
517
- flexmock(@instance).should_receive(:check_connection).once
576
+ flexmock(@instance.connectivity_checker).should_receive(:check).once
518
577
  @broker.should_receive(:publish).and_return(@broker_ids).times(3)
519
578
  @instance.send_retryable_request('/welcome/aboard', 'iZac') do |response|
520
579
  result = RightScale::OperationResult.from_results(response)
@@ -537,7 +596,7 @@ describe RightScale::Sender do
537
596
  @agent.should_receive(:options).and_return({:retry_timeout => 0.5, :retry_interval => 0.1})
538
597
  RightScale::Sender.new(@agent)
539
598
  @instance = RightScale::Sender.instance
540
- flexmock(@instance).should_receive(:check_connection).once
599
+ flexmock(@instance.connectivity_checker).should_receive(:check).once
541
600
  @broker.should_receive(:publish).with(hsh(:name => "request"), on do |request|
542
601
  request.expires_at.should == (expires_at ||= request.expires_at)
543
602
  end, hsh(:persistent => false, :mandatory => true)).and_return(@broker_ids).twice
@@ -554,16 +613,16 @@ describe RightScale::Sender do
554
613
 
555
614
  it "should not check connection if check already in progress" do
556
615
  flexmock(EM::Timer).should_receive(:new).and_return(@timer).never
557
- @instance.pending_ping = true
616
+ @instance.connectivity_checker.ping_timer = true
558
617
  flexmock(@instance).should_receive(:publish).never
559
- @instance.__send__(:check_connection, @broker_ids)
618
+ @instance.connectivity_checker.check(@broker_ids)
560
619
  end
561
620
 
562
621
  it "should publish ping to mapper" do
563
622
  flexmock(EM::Timer).should_receive(:new).and_return(@timer).once
564
623
  flexmock(@instance).should_receive(:publish).with(on { |request| request.type.should == "/mapper/ping" },
565
624
  @broker_ids).and_return(@broker_ids).once
566
- @instance.__send__(:check_connection, @broker_id)
625
+ @instance.connectivity_checker.check(@broker_id)
567
626
  @instance.pending_requests.size.should == 1
568
627
  end
569
628
 
@@ -572,19 +631,19 @@ describe RightScale::Sender do
572
631
  @timer.should_receive(:cancel).once
573
632
  flexmock(EM::Timer).should_receive(:new).and_return(@timer).once
574
633
  flexmock(@instance).should_receive(:publish).and_return(@broker_ids).once
575
- @instance.__send__(:check_connection, @broker_id)
576
- @instance.pending_ping.should == @timer
634
+ @instance.connectivity_checker.check(@broker_id)
635
+ @instance.connectivity_checker.ping_timer.should == @timer
577
636
  @instance.pending_requests.size.should == 1
578
- @instance.pending_requests['abc'][:response_handler].call(nil)
579
- @instance.pending_ping.should == nil
637
+ @instance.pending_requests['abc'].response_handler.call(nil)
638
+ @instance.connectivity_checker.ping_timer.should == nil
580
639
  end
581
640
 
582
641
  it "should try to reconnect if ping times out" do
583
642
  @log.should_receive(:warning).once
584
643
  flexmock(EM::Timer).should_receive(:new).and_yield.once
585
644
  flexmock(@agent).should_receive(:connect).once
586
- @instance.__send__(:check_connection, @broker_id)
587
- @instance.pending_ping.should == nil
645
+ @instance.connectivity_checker.check(@broker_id)
646
+ @instance.connectivity_checker.ping_timer.should == nil
588
647
  end
589
648
 
590
649
  it "should log error if attempt to reconnect fails" do
@@ -592,7 +651,7 @@ describe RightScale::Sender do
592
651
  @log.should_receive(:error).with(/Failed to reconnect/, Exception, :trace).once
593
652
  flexmock(@agent).should_receive(:connect).and_raise(Exception)
594
653
  flexmock(EM::Timer).should_receive(:new).and_yield.once
595
- @instance.__send__(:check_connection, @broker_id)
654
+ @instance.connectivity_checker.check(@broker_id)
596
655
  end
597
656
  end
598
657
  end
@@ -668,10 +727,17 @@ describe RightScale::Sender do
668
727
  flexmock(RightScale::AgentIdentity, :generate => 'token1')
669
728
  end
670
729
 
671
- it "should deliver the response" do
730
+ it "should deliver the response for a Request" do
672
731
  @instance.send_retryable_request('/welcome/aboard', 'iZac') {|_|}
673
732
  response = RightScale::Result.new('token1', 'to', RightScale::OperationResult.success, 'target1')
674
- flexmock(@instance).should_receive(:deliver).with(response, Hash).once
733
+ flexmock(@instance).should_receive(:deliver).with(response, RightScale::Sender::PendingRequest).once
734
+ @instance.handle_response(response)
735
+ end
736
+
737
+ it "should deliver the response for a Push" do
738
+ @instance.send_push('/welcome/aboard', 'iZac') {|_|}
739
+ response = RightScale::Result.new('token1', 'to', RightScale::OperationResult.success, 'target1')
740
+ flexmock(@instance).should_receive(:deliver).with(response, RightScale::Sender::PendingRequest).once
675
741
  @instance.handle_response(response)
676
742
  end
677
743
 
@@ -691,7 +757,7 @@ describe RightScale::Sender do
691
757
  non_delivery = RightScale::OperationResult.non_delivery(RightScale::OperationResult::NO_ROUTE_TO_TARGET)
692
758
  response = RightScale::Result.new('token1', 'to', non_delivery, 'target1')
693
759
  @instance.handle_response(response)
694
- @instance.instance_variable_get(:@non_deliveries).total.should == 1
760
+ @instance.instance_variable_get(:@non_delivery_stats).total.should == 1
695
761
  end
696
762
 
697
763
  it "should log non-delivery if there is no response handler" do
@@ -724,7 +790,7 @@ describe RightScale::Sender do
724
790
  flexmock(RightScale::AgentIdentity, :generate => 'token1')
725
791
  end
726
792
 
727
- it "should delete all associated pending requests" do
793
+ it "should delete all associated pending Request requests" do
728
794
  @instance.send_retryable_request('/welcome/aboard', 'iZac') {|_|}
729
795
  @instance.pending_requests['token1'].should_not be_nil
730
796
  response = RightScale::Result.new('token1', 'to', RightScale::OperationResult.success, 'target1')
@@ -732,11 +798,19 @@ describe RightScale::Sender do
732
798
  @instance.pending_requests['token1'].should be_nil
733
799
  end
734
800
 
801
+ it "should not delete any pending Push requests" do
802
+ @instance.send_push('/welcome/aboard', 'iZac') {|_|}
803
+ @instance.pending_requests['token1'].should_not be_nil
804
+ response = RightScale::Result.new('token1', 'to', RightScale::OperationResult.success, 'target1')
805
+ @instance.handle_response(response)
806
+ @instance.pending_requests['token1'].should_not be_nil
807
+ end
808
+
735
809
  it "should delete any associated retry requests" do
736
810
  @instance.send_retryable_request('/welcome/aboard', 'iZac') {|_|}
737
811
  @instance.pending_requests['token1'].should_not be_nil
738
812
  @instance.pending_requests['token2'] = @instance.pending_requests['token1'].dup
739
- @instance.pending_requests['token2'][:retry_parent] = 'token1'
813
+ @instance.pending_requests['token2'].retry_parent = 'token1'
740
814
  response = RightScale::Result.new('token2', 'to', RightScale::OperationResult.success, 'target1')
741
815
  @instance.handle_response(response)
742
816
  @instance.pending_requests['token1'].should be_nil
@@ -799,52 +873,52 @@ describe RightScale::Sender do
799
873
  end
800
874
 
801
875
  it 'should vote for restart after the maximum number of queued requests is reached' do
802
- @instance.instance_variable_get(:@restart_vote_count).should == 0
876
+ @instance.offline_handler.instance_variable_get(:@restart_vote_count).should == 0
803
877
  EM.run do
804
878
  @instance.enable_offline_mode
805
- @instance.instance_variable_set(:@queue, ('*' * (RightScale::Sender::MAX_QUEUED_REQUESTS - 1)).split(//))
879
+ @instance.offline_handler.queue = ('*' * (RightScale::Sender::OfflineHandler::MAX_QUEUED_REQUESTS - 1)).split(//)
806
880
  @instance.send_push('/dummy', 'payload')
807
881
  EM.next_tick { EM.stop }
808
882
  end
809
- @instance.instance_variable_get(:@queue).size.should == RightScale::Sender::MAX_QUEUED_REQUESTS
810
- @instance.instance_variable_get(:@restart_vote_count).should == 1
883
+ @instance.offline_handler.queue.size.should == RightScale::Sender::OfflineHandler::MAX_QUEUED_REQUESTS
884
+ @instance.offline_handler.instance_variable_get(:@restart_vote_count).should == 1
811
885
  end
812
886
 
813
887
  it 'should vote for restart after the threshold delay is reached' do
814
- old_vote_delay = RightScale::Sender::RESTART_VOTE_DELAY
888
+ old_vote_delay = RightScale::Sender::OfflineHandler::RESTART_VOTE_DELAY
815
889
  begin
816
- RightScale::Sender.const_set(:RESTART_VOTE_DELAY, 0.1)
817
- @instance.instance_variable_get(:@restart_vote_count).should == 0
890
+ RightScale::Sender::OfflineHandler.const_set(:RESTART_VOTE_DELAY, 0.1)
891
+ @instance.offline_handler.instance_variable_get(:@restart_vote_count).should == 0
818
892
  EM.run do
819
893
  @instance.enable_offline_mode
820
894
  @instance.send_push('/dummy', 'payload')
821
895
  EM.add_timer(0.5) { EM.stop }
822
896
  end
823
- @instance.instance_variable_get(:@restart_vote_count).should == 1
897
+ @instance.offline_handler.instance_variable_get(:@restart_vote_count).should == 1
824
898
  ensure
825
- RightScale::Sender.const_set(:RESTART_VOTE_DELAY, old_vote_delay)
899
+ RightScale::Sender::OfflineHandler.const_set(:RESTART_VOTE_DELAY, old_vote_delay)
826
900
  end
827
901
  end
828
902
 
829
903
  it 'should not flush queued requests until back online' do
830
- old_flush_delay = RightScale::Sender::MAX_QUEUE_FLUSH_DELAY
904
+ old_flush_delay = RightScale::Sender::OfflineHandler::MAX_QUEUE_FLUSH_DELAY
831
905
  begin
832
- RightScale::Sender.const_set(:MAX_QUEUE_FLUSH_DELAY, 0.1)
906
+ RightScale::Sender::OfflineHandler.const_set(:MAX_QUEUE_FLUSH_DELAY, 0.1)
833
907
  EM.run do
834
908
  @instance.enable_offline_mode
835
909
  @instance.send_push('/dummy', 'payload')
836
910
  EM.add_timer(0.5) { EM.stop }
837
911
  end
838
912
  ensure
839
- RightScale::Sender.const_set(:MAX_QUEUE_FLUSH_DELAY, old_flush_delay)
913
+ RightScale::Sender::OfflineHandler.const_set(:MAX_QUEUE_FLUSH_DELAY, old_flush_delay)
840
914
  end
841
915
  end
842
916
 
843
917
  it 'should flush queued requests once back online' do
844
- old_flush_delay = RightScale::Sender::MAX_QUEUE_FLUSH_DELAY
918
+ old_flush_delay = RightScale::Sender::OfflineHandler::MAX_QUEUE_FLUSH_DELAY
845
919
  @broker.should_receive(:publish).once.and_return { EM.stop }
846
920
  begin
847
- RightScale::Sender.const_set(:MAX_QUEUE_FLUSH_DELAY, 0.1)
921
+ RightScale::Sender::OfflineHandler.const_set(:MAX_QUEUE_FLUSH_DELAY, 0.1)
848
922
  EM.run do
849
923
  @instance.enable_offline_mode
850
924
  @instance.send_push('/dummy', 'payload')
@@ -852,34 +926,31 @@ describe RightScale::Sender do
852
926
  EM.add_timer(1) { EM.stop }
853
927
  end
854
928
  ensure
855
- RightScale::Sender.const_set(:MAX_QUEUE_FLUSH_DELAY, old_flush_delay)
929
+ RightScale::Sender::OfflineHandler.const_set(:MAX_QUEUE_FLUSH_DELAY, old_flush_delay)
856
930
  end
857
931
  end
858
932
 
859
933
  it 'should stop flushing when going back to offline mode' do
860
- old_flush_delay = RightScale::Sender::MAX_QUEUE_FLUSH_DELAY
934
+ old_flush_delay = RightScale::Sender::OfflineHandler::MAX_QUEUE_FLUSH_DELAY
861
935
  begin
862
- RightScale::Sender.const_set(:MAX_QUEUE_FLUSH_DELAY, 0.1)
936
+ RightScale::Sender::OfflineHandler.const_set(:MAX_QUEUE_FLUSH_DELAY, 0.1)
863
937
  EM.run do
864
938
  @instance.enable_offline_mode
865
939
  @instance.send_push('/dummy', 'payload')
866
940
  @instance.disable_offline_mode
867
- @instance.instance_variable_get(:@flushing_queue).should be_true
868
- @instance.instance_variable_get(:@stop_flushing_queue).should be_false
869
- @instance.instance_variable_get(:@queueing_mode).should == :offline
941
+ @instance.offline_handler.state.should == :flushing
942
+ @instance.offline_handler.mode.should == :offline
870
943
  @instance.enable_offline_mode
871
- @instance.instance_variable_get(:@flushing_queue).should be_true
872
- @instance.instance_variable_get(:@stop_flushing_queue).should be_true
873
- @instance.instance_variable_get(:@queueing_mode).should == :offline
944
+ @instance.offline_handler.state.should == :running
945
+ @instance.offline_handler.mode.should == :offline
874
946
  EM.add_timer(1) do
875
- @instance.instance_variable_get(:@flushing_queue).should be_false
876
- @instance.instance_variable_get(:@stop_flushing_queue).should be_false
877
- @instance.instance_variable_get(:@queueing_mode).should == :offline
947
+ @instance.offline_handler.state.should == :running
948
+ @instance.offline_handler.mode.should == :offline
878
949
  EM.stop
879
950
  end
880
951
  end
881
952
  ensure
882
- RightScale::Sender.const_set(:MAX_QUEUE_FLUSH_DELAY, old_flush_delay)
953
+ RightScale::Sender::OfflineHandler.const_set(:MAX_QUEUE_FLUSH_DELAY, old_flush_delay)
883
954
  end
884
955
  end
885
956
  end