From 6838784f7be3872a05c300fc7dd194c325cf54b6 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Fri, 24 Jun 2016 10:54:30 -0300 Subject: [PATCH] fix #1. avoid marking as closed if do_close raise exception --- spec/disposable_spec.cr | 31 +++++++++++++++++++++++++++++++ src/db/disposable.cr | 3 ++- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 spec/disposable_spec.cr diff --git a/spec/disposable_spec.cr b/spec/disposable_spec.cr new file mode 100644 index 0000000..3b749d8 --- /dev/null +++ b/spec/disposable_spec.cr @@ -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 diff --git a/src/db/disposable.cr b/src/db/disposable.cr index 4d52dc6..10afb2b 100644 --- a/src/db/disposable.cr +++ b/src/db/disposable.cr @@ -8,8 +8,8 @@ module DB # Closes this object. def close return if @closed - @closed = true do_close + @closed = true end # Returns `true` if this object is closed. See `#close`. @@ -18,6 +18,7 @@ module DB end # 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 end end