mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Add before_checkout/after_release callbacks for resources in pool (#36)
add auto_release to DB::Connection (default true on each pool checkout). fixes #35.
This commit is contained in:
parent
e3762eec46
commit
09b8997636
5 changed files with 47 additions and 2 deletions
|
@ -98,6 +98,22 @@ describe DB::Database do
|
|||
end
|
||||
end
|
||||
|
||||
it "should not return connection to pool if checkout explicitly" do
|
||||
DummyDriver::DummyConnection.clear_connections
|
||||
DB.open "dummy://localhost:1027?initial_pool_size=1&max_pool_size=1&retry_attempts=0" do |db|
|
||||
the_cnn = uninitialized DB::Connection
|
||||
db.using_connection do |cnn|
|
||||
the_cnn = cnn
|
||||
db.pool.is_available?(cnn).should be_false
|
||||
3.times do
|
||||
cnn.exec("stmt1")
|
||||
db.pool.is_available?(cnn).should be_false
|
||||
end
|
||||
end
|
||||
db.pool.is_available?(the_cnn).should be_true
|
||||
end
|
||||
end
|
||||
|
||||
describe "prepared_statements connection option" do
|
||||
it "defaults to true" do
|
||||
with_dummy "dummy://localhost:1027" do |db|
|
||||
|
|
|
@ -42,9 +42,19 @@ end
|
|||
|
||||
class Closable
|
||||
include DB::Disposable
|
||||
property before_checkout_called : Bool = false
|
||||
property after_release_called : Bool = false
|
||||
|
||||
protected def do_close
|
||||
end
|
||||
|
||||
def before_checkout
|
||||
@before_checkout_called = true
|
||||
end
|
||||
|
||||
def after_release
|
||||
@after_release_called = true
|
||||
end
|
||||
end
|
||||
|
||||
describe DB::Pool do
|
||||
|
@ -56,7 +66,9 @@ describe DB::Pool do
|
|||
|
||||
it "should get resource" do
|
||||
pool = DB::Pool.new { Closable.new }
|
||||
pool.checkout.should be_a Closable
|
||||
resource = pool.checkout
|
||||
resource.should be_a Closable
|
||||
resource.before_checkout_called.should be_true
|
||||
end
|
||||
|
||||
it "should be available if not checkedout" do
|
||||
|
@ -74,8 +86,10 @@ describe DB::Pool do
|
|||
it "should be available if returned" do
|
||||
pool = DB::Pool.new { Closable.new }
|
||||
resource = pool.checkout
|
||||
resource.after_release_called.should be_false
|
||||
pool.release resource
|
||||
pool.is_available?(resource).should be_true
|
||||
resource.after_release_called.should be_true
|
||||
end
|
||||
|
||||
it "should wait for available resource" do
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue