From 8d891a5a4ed7aa00a6456add9e1758de3ce829e7 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Mon, 29 Aug 2016 13:14:47 -0300 Subject: [PATCH] changes needed due to 3c91978d368b50bbdb86d1c1cae3fc50db34b70c add posibility to inspect availability of a resource in a pool (testing only) allow access to internal connection pool of db (testing only) --- spec/pool_spec.cr | 19 +++++++++++++++++++ spec/statement_spec.cr | 3 ++- src/db/database.cr | 2 ++ src/db/pool.cr | 5 +++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/spec/pool_spec.cr b/spec/pool_spec.cr index 86e6bea..1f59aaa 100644 --- a/spec/pool_spec.cr +++ b/spec/pool_spec.cr @@ -59,6 +59,25 @@ describe DB::Pool do pool.checkout.should be_a Closable end + it "should be available if not checkedout" do + resource = uninitialized Closable + pool = DB::Pool.new(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 } + resource = pool.checkout + pool.is_available?(resource).should be_false + end + + it "should be available if returned" do + pool = DB::Pool.new { Closable.new } + resource = pool.checkout + pool.release resource + pool.is_available?(resource).should be_true + end + it "should wait for available resource" do pool = DB::Pool.new(max_pool_size: 1, initial_pool_size: 1) { Closable.new } diff --git a/spec/statement_spec.cr b/spec/statement_spec.cr index 196c2ed..aa3c245 100644 --- a/spec/statement_spec.cr +++ b/spec/statement_spec.cr @@ -124,7 +124,8 @@ describe DB::Statement do expect_raises do db.exec "raise" end - db.@in_pool.should be_true + DummyDriver::DummyConnection.connections.size.should eq(1) + db.pool.is_available?(DummyDriver::DummyConnection.connections.first) end end end diff --git a/src/db/database.cr b/src/db/database.cr index 59b33e3..e96d5a1 100644 --- a/src/db/database.cr +++ b/src/db/database.cr @@ -16,6 +16,8 @@ module DB class Database # :nodoc: getter driver + # :nodoc: + getter pool # Returns the uri with the connection settings to the database getter uri diff --git a/src/db/pool.cr b/src/db/pool.cr index 2f68771..f5ce6f8 100644 --- a/src/db/pool.cr +++ b/src/db/pool.cr @@ -53,6 +53,11 @@ module DB end end + # :nodon: + def is_available?(resource : T) + @available.includes?(resource) + end + private def build_resource : T resource = @factory.call @total << resource