gene_pool 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  doc
2
2
  pkg
3
+ .idea
data/README.rdoc CHANGED
@@ -30,6 +30,13 @@ Generic pooling library for connection pools.
30
30
  # use socket here
31
31
  end
32
32
  end
33
+
34
+ def send_message_auto_retry
35
+ # On exception, close and reopen socket and perform retry
36
+ @@gene_pool.with_connection_auto_retry do |socket|
37
+ # use socket here,
38
+ end
39
+ end
33
40
  end
34
41
 
35
42
  == Copyright
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.1.0
data/gene_pool.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{gene_pool}
8
- s.version = "1.0.1"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brad Pardee"]
12
- s.date = %q{2010-09-12}
12
+ s.date = %q{2010-11-11}
13
13
  s.description = %q{Generic pooling library for creating a connection pool}
14
14
  s.email = %q{bradpardee@gmail.com}
15
15
  s.extra_rdoc_files = [
data/lib/gene_pool.rb CHANGED
@@ -88,6 +88,33 @@ class GenePool
88
88
  end
89
89
  end
90
90
 
91
+ # Create a scope for checking out a connection while automatically retrying on exception
92
+ def with_connection_auto_retry(close_on_error = true)
93
+ with_connection do |connection|
94
+ begin
95
+ yield connection
96
+ rescue Exception => e
97
+ if close_on_error
98
+ connection.close rescue nil
99
+ end
100
+ if (e.kind_of?(Timeout::Error))
101
+ remove(connection)
102
+ raise
103
+ end
104
+ connection = renew(connection)
105
+ begin
106
+ yield connection
107
+ rescue Exception => e
108
+ if close_on_error
109
+ connection.close rescue nil
110
+ end
111
+ remove(connection)
112
+ raise
113
+ end
114
+ end
115
+ end
116
+ end
117
+
91
118
  # Remove an existing connection from the pool
92
119
  def remove(connection)
93
120
  @mutex.synchronize do
@@ -15,6 +15,7 @@ class DummyConnection
15
15
  def initialize(count, sleep_time=nil)
16
16
  sleep sleep_time if sleep_time
17
17
  @count = count
18
+ @closed = false
18
19
  end
19
20
 
20
21
  def to_i
@@ -24,6 +25,18 @@ class DummyConnection
24
25
  def to_s
25
26
  @count.to_s
26
27
  end
28
+
29
+ def fail_on(*counts)
30
+ raise Exception.new("Dummy exception on count #{@count}") if counts.include?(@count)
31
+ end
32
+
33
+ def close
34
+ @closed = true
35
+ end
36
+
37
+ def closed?
38
+ @closed
39
+ end
27
40
  end
28
41
 
29
42
 
@@ -228,5 +241,53 @@ class GenePoolTest < Test::Unit::TestCase
228
241
  ival_conns.sort!
229
242
  assert_equal (1..pool_size).to_a, ival_conns
230
243
  end
244
+
245
+ should 'be able to auto-retry connection' do
246
+ i = 1
247
+ [false, true].each do |stat|
248
+ conns = []
249
+ @gene_pool.with_connection_auto_retry(stat) do |conn|
250
+ conns << conn
251
+ conn.fail_on(i)
252
+ assert_equal 1, @gene_pool.connections.size
253
+ assert_equal 1, @gene_pool.checked_out.size
254
+ assert_equal i+1, conn.to_i
255
+ end
256
+ assert_equal stat, conns[0].closed?
257
+ assert !conns[1].closed?
258
+ i += 1
259
+ end
260
+ assert_equal 1, @gene_pool.connections.size
261
+ assert_equal 0, @gene_pool.checked_out.size
262
+ end
263
+
264
+ should 'fail with auto-retry on double failure' do
265
+ e = assert_raises Exception do
266
+ @gene_pool.with_connection_auto_retry(false) do |conn|
267
+ conn.fail_on(1,2)
268
+ end
269
+ end
270
+ assert_match %r%Dummy exception%, e.message
271
+ assert_equal 0, @gene_pool.connections.size
272
+ assert_equal 0, @gene_pool.checked_out.size
273
+ end
274
+
275
+ should 'not auto-retry on timeout' do
276
+ @sleep = 2
277
+ @timeout = 1
278
+ e = assert_raises Timeout::Error do
279
+ @gene_pool.with_connection_auto_retry do |conn|
280
+ end
281
+ end
282
+ assert_equal 0, @gene_pool.connections.size
283
+ assert_equal 0, @gene_pool.checked_out.size
284
+ @sleep = 0
285
+ @gene_pool.with_connection_auto_retry do |conn|
286
+ assert_equal 2, conn.to_i
287
+ assert_equal 1, @gene_pool.checked_out.size
288
+ end
289
+ assert_equal 1, @gene_pool.connections.size
290
+ assert_equal 0, @gene_pool.checked_out.size
291
+ end
231
292
  end
232
293
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
- - 0
8
7
  - 1
9
- version: 1.0.1
8
+ - 0
9
+ version: 1.1.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brad Pardee
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-12 00:00:00 -04:00
17
+ date: 2010-11-11 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20