brontes3d-amqp 0.6.4.1 → 0.6.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/amqp.gemspec +1 -1
  2. data/bacon/client.rb +48 -0
  3. data/lib/amqp/client.rb +13 -2
  4. metadata +2 -3
@@ -1,6 +1,6 @@
1
1
  spec = Gem::Specification.new do |s|
2
2
  s.name = 'amqp'
3
- s.version = '0.6.4.1'
3
+ s.version = '0.6.4.2'
4
4
  s.date = '2009-01-09'
5
5
  s.summary = 'AMQP client implementation in Ruby/EventMachine'
6
6
  s.email = "amqp@tmm1.net"
@@ -13,6 +13,7 @@ describe Client do
13
13
  AMQP.instance_eval{ @conn = nil }
14
14
  AMQP.instance_eval{ @closing = false }
15
15
  Client.class_eval{ @retry_count = 0 }
16
+ Client.class_eval{ @server_to_select = 0 }
16
17
  end
17
18
 
18
19
  should 'reconnect on disconnect after connection_completed (use reconnect_timer)' do
@@ -229,5 +230,52 @@ describe Client do
229
230
  @re_connect_args.should == [["otherhost", 5672]]
230
231
  end
231
232
 
233
+ should "respect max_retry if the disconnect happens before connection completes" do
234
+ @times_connected = 0
235
+
236
+ EventMachine.stubs(:connect_server).returns(99).with do |arg1, arg2|
237
+ @times_connected += 1
238
+ EM.next_tick do
239
+ @client = EM.class_eval{ @conns }[99]
240
+ @client.stubs(:send_data).returns(true)
241
+ EM.class_eval{ @conns.delete(99) }
242
+ @client.unbind
243
+ end
244
+ true
245
+ end
246
+
247
+ EM.next_tick{ EM.add_timer(0.5){ EM.stop_event_loop } }
248
+
249
+ #connect
250
+ lambda{
251
+ AMQP.start(:host => 'nonexistanthost', :reconnect_timer => 0.01, :max_retry => 6)
252
+ }.should.raise(RuntimeError)
253
+ # puts "\nreconnected #{@times_connected} times"
254
+ @times_connected.should == 7
255
+ end
256
+
257
+ should "reset connection count if the disconnect happens after connection completes" do
258
+ @times_connected = 0
259
+
260
+ EventMachine.stubs(:connect_server).returns(99).with do |arg1, arg2|
261
+ @times_connected += 1
262
+ EM.next_tick do
263
+ @client = EM.class_eval{ @conns }[99]
264
+ @client.stubs(:send_data).returns(true)
265
+ @client.connection_completed
266
+ EM.class_eval{ @conns.delete(99) }
267
+ @client.unbind
268
+ end
269
+ true
270
+ end
271
+
272
+ EM.next_tick{ EM.add_timer(0.5){ EM.stop_event_loop } }
273
+
274
+ #connect
275
+ AMQP.start(:host => 'nonexistanthost', :reconnect_timer => 0.01, :max_retry => 6)
276
+ # puts "\nreconnected #{@times_connected} times"
277
+ @times_connected.should > 7
278
+ end
279
+
232
280
 
233
281
  end
@@ -85,6 +85,7 @@ module AMQP
85
85
  log 'connected'
86
86
  # @on_disconnect = proc{ raise Error, 'Disconnected from server' }
87
87
  unless @closing
88
+ Client.connection_succeeded!
88
89
  @on_disconnect = method(:disconnected)
89
90
  end
90
91
 
@@ -209,17 +210,26 @@ module AMQP
209
210
  try_host = opts[:host]
210
211
  try_port = opts[:port]
211
212
  @retry_count ||= 0
213
+ if opts[:max_retry] && @retry_count >= opts[:max_retry]
214
+ raise "max_retry (#{@retry_count}) reached, disconnecting"
215
+ end
212
216
  if srv_list = opts[:fallback_servers]
213
- idex = @retry_count % (srv_list.size + 1)
217
+ @server_to_select ||= 0
218
+ idex = @server_to_select % (srv_list.size + 1)
214
219
  if idex != 0
215
220
  try = srv_list[idex - 1]
216
221
  try_host = try[:host] || AMQP.settings[:host]
217
222
  try_port = try[:port] || AMQP.settings[:port]
218
223
  end
219
- end
224
+ @server_to_select += 1
225
+ end
220
226
  @retry_count += 1
221
227
  [try_host, try_port]
222
228
  end
229
+
230
+ def self.connection_succeeded!
231
+ @retry_count = 0
232
+ end
223
233
 
224
234
  def self.connect opts = {}
225
235
  opts = AMQP.settings.merge(opts)
@@ -227,6 +237,7 @@ module AMQP
227
237
  begin
228
238
  (try_host, try_port) = determine_reconnect_server(opts)
229
239
  EM.connect try_host, try_port, self, opts
240
+ connection_succeeded!
230
241
  rescue RuntimeError => e
231
242
  STDERR.puts "'#{e.message}' on connect to #{try_host}:#{try_port}"
232
243
  retry if e.message == "no connection" && @retry_count < max_retry
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brontes3d-amqp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4.1
4
+ version: 0.6.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aman Gupta
@@ -94,7 +94,6 @@ files:
94
94
  - bacon/client.rb
95
95
  has_rdoc: true
96
96
  homepage: http://amqp.rubyforge.org/
97
- licenses:
98
97
  post_install_message:
99
98
  rdoc_options:
100
99
  - --include=examples
@@ -115,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
114
  requirements: []
116
115
 
117
116
  rubyforge_project:
118
- rubygems_version: 1.3.5
117
+ rubygems_version: 1.2.0
119
118
  signing_key:
120
119
  specification_version: 2
121
120
  summary: AMQP client implementation in Ruby/EventMachine