mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Introduce DB::Pool::Options
This commit is contained in:
parent
36f0a11d07
commit
b3683283d2
5 changed files with 67 additions and 28 deletions
|
@ -22,10 +22,10 @@ describe DB::Pool do
|
|||
expected_per_connection = 5
|
||||
requests = fixed_pool_size * expected_per_connection
|
||||
|
||||
pool = DB::Pool.new(
|
||||
pool = DB::Pool.new(DB::Pool::Options.new(
|
||||
initial_pool_size: fixed_pool_size,
|
||||
max_pool_size: fixed_pool_size,
|
||||
max_idle_pool_size: fixed_pool_size) {
|
||||
max_idle_pool_size: fixed_pool_size)) {
|
||||
HTTP::Client.new(URI.parse("http://127.0.0.1:#{address.port}/"))
|
||||
}
|
||||
|
||||
|
|
|
@ -57,15 +57,19 @@ class Closable
|
|||
end
|
||||
end
|
||||
|
||||
private def create_pool(**options, &factory : -> T) forall T
|
||||
DB::Pool.new(DB::Pool::Options.new(**options), &factory)
|
||||
end
|
||||
|
||||
describe DB::Pool do
|
||||
it "should use proc to create objects" do
|
||||
block_called = 0
|
||||
pool = DB::Pool.new(initial_pool_size: 3) { block_called += 1; Closable.new }
|
||||
pool = create_pool(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 = create_pool { Closable.new }
|
||||
resource = pool.checkout
|
||||
resource.should be_a Closable
|
||||
resource.before_checkout_called.should be_true
|
||||
|
@ -73,18 +77,18 @@ describe DB::Pool do
|
|||
|
||||
it "should be available if not checkedout" do
|
||||
resource = uninitialized Closable
|
||||
pool = DB::Pool.new(initial_pool_size: 1) { resource = Closable.new }
|
||||
pool = create_pool(initial_pool_size: 1) { resource = Closable.new }
|
||||
pool.is_available?(resource).should be_true
|
||||
end
|
||||
|
||||
it "should not be available if checkedout" do
|
||||
pool = DB::Pool.new { Closable.new }
|
||||
pool = create_pool { Closable.new }
|
||||
resource = pool.checkout
|
||||
pool.is_available?(resource).should be_false
|
||||
end
|
||||
|
||||
it "should be available if returned" do
|
||||
pool = DB::Pool.new { Closable.new }
|
||||
pool = create_pool { Closable.new }
|
||||
resource = pool.checkout
|
||||
resource.after_release_called.should be_false
|
||||
pool.release resource
|
||||
|
@ -93,7 +97,7 @@ describe DB::Pool do
|
|||
end
|
||||
|
||||
it "should wait for available resource" do
|
||||
pool = DB::Pool.new(max_pool_size: 1, initial_pool_size: 1) { Closable.new }
|
||||
pool = create_pool(max_pool_size: 1, initial_pool_size: 1) { Closable.new }
|
||||
|
||||
b_cnn_request = ShouldSleepingOp.new
|
||||
wait_a = WaitFor.new
|
||||
|
@ -121,7 +125,7 @@ describe DB::Pool do
|
|||
|
||||
it "should create new if max was not reached" do
|
||||
block_called = 0
|
||||
pool = DB::Pool.new(max_pool_size: 2, initial_pool_size: 1) { block_called += 1; Closable.new }
|
||||
pool = create_pool(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
|
||||
|
@ -131,7 +135,7 @@ describe DB::Pool do
|
|||
|
||||
it "should reuse returned resources" do
|
||||
all = [] of Closable
|
||||
pool = DB::Pool.new(max_pool_size: 2, initial_pool_size: 1) { Closable.new.tap { |c| all << c } }
|
||||
pool = create_pool(max_pool_size: 2, initial_pool_size: 1) { Closable.new.tap { |c| all << c } }
|
||||
pool.checkout
|
||||
b1 = pool.checkout
|
||||
pool.release b1
|
||||
|
@ -143,7 +147,7 @@ describe DB::Pool do
|
|||
|
||||
it "should close available and total" do
|
||||
all = [] of Closable
|
||||
pool = DB::Pool.new(max_pool_size: 2, initial_pool_size: 1) { Closable.new.tap { |c| all << c } }
|
||||
pool = create_pool(max_pool_size: 2, initial_pool_size: 1) { Closable.new.tap { |c| all << c } }
|
||||
a = pool.checkout
|
||||
b = pool.checkout
|
||||
pool.release b
|
||||
|
@ -157,7 +161,7 @@ describe DB::Pool do
|
|||
end
|
||||
|
||||
it "should timeout" do
|
||||
pool = DB::Pool.new(max_pool_size: 1, checkout_timeout: 0.1) { Closable.new }
|
||||
pool = create_pool(max_pool_size: 1, checkout_timeout: 0.1) { Closable.new }
|
||||
pool.checkout
|
||||
expect_raises DB::PoolTimeout do
|
||||
pool.checkout
|
||||
|
@ -165,7 +169,7 @@ describe DB::Pool do
|
|||
end
|
||||
|
||||
it "should be able to release after a timeout" do
|
||||
pool = DB::Pool.new(max_pool_size: 1, checkout_timeout: 0.1) { Closable.new }
|
||||
pool = create_pool(max_pool_size: 1, checkout_timeout: 0.1) { Closable.new }
|
||||
a = pool.checkout
|
||||
pool.checkout rescue nil
|
||||
pool.release a
|
||||
|
@ -173,7 +177,7 @@ describe DB::Pool do
|
|||
|
||||
it "should close if max idle amount is reached" do
|
||||
all = [] of Closable
|
||||
pool = DB::Pool.new(max_pool_size: 3, max_idle_pool_size: 1) { Closable.new.tap { |c| all << c } }
|
||||
pool = create_pool(max_pool_size: 3, max_idle_pool_size: 1) { Closable.new.tap { |c| all << c } }
|
||||
pool.checkout
|
||||
pool.checkout
|
||||
pool.checkout
|
||||
|
@ -191,7 +195,7 @@ describe DB::Pool do
|
|||
end
|
||||
|
||||
it "should not return closed resources to the pool" do
|
||||
pool = DB::Pool.new(max_pool_size: 1, max_idle_pool_size: 1) { Closable.new }
|
||||
pool = create_pool(max_pool_size: 1, max_idle_pool_size: 1) { Closable.new }
|
||||
|
||||
# pool size 1 should be reusing the one resource
|
||||
resource1 = pool.checkout
|
||||
|
@ -209,7 +213,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(max_pool_size: 3, max_idle_pool_size: 1) { Closable.new.tap { |c| all << c } }
|
||||
pool = create_pool(max_pool_size: 3, max_idle_pool_size: 1) { Closable.new.tap { |c| all << c } }
|
||||
pool.checkout
|
||||
pool.checkout
|
||||
pool.checkout
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue