diff --git a/spec/dummy_driver.cr b/spec/dummy_driver.cr index 994eeaf..098dfd8 100644 --- a/spec/dummy_driver.cr +++ b/spec/dummy_driver.cr @@ -9,6 +9,7 @@ class DummyDriver < DB::Driver class DummyConnection < DB::Connection def initialize(context) super(context) + context.io_provider.try &.build_io @connected = true @@connections ||= [] of DummyConnection @@connections.not_nil! << self diff --git a/spec/io_provider_spec.cr b/spec/io_provider_spec.cr new file mode 100644 index 0000000..1f9d1a6 --- /dev/null +++ b/spec/io_provider_spec.cr @@ -0,0 +1,54 @@ +require "./spec_helper" +require "log/spec" + +class DummyIOProvider < DB::IOProvider + @setup_called = false + @teardown_called = false + + def setup : Void + raise "setup called twice" if @setup_called + @setup_called = true + DB::Log.debug &.emit("DummyIOProvider#setup") + end + + def teardown : Void + raise "teardown called twice" if @teardown_called + @teardown_called = true + DB::Log.debug &.emit("DummyIOProvider#teardown") + end + + def build_io : IO + DB::Log.debug &.emit("DummyIOProvider#build_io") + return IO::Memory.new + end +end + +describe DB::IOProvider do + it "setup/teardown are called for pool connection" do + Log.capture(DB::Log.source) do |logs| + DB.open "dummy://host", io_provider: DummyIOProvider.new do |db| + cnn1 = db.checkout + cnn2 = db.checkout + + db.release(cnn1) + db.release(cnn2) + end + + logs.check(:debug, /DummyIOProvider#setup/i) + logs.check(:debug, /DummyIOProvider#build_io/i) + logs.check(:debug, /DummyIOProvider#build_io/i) + logs.check(:debug, /DummyIOProvider#teardown/i) + end + end + + it "setup/teardown are called for single connection" do + Log.capture(DB::Log.source) do |logs| + DB.connect "dummy://host", io_provider: DummyIOProvider.new do |cnn| + end + + logs.check(:debug, /DummyIOProvider#setup/i) + logs.check(:debug, /DummyIOProvider#build_io/i) + logs.check(:debug, /DummyIOProvider#teardown/i) + end + end +end