Skip to content

Commit 1691152

Browse files
committed
add with_connection to connection pool
1 parent 5efd34b commit 1691152

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/resque/connection_pool.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ def checkin(conn)
3535
end
3636
end
3737

38+
def with_connection
39+
conn = checkout
40+
yield(conn)
41+
ensure
42+
checkin(conn)
43+
end
44+
3845
private
3946
def checked_out_conns
4047
@conns.find_all {|k, v| v }.map(&:first)

test/connection_pool_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,26 @@ module Resque
3636

3737
assert_equal conn, t.join.value
3838
end
39+
40+
it "yields a connection to a block" do
41+
cp = ConnectionPool.new(REDIS_URL, 1)
42+
c = nil
43+
cp.with_connection do |conn|
44+
c = conn
45+
end
46+
assert_equal c, cp.checkout
47+
end
48+
49+
it 'checks in the connection if there is an exception' do
50+
cp = ConnectionPool.new(REDIS_URL, 1)
51+
c = nil
52+
assert_raises(RuntimeError) do
53+
cp.with_connection do |conn|
54+
c = conn
55+
raise
56+
end
57+
end
58+
assert_equal c, cp.checkout
59+
end
3960
end
4061
end

0 commit comments

Comments
 (0)