mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
fix #1. avoid marking as closed if do_close raise exception
This commit is contained in:
parent
933014c1aa
commit
6838784f7b
2 changed files with 33 additions and 1 deletions
31
spec/disposable_spec.cr
Normal file
31
spec/disposable_spec.cr
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
require "./spec_helper"
|
||||||
|
|
||||||
|
class ADisposable
|
||||||
|
include DB::Disposable
|
||||||
|
@raise = false
|
||||||
|
|
||||||
|
property raise
|
||||||
|
|
||||||
|
protected def do_close
|
||||||
|
raise "Unable to close" if @raise
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe DB::Disposable do
|
||||||
|
it "should mark as closed if able to close" do
|
||||||
|
obj = ADisposable.new
|
||||||
|
obj.closed?.should be_false
|
||||||
|
obj.close
|
||||||
|
obj.closed?.should be_true
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not mark as closed if unable to close" do
|
||||||
|
obj = ADisposable.new
|
||||||
|
obj.raise = true
|
||||||
|
obj.closed?.should be_false
|
||||||
|
expect_raises Exception do
|
||||||
|
obj.close
|
||||||
|
end
|
||||||
|
obj.closed?.should be_false
|
||||||
|
end
|
||||||
|
end
|
|
@ -8,8 +8,8 @@ module DB
|
||||||
# Closes this object.
|
# Closes this object.
|
||||||
def close
|
def close
|
||||||
return if @closed
|
return if @closed
|
||||||
@closed = true
|
|
||||||
do_close
|
do_close
|
||||||
|
@closed = true
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns `true` if this object is closed. See `#close`.
|
# Returns `true` if this object is closed. See `#close`.
|
||||||
|
@ -18,6 +18,7 @@ module DB
|
||||||
end
|
end
|
||||||
|
|
||||||
# Implementors overrides this method to perform resource cleanup
|
# Implementors overrides this method to perform resource cleanup
|
||||||
|
# If an exception is raised, the resource will not be marked as closed.
|
||||||
protected abstract def do_close
|
protected abstract def do_close
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue