File tree Expand file tree Collapse file tree 2 files changed +28
-0
lines changed
Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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
4061end
You can’t perform that action at this time.
0 commit comments