From ec5e310abc56cc8f31c1e889dc1a50408355d426 Mon Sep 17 00:00:00 2001 From: John Bachir Date: Fri, 30 Jun 2023 19:49:58 -0400 Subject: [PATCH 01/15] error lifecycle tests --- test/lib/error_lifecycle.rb | 47 +++++++++++++++ test/test_error_lifecycle.rb | 113 +++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 test/lib/error_lifecycle.rb create mode 100644 test/test_error_lifecycle.rb diff --git a/test/lib/error_lifecycle.rb b/test/lib/error_lifecycle.rb new file mode 100644 index 0000000..96159bc --- /dev/null +++ b/test/lib/error_lifecycle.rb @@ -0,0 +1,47 @@ +class MyStandardError < StandardError; end +class MyException< Exception; end + +def subject(error_to_raise, error_to_rescue) + $inner_attempted = nil + $inner_else = nil + $inner_rescue = nil + $inner_ensure = nil + $inner_ensure_has_time_to_finish = nil + + $outer_rescue = nil + $outer_else = nil + $outer_ensure = nil + $outer_ensure_has_time_to_finish = nil + + begin + Timeout.timeout(0.001, error_to_raise){ + begin + $inner_attempted = true + nil while true + rescue error_to_rescue + $inner_rescue = true + else + $inner_else = true + ensure + $inner_ensure = true + t = Time.now; nil while Time.now < t+1 + $inner_ensure_has_time_to_finish = true + end + } + rescue Exception + $outer_rescue = true + else + $outer_else = true + ensure + $outer_ensure = true + t = Time.now; nil while Time.now < t+1 + $outer_ensure_has_time_to_finish = true + end + + # remove if/when stop using global variables + Thread.list.each{|t| t.kill unless t==Thread.current } + + unless !!$outer_else ^ !!$outer_rescue + raise "something strange happened with the outer_rescue variables" + end +end diff --git a/test/test_error_lifecycle.rb b/test/test_error_lifecycle.rb new file mode 100644 index 0000000..d0f970d --- /dev/null +++ b/test/test_error_lifecycle.rb @@ -0,0 +1,113 @@ +require 'test/unit' +require 'timeout' +require 'thread' + +class TestTimeout < Test::Unit::TestCase + + ### Tests demonstrating problems with standard lib + # NOTE: this demonstration was done such that all of the assertions pass, + # The ones marked weird, bad, and very bad should not, and their + # passing is demonstrating the brokenness. + # ruby gem at 0f12a0ec11d4a860a56e74a2bb051a77fe70b006 also passes + + require_relative 'lib/error_lifecycle.rb' + + # when an exception to raise is not specified and the inner code does not catch Exception + def test_1 + subject(nil, StandardError) + + # EXPECTED + assert $inner_attempted + assert !$inner_else + assert !$inner_rescue + assert $inner_ensure + assert $outer_rescue + assert $outer_ensure + assert $inner_ensure_has_time_to_finish + assert $outer_ensure_has_time_to_finish + end + + # when an exception to raise is not specified and the inner code does catch Exception + def test_2 + subject(nil, Exception) + + # EXPECTED + assert $inner_attempted + assert !$inner_else + assert $inner_ensure + assert $outer_ensure + assert $inner_ensure_has_time_to_finish + assert $outer_ensure_has_time_to_finish + assert $inner_rescue # true in 1.9, false in gem 0.2.0, true in 0.4.0 + + # BAD? + assert !$outer_rescue # false in 1.9 stdlib, true in gem 0.2.0, false in 0.4.0 + end + + # when an exception to raise is StandardError and the inner code does not catch Exception + def test_3 + subject(MyStandardError, StandardError) + + # EXPECTED + assert $inner_attempted + assert !$inner_else + assert $inner_rescue + assert $inner_ensure + assert $outer_ensure + assert $inner_ensure_has_time_to_finish + assert $outer_ensure_has_time_to_finish + + # BAD? + assert !$outer_rescue + end + + # when an exception to raise is StandardError and the inner code does catch Exception + def test_4 + subject(MyStandardError, Exception) + + # EXPECTED + assert $inner_attempted + assert !$inner_else + assert $inner_rescue + assert $inner_ensure + assert $outer_ensure + assert $inner_ensure_has_time_to_finish + assert $outer_ensure_has_time_to_finish + + # BAD? + assert !$outer_rescue + end + + # when an exception to raise is Exception and the inner code does not catch Exception + def test_5 + subject(MyException, StandardError) + + # EXPECTED + assert $inner_attempted + assert !$inner_else + assert !$inner_rescue + assert $inner_ensure + assert $outer_ensure + assert $outer_rescue + assert $inner_ensure_has_time_to_finish + assert $outer_ensure_has_time_to_finish + end + + # when an exception to raise is Exception and the inner code does catch Exception + def test_6 + subject(MyException, Exception) + + # EXPECTED + assert $inner_attempted + assert !$inner_else + assert $inner_rescue + assert $inner_ensure + assert $outer_ensure + assert $inner_ensure_has_time_to_finish + assert $outer_ensure_has_time_to_finish + + # BAD? + assert !$outer_rescue + end + +end From e7ae1b34b274269ba5a09cedc1a5d57d9d6660d2 Mon Sep 17 00:00:00 2001 From: John Bachir Date: Fri, 30 Jun 2023 20:25:52 -0400 Subject: [PATCH 02/15] no more globals! --- test/lib/error_lifecycle.rb | 80 ++++++++++++------------ test/test_error_lifecycle.rb | 114 ++++++++++++++++++----------------- 2 files changed, 101 insertions(+), 93 deletions(-) diff --git a/test/lib/error_lifecycle.rb b/test/lib/error_lifecycle.rb index 96159bc..bc0fa8a 100644 --- a/test/lib/error_lifecycle.rb +++ b/test/lib/error_lifecycle.rb @@ -1,47 +1,49 @@ class MyStandardError < StandardError; end class MyException< Exception; end -def subject(error_to_raise, error_to_rescue) - $inner_attempted = nil - $inner_else = nil - $inner_rescue = nil - $inner_ensure = nil - $inner_ensure_has_time_to_finish = nil +class ErrorLifeCycleTester + attr_reader :inner_attempted, :inner_else, :inner_rescue, :inner_ensure, :inner_ensure_has_time_to_finish, :outer_rescue, :outer_else, :outer_ensure, :outer_ensure_has_time_to_finish - $outer_rescue = nil - $outer_else = nil - $outer_ensure = nil - $outer_ensure_has_time_to_finish = nil + def subject(error_to_raise, error_to_rescue) - begin - Timeout.timeout(0.001, error_to_raise){ - begin - $inner_attempted = true - nil while true - rescue error_to_rescue - $inner_rescue = true - else - $inner_else = true - ensure - $inner_ensure = true - t = Time.now; nil while Time.now < t+1 - $inner_ensure_has_time_to_finish = true - end - } - rescue Exception - $outer_rescue = true - else - $outer_else = true - ensure - $outer_ensure = true - t = Time.now; nil while Time.now < t+1 - $outer_ensure_has_time_to_finish = true - end + @inner_attempted = nil + @inner_else = nil + @inner_rescue = nil + @inner_ensure = nil + @inner_ensure_has_time_to_finish = nil + + @outer_rescue = nil + @outer_else = nil + @outer_ensure = nil + @outer_ensure_has_time_to_finish = nil - # remove if/when stop using global variables - Thread.list.each{|t| t.kill unless t==Thread.current } + begin + Timeout.timeout(0.001, error_to_raise){ + begin + @inner_attempted = true + nil while true + rescue error_to_rescue + @inner_rescue = true + else + @inner_else = true + ensure + @inner_ensure = true + t = Time.now; nil while Time.now < t+1 + @inner_ensure_has_time_to_finish = true + end + } + rescue Exception + @outer_rescue = true + else + @outer_else = true + ensure + @outer_ensure = true + t = Time.now; nil while Time.now < t+1 + @outer_ensure_has_time_to_finish = true + end - unless !!$outer_else ^ !!$outer_rescue - raise "something strange happened with the outer_rescue variables" + unless !!@outer_else ^ !!@outer_rescue + raise "something strange happened with the outer_rescue variables" + end end -end +end \ No newline at end of file diff --git a/test/test_error_lifecycle.rb b/test/test_error_lifecycle.rb index d0f970d..710c5ae 100644 --- a/test/test_error_lifecycle.rb +++ b/test/test_error_lifecycle.rb @@ -14,100 +14,106 @@ class TestTimeout < Test::Unit::TestCase # when an exception to raise is not specified and the inner code does not catch Exception def test_1 - subject(nil, StandardError) + s = ErrorLifeCycleTester.new + s.subject(nil, StandardError) # EXPECTED - assert $inner_attempted - assert !$inner_else - assert !$inner_rescue - assert $inner_ensure - assert $outer_rescue - assert $outer_ensure - assert $inner_ensure_has_time_to_finish - assert $outer_ensure_has_time_to_finish + assert s.inner_attempted + assert !s.inner_else + assert !s.inner_rescue + assert s.inner_ensure + assert s.outer_rescue + assert s.outer_ensure + assert s.inner_ensure_has_time_to_finish + assert s.outer_ensure_has_time_to_finish end # when an exception to raise is not specified and the inner code does catch Exception def test_2 - subject(nil, Exception) + s = ErrorLifeCycleTester.new + s.subject(nil, Exception) # EXPECTED - assert $inner_attempted - assert !$inner_else - assert $inner_ensure - assert $outer_ensure - assert $inner_ensure_has_time_to_finish - assert $outer_ensure_has_time_to_finish - assert $inner_rescue # true in 1.9, false in gem 0.2.0, true in 0.4.0 + assert s.inner_attempted + assert !s.inner_else + assert s.inner_ensure + assert s.outer_ensure + assert s.inner_ensure_has_time_to_finish + assert s.outer_ensure_has_time_to_finish + assert s.inner_rescue # true in 1.9, false in gem 0.2.0, true in 0.4.0 # BAD? - assert !$outer_rescue # false in 1.9 stdlib, true in gem 0.2.0, false in 0.4.0 + assert !s.outer_rescue # false in 1.9 stdlib, true in gem 0.2.0, false in 0.4.0 end # when an exception to raise is StandardError and the inner code does not catch Exception def test_3 - subject(MyStandardError, StandardError) + s = ErrorLifeCycleTester.new + s.subject(MyStandardError, StandardError) # EXPECTED - assert $inner_attempted - assert !$inner_else - assert $inner_rescue - assert $inner_ensure - assert $outer_ensure - assert $inner_ensure_has_time_to_finish - assert $outer_ensure_has_time_to_finish + assert s.inner_attempted + assert !s.inner_else + assert s.inner_rescue + assert s.inner_ensure + assert s.outer_ensure + assert s.inner_ensure_has_time_to_finish + assert s.outer_ensure_has_time_to_finish # BAD? - assert !$outer_rescue + assert !s.outer_rescue end # when an exception to raise is StandardError and the inner code does catch Exception def test_4 - subject(MyStandardError, Exception) + s = ErrorLifeCycleTester.new + s.subject(MyStandardError, Exception) # EXPECTED - assert $inner_attempted - assert !$inner_else - assert $inner_rescue - assert $inner_ensure - assert $outer_ensure - assert $inner_ensure_has_time_to_finish - assert $outer_ensure_has_time_to_finish + assert s.inner_attempted + assert !s.inner_else + assert s.inner_rescue + assert s.inner_ensure + assert s.outer_ensure + assert s.inner_ensure_has_time_to_finish + assert s.outer_ensure_has_time_to_finish # BAD? - assert !$outer_rescue + assert !s.outer_rescue end # when an exception to raise is Exception and the inner code does not catch Exception def test_5 - subject(MyException, StandardError) + s = ErrorLifeCycleTester.new + s.subject(MyException, StandardError) # EXPECTED - assert $inner_attempted - assert !$inner_else - assert !$inner_rescue - assert $inner_ensure - assert $outer_ensure - assert $outer_rescue - assert $inner_ensure_has_time_to_finish - assert $outer_ensure_has_time_to_finish + assert s.inner_attempted + assert !s.inner_else + assert !s.inner_rescue + assert s.inner_ensure + assert s.outer_ensure + assert s.outer_rescue + assert s.inner_ensure_has_time_to_finish + assert s.outer_ensure_has_time_to_finish end # when an exception to raise is Exception and the inner code does catch Exception def test_6 - subject(MyException, Exception) + s = ErrorLifeCycleTester.new + s.subject(MyException, Exception) # EXPECTED - assert $inner_attempted - assert !$inner_else - assert $inner_rescue - assert $inner_ensure - assert $outer_ensure - assert $inner_ensure_has_time_to_finish - assert $outer_ensure_has_time_to_finish + assert s.inner_attempted + assert !s.inner_else + assert s.inner_rescue + assert s.inner_ensure + assert s.outer_ensure + assert s.inner_ensure_has_time_to_finish + assert s.outer_ensure_has_time_to_finish # BAD? - assert !$outer_rescue + assert !s.outer_rescue end end From 096d15e3f826e167eeab5273a8da3315835c733f Mon Sep 17 00:00:00 2001 From: John Bachir Date: Fri, 30 Jun 2023 20:32:34 -0400 Subject: [PATCH 03/15] whitespace --- test/lib/error_lifecycle.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/lib/error_lifecycle.rb b/test/lib/error_lifecycle.rb index bc0fa8a..99df765 100644 --- a/test/lib/error_lifecycle.rb +++ b/test/lib/error_lifecycle.rb @@ -46,4 +46,4 @@ def subject(error_to_raise, error_to_rescue) raise "something strange happened with the outer_rescue variables" end end -end \ No newline at end of file +end From fbd0e644771f1a2d21f74c43be190a81b536f923 Mon Sep 17 00:00:00 2001 From: John Bachir Date: Fri, 30 Jun 2023 20:41:39 -0400 Subject: [PATCH 04/15] comments --- test/test_error_lifecycle.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/test/test_error_lifecycle.rb b/test/test_error_lifecycle.rb index 710c5ae..f2a2c09 100644 --- a/test/test_error_lifecycle.rb +++ b/test/test_error_lifecycle.rb @@ -4,12 +4,8 @@ class TestTimeout < Test::Unit::TestCase - ### Tests demonstrating problems with standard lib - # NOTE: this demonstration was done such that all of the assertions pass, - # The ones marked weird, bad, and very bad should not, and their - # passing is demonstrating the brokenness. - # ruby gem at 0f12a0ec11d4a860a56e74a2bb051a77fe70b006 also passes - + # Behavior marked "BAD?" is done so as John's opinion, these can/should be removed before the PR is merged + require_relative 'lib/error_lifecycle.rb' # when an exception to raise is not specified and the inner code does not catch Exception From d0fb42457c0389e5e89843fd140f9037e18a1f75 Mon Sep 17 00:00:00 2001 From: John Bachir Date: Fri, 30 Jun 2023 20:44:25 -0400 Subject: [PATCH 05/15] code improvement --- test/lib/error_lifecycle.rb | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/test/lib/error_lifecycle.rb b/test/lib/error_lifecycle.rb index 99df765..c6b4b28 100644 --- a/test/lib/error_lifecycle.rb +++ b/test/lib/error_lifecycle.rb @@ -18,20 +18,18 @@ def subject(error_to_raise, error_to_rescue) @outer_ensure_has_time_to_finish = nil begin - Timeout.timeout(0.001, error_to_raise){ - begin - @inner_attempted = true - nil while true - rescue error_to_rescue - @inner_rescue = true - else - @inner_else = true - ensure - @inner_ensure = true - t = Time.now; nil while Time.now < t+1 - @inner_ensure_has_time_to_finish = true - end - } + Timeout.timeout(0.001, error_to_raise) do + @inner_attempted = true + nil while true + rescue error_to_rescue + @inner_rescue = true + else + @inner_else = true + ensure + @inner_ensure = true + t = Time.now; nil while Time.now < t+1 + @inner_ensure_has_time_to_finish = true + end rescue Exception @outer_rescue = true else From d42ef28c89f4a5303e2e61f73a276c25671d089b Mon Sep 17 00:00:00 2001 From: John Bachir Date: Fri, 30 Jun 2023 21:15:30 -0400 Subject: [PATCH 06/15] comments --- test/test_error_lifecycle.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/test_error_lifecycle.rb b/test/test_error_lifecycle.rb index f2a2c09..7be9218 100644 --- a/test/test_error_lifecycle.rb +++ b/test/test_error_lifecycle.rb @@ -4,8 +4,8 @@ class TestTimeout < Test::Unit::TestCase - # Behavior marked "BAD?" is done so as John's opinion, these can/should be removed before the PR is merged - + # Behavior marked "UNDESIRED?" is done so as John's opinion, these can/should be removed before the PR is merged + require_relative 'lib/error_lifecycle.rb' # when an exception to raise is not specified and the inner code does not catch Exception @@ -38,7 +38,7 @@ def test_2 assert s.outer_ensure_has_time_to_finish assert s.inner_rescue # true in 1.9, false in gem 0.2.0, true in 0.4.0 - # BAD? + # UNDESIRED? assert !s.outer_rescue # false in 1.9 stdlib, true in gem 0.2.0, false in 0.4.0 end @@ -56,7 +56,7 @@ def test_3 assert s.inner_ensure_has_time_to_finish assert s.outer_ensure_has_time_to_finish - # BAD? + # UNDESIRED? assert !s.outer_rescue end @@ -74,7 +74,7 @@ def test_4 assert s.inner_ensure_has_time_to_finish assert s.outer_ensure_has_time_to_finish - # BAD? + # UNDESIRED? assert !s.outer_rescue end @@ -108,7 +108,7 @@ def test_6 assert s.inner_ensure_has_time_to_finish assert s.outer_ensure_has_time_to_finish - # BAD? + # UNDESIRED? assert !s.outer_rescue end From cb1ef51ce6e5edbdf1b9968f9b4acc7761468fec Mon Sep 17 00:00:00 2001 From: John Bachir Date: Sat, 1 Jul 2023 00:34:19 -0400 Subject: [PATCH 07/15] move support file out of lib because it should only be used for extending test-unit --- test/{lib => }/error_lifecycle.rb | 0 test/test_error_lifecycle.rb | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename test/{lib => }/error_lifecycle.rb (100%) diff --git a/test/lib/error_lifecycle.rb b/test/error_lifecycle.rb similarity index 100% rename from test/lib/error_lifecycle.rb rename to test/error_lifecycle.rb diff --git a/test/test_error_lifecycle.rb b/test/test_error_lifecycle.rb index 7be9218..2dc5683 100644 --- a/test/test_error_lifecycle.rb +++ b/test/test_error_lifecycle.rb @@ -6,7 +6,7 @@ class TestTimeout < Test::Unit::TestCase # Behavior marked "UNDESIRED?" is done so as John's opinion, these can/should be removed before the PR is merged - require_relative 'lib/error_lifecycle.rb' + require_relative 'error_lifecycle.rb' # when an exception to raise is not specified and the inner code does not catch Exception def test_1 From 1d4a8a86709aafd087d7630018ead86d3c0c1a0a Mon Sep 17 00:00:00 2001 From: John Bachir Date: Sat, 1 Jul 2023 00:37:19 -0400 Subject: [PATCH 08/15] formatting --- test/error_lifecycle.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/error_lifecycle.rb b/test/error_lifecycle.rb index c6b4b28..7a6c522 100644 --- a/test/error_lifecycle.rb +++ b/test/error_lifecycle.rb @@ -2,10 +2,10 @@ class MyStandardError < StandardError; end class MyException< Exception; end class ErrorLifeCycleTester - attr_reader :inner_attempted, :inner_else, :inner_rescue, :inner_ensure, :inner_ensure_has_time_to_finish, :outer_rescue, :outer_else, :outer_ensure, :outer_ensure_has_time_to_finish + attr_reader :inner_attempted, :inner_else, :inner_rescue, :inner_ensure, :inner_ensure_has_time_to_finish, + :outer_rescue, :outer_else, :outer_ensure, :outer_ensure_has_time_to_finish def subject(error_to_raise, error_to_rescue) - @inner_attempted = nil @inner_else = nil @inner_rescue = nil From dd46da03363f48443970927a4e6fdcc96df780e1 Mon Sep 17 00:00:00 2001 From: John Bachir Date: Sat, 1 Jul 2023 00:46:14 -0400 Subject: [PATCH 09/15] cleanup --- test/test_error_lifecycle.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/test_error_lifecycle.rb b/test/test_error_lifecycle.rb index 2dc5683..648352f 100644 --- a/test/test_error_lifecycle.rb +++ b/test/test_error_lifecycle.rb @@ -13,7 +13,6 @@ def test_1 s = ErrorLifeCycleTester.new s.subject(nil, StandardError) - # EXPECTED assert s.inner_attempted assert !s.inner_else assert !s.inner_rescue @@ -29,7 +28,6 @@ def test_2 s = ErrorLifeCycleTester.new s.subject(nil, Exception) - # EXPECTED assert s.inner_attempted assert !s.inner_else assert s.inner_ensure @@ -47,7 +45,6 @@ def test_3 s = ErrorLifeCycleTester.new s.subject(MyStandardError, StandardError) - # EXPECTED assert s.inner_attempted assert !s.inner_else assert s.inner_rescue @@ -65,7 +62,6 @@ def test_4 s = ErrorLifeCycleTester.new s.subject(MyStandardError, Exception) - # EXPECTED assert s.inner_attempted assert !s.inner_else assert s.inner_rescue @@ -83,7 +79,6 @@ def test_5 s = ErrorLifeCycleTester.new s.subject(MyException, StandardError) - # EXPECTED assert s.inner_attempted assert !s.inner_else assert !s.inner_rescue @@ -99,7 +94,6 @@ def test_6 s = ErrorLifeCycleTester.new s.subject(MyException, Exception) - # EXPECTED assert s.inner_attempted assert !s.inner_else assert s.inner_rescue From 9da21eeed2c2a7ea2f87703d3440d6a14e01ac48 Mon Sep 17 00:00:00 2001 From: John Bachir Date: Sat, 1 Jul 2023 12:43:56 -0400 Subject: [PATCH 10/15] DRY --- test/test_error_lifecycle.rb | 53 +++++++++++------------------------- 1 file changed, 16 insertions(+), 37 deletions(-) diff --git a/test/test_error_lifecycle.rb b/test/test_error_lifecycle.rb index 648352f..dbc59e6 100644 --- a/test/test_error_lifecycle.rb +++ b/test/test_error_lifecycle.rb @@ -8,32 +8,31 @@ class TestTimeout < Test::Unit::TestCase require_relative 'error_lifecycle.rb' + def core_assertions(s) + assert s.inner_attempted + assert !s.inner_else + assert s.inner_ensure + assert s.outer_ensure + assert s.inner_ensure_has_time_to_finish + assert s.outer_ensure_has_time_to_finish + end + # when an exception to raise is not specified and the inner code does not catch Exception def test_1 s = ErrorLifeCycleTester.new s.subject(nil, StandardError) + core_assertions(s) - assert s.inner_attempted - assert !s.inner_else assert !s.inner_rescue - assert s.inner_ensure assert s.outer_rescue - assert s.outer_ensure - assert s.inner_ensure_has_time_to_finish - assert s.outer_ensure_has_time_to_finish end # when an exception to raise is not specified and the inner code does catch Exception def test_2 s = ErrorLifeCycleTester.new s.subject(nil, Exception) + core_assertions(s) - assert s.inner_attempted - assert !s.inner_else - assert s.inner_ensure - assert s.outer_ensure - assert s.inner_ensure_has_time_to_finish - assert s.outer_ensure_has_time_to_finish assert s.inner_rescue # true in 1.9, false in gem 0.2.0, true in 0.4.0 # UNDESIRED? @@ -44,14 +43,9 @@ def test_2 def test_3 s = ErrorLifeCycleTester.new s.subject(MyStandardError, StandardError) + core_assertions(s) - assert s.inner_attempted - assert !s.inner_else assert s.inner_rescue - assert s.inner_ensure - assert s.outer_ensure - assert s.inner_ensure_has_time_to_finish - assert s.outer_ensure_has_time_to_finish # UNDESIRED? assert !s.outer_rescue @@ -61,14 +55,9 @@ def test_3 def test_4 s = ErrorLifeCycleTester.new s.subject(MyStandardError, Exception) + core_assertions(s) - assert s.inner_attempted - assert !s.inner_else assert s.inner_rescue - assert s.inner_ensure - assert s.outer_ensure - assert s.inner_ensure_has_time_to_finish - assert s.outer_ensure_has_time_to_finish # UNDESIRED? assert !s.outer_rescue @@ -78,29 +67,19 @@ def test_4 def test_5 s = ErrorLifeCycleTester.new s.subject(MyException, StandardError) + core_assertions(s) - assert s.inner_attempted - assert !s.inner_else assert !s.inner_rescue - assert s.inner_ensure - assert s.outer_ensure assert s.outer_rescue - assert s.inner_ensure_has_time_to_finish - assert s.outer_ensure_has_time_to_finish end # when an exception to raise is Exception and the inner code does catch Exception def test_6 s = ErrorLifeCycleTester.new s.subject(MyException, Exception) - - assert s.inner_attempted - assert !s.inner_else + core_assertions(s) + assert s.inner_rescue - assert s.inner_ensure - assert s.outer_ensure - assert s.inner_ensure_has_time_to_finish - assert s.outer_ensure_has_time_to_finish # UNDESIRED? assert !s.outer_rescue From 950c0db93447ab270902f6335a60a1db5eaf9ae2 Mon Sep 17 00:00:00 2001 From: John Bachir Date: Sat, 1 Jul 2023 12:47:56 -0400 Subject: [PATCH 11/15] note about ensure time --- test/test_error_lifecycle.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/test_error_lifecycle.rb b/test/test_error_lifecycle.rb index dbc59e6..f97a7ab 100644 --- a/test/test_error_lifecycle.rb +++ b/test/test_error_lifecycle.rb @@ -13,6 +13,11 @@ def core_assertions(s) assert !s.inner_else assert s.inner_ensure assert s.outer_ensure + + # This can result in user's expectation of total possible time + # being very wrong + # t = Time.now; Timeout.timeout(0.1){begin; sleep 1; ensure; sleep 2; end} rescue puts Time.now-t + # => 2.106306 assert s.inner_ensure_has_time_to_finish assert s.outer_ensure_has_time_to_finish end From abc3a6b9cdd12ce1a8ba92cbfdfc80a1d99f357f Mon Sep 17 00:00:00 2001 From: John Bachir Date: Sat, 1 Jul 2023 14:14:33 -0400 Subject: [PATCH 12/15] comment --- test/error_lifecycle.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/error_lifecycle.rb b/test/error_lifecycle.rb index 7a6c522..3892a42 100644 --- a/test/error_lifecycle.rb +++ b/test/error_lifecycle.rb @@ -40,6 +40,8 @@ def subject(error_to_raise, error_to_rescue) @outer_ensure_has_time_to_finish = true end + # this is here to avoid cluttering the "UNDESIRED?" section of each test, + # can be flatted into the main tests unless !!@outer_else ^ !!@outer_rescue raise "something strange happened with the outer_rescue variables" end From 9b9c26cdee9464faf907edec4bd1ef00bca5818c Mon Sep 17 00:00:00 2001 From: John Bachir Date: Sat, 1 Jul 2023 14:15:13 -0400 Subject: [PATCH 13/15] false instead of nil --- test/error_lifecycle.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/error_lifecycle.rb b/test/error_lifecycle.rb index 3892a42..db89cd5 100644 --- a/test/error_lifecycle.rb +++ b/test/error_lifecycle.rb @@ -6,16 +6,16 @@ class ErrorLifeCycleTester :outer_rescue, :outer_else, :outer_ensure, :outer_ensure_has_time_to_finish def subject(error_to_raise, error_to_rescue) - @inner_attempted = nil - @inner_else = nil - @inner_rescue = nil - @inner_ensure = nil - @inner_ensure_has_time_to_finish = nil + @inner_attempted = false + @inner_else = false + @inner_rescue = false + @inner_ensure = false + @inner_ensure_has_time_to_finish = false - @outer_rescue = nil - @outer_else = nil - @outer_ensure = nil - @outer_ensure_has_time_to_finish = nil + @outer_rescue = false + @outer_else = false + @outer_ensure = false + @outer_ensure_has_time_to_finish = false begin Timeout.timeout(0.001, error_to_raise) do @@ -42,7 +42,7 @@ def subject(error_to_raise, error_to_rescue) # this is here to avoid cluttering the "UNDESIRED?" section of each test, # can be flatted into the main tests - unless !!@outer_else ^ !!@outer_rescue + unless @outer_else ^ @outer_rescue raise "something strange happened with the outer_rescue variables" end end From b3bec3f14a2e97e6e55ed269686a5ca901c6f686 Mon Sep 17 00:00:00 2001 From: John Bachir Date: Sat, 1 Jul 2023 14:17:14 -0400 Subject: [PATCH 14/15] comment --- test/test_error_lifecycle.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_error_lifecycle.rb b/test/test_error_lifecycle.rb index f97a7ab..c20670f 100644 --- a/test/test_error_lifecycle.rb +++ b/test/test_error_lifecycle.rb @@ -38,10 +38,10 @@ def test_2 s.subject(nil, Exception) core_assertions(s) - assert s.inner_rescue # true in 1.9, false in gem 0.2.0, true in 0.4.0 + assert s.inner_rescue # true in 1.9, false in gem 0.2.0, true in gem 0.4.0 # UNDESIRED? - assert !s.outer_rescue # false in 1.9 stdlib, true in gem 0.2.0, false in 0.4.0 + assert !s.outer_rescue # false in 1.9 stdlib, true in gem 0.2.0, false in gem 0.4.0 end # when an exception to raise is StandardError and the inner code does not catch Exception From a450d02792f92e24b6aa7f764e3fa729ce18faba Mon Sep 17 00:00:00 2001 From: John Bachir Date: Sun, 2 Jul 2023 22:11:13 -0400 Subject: [PATCH 15/15] Update test_error_lifecycle.rb --- test/test_error_lifecycle.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_error_lifecycle.rb b/test/test_error_lifecycle.rb index c20670f..87aaa42 100644 --- a/test/test_error_lifecycle.rb +++ b/test/test_error_lifecycle.rb @@ -2,7 +2,7 @@ require 'timeout' require 'thread' -class TestTimeout < Test::Unit::TestCase +class TestErrorLifecycle < Test::Unit::TestCase # Behavior marked "UNDESIRED?" is done so as John's opinion, these can/should be removed before the PR is merged