Make DB::Pool accept a block

This commit is contained in:
Ary Borenszweig 2016-07-11 15:38:31 -03:00
parent fff67e86a0
commit 1cce4685ff
3 changed files with 12 additions and 12 deletions

View file

@ -50,17 +50,17 @@ end
describe DB::Pool do
it "should use proc to create objects" do
block_called = 0
pool = DB::Pool.new(->{ block_called += 1; Closable.new }, initial_pool_size: 3)
pool = DB::Pool.new(initial_pool_size: 3) { block_called += 1; Closable.new }
block_called.should eq(3)
end
it "should get resource" do
pool = DB::Pool.new(->{ Closable.new })
pool = DB::Pool.new { Closable.new }
pool.checkout.should be_a Closable
end
it "should wait for available resource" do
pool = DB::Pool.new(->{ Closable.new }, max_pool_size: 1, initial_pool_size: 1)
pool = DB::Pool.new(max_pool_size: 1, initial_pool_size: 1) { Closable.new }
b_cnn_request = ShouldSleepingOp.new
wait_a = WaitFor.new
@ -88,7 +88,7 @@ describe DB::Pool do
it "should create new if max was not reached" do
block_called = 0
pool = DB::Pool.new(->{ block_called += 1; Closable.new }, max_pool_size: 2, initial_pool_size: 1)
pool = DB::Pool.new(max_pool_size: 2, initial_pool_size: 1) { block_called += 1; Closable.new }
block_called.should eq 1
pool.checkout
block_called.should eq 1
@ -98,7 +98,7 @@ describe DB::Pool do
it "should reuse returned resources" do
all = [] of Closable
pool = DB::Pool.new(->{ Closable.new.tap { |c| all << c } }, max_pool_size: 2, initial_pool_size: 1)
pool = DB::Pool.new(max_pool_size: 2, initial_pool_size: 1) { Closable.new.tap { |c| all << c } }
pool.checkout
b1 = pool.checkout
pool.release b1
@ -110,7 +110,7 @@ describe DB::Pool do
it "should close available and total" do
all = [] of Closable
pool = DB::Pool.new(->{ Closable.new.tap { |c| all << c } }, max_pool_size: 2, initial_pool_size: 1)
pool = DB::Pool.new(max_pool_size: 2, initial_pool_size: 1) { Closable.new.tap { |c| all << c } }
a = pool.checkout
b = pool.checkout
pool.release b
@ -124,7 +124,7 @@ describe DB::Pool do
end
it "should timeout" do
pool = DB::Pool.new(->{ Closable.new }, max_pool_size: 1, checkout_timeout: 0.1)
pool = DB::Pool.new(max_pool_size: 1, checkout_timeout: 0.1) { Closable.new }
pool.checkout
expect_raises DB::PoolTimeout do
pool.checkout
@ -132,7 +132,7 @@ describe DB::Pool do
end
it "should be able to release after a timeout" do
pool = DB::Pool.new(->{ Closable.new }, max_pool_size: 1, checkout_timeout: 0.1)
pool = DB::Pool.new(max_pool_size: 1, checkout_timeout: 0.1) { Closable.new }
a = pool.checkout
pool.checkout rescue nil
pool.release a
@ -140,7 +140,7 @@ describe DB::Pool do
it "should close if max idle amount is reached" do
all = [] of Closable
pool = DB::Pool.new(->{ Closable.new.tap { |c| all << c } }, max_pool_size: 3, max_idle_pool_size: 1)
pool = DB::Pool.new(max_pool_size: 3, max_idle_pool_size: 1) { Closable.new.tap { |c| all << c } }
pool.checkout
pool.checkout
pool.checkout
@ -159,7 +159,7 @@ describe DB::Pool do
it "should create resource after max_pool was reached if idle forced some close up" do
all = [] of Closable
pool = DB::Pool.new(->{ Closable.new.tap { |c| all << c } }, max_pool_size: 3, max_idle_pool_size: 1)
pool = DB::Pool.new(max_pool_size: 3, max_idle_pool_size: 1) { Closable.new.tap { |c| all << c } }
pool.checkout
pool.checkout
pool.checkout

View file

@ -29,7 +29,7 @@ module DB
pool_options = @driver.connection_pool_options(params)
@pool = uninitialized Pool(Connection) # in order to use self in the factory proc
@pool = Pool.new(->{ @driver.build_connection(self).as(Connection) }, **pool_options)
@pool = Pool.new(**pool_options) { @driver.build_connection(self).as(Connection) }
end
# Closes all connection to the database.

View file

@ -7,7 +7,7 @@ module DB
@total = [] of T
@checkout_timeout : Float64
def initialize(@factory : Proc(T), @initial_pool_size = 1, @max_pool_size = 1, @max_idle_pool_size = 1, @checkout_timeout = 5.0)
def initialize(@initial_pool_size = 1, @max_pool_size = 1, @max_idle_pool_size = 1, @checkout_timeout = 5.0, &@factory : -> T)
@initial_pool_size.times { build_resource }
@availability_channel = Channel(Nil).new