diff options
author | Matthew Draper <matthew@trebex.net> | 2016-04-07 03:30:03 +0930 |
---|---|---|
committer | Matthew Draper <matthew@trebex.net> | 2016-04-07 03:30:03 +0930 |
commit | 1c3afbab779f052a3fc6d5e2747874b3347d58cf (patch) | |
tree | ba078fa6f8d0ecf3b5c520ae4b269e53fe5a5545 /activesupport/test | |
parent | c908a902b3c3d4bdf3d18dcf83570277c706bd35 (diff) | |
parent | 291a098c111ff419506094e14c0186389b0020ca (diff) | |
download | rails-1c3afbab779f052a3fc6d5e2747874b3347d58cf.tar.gz rails-1c3afbab779f052a3fc6d5e2747874b3347d58cf.tar.bz2 rails-1c3afbab779f052a3fc6d5e2747874b3347d58cf.zip |
Merge pull request #24422 from matthewd/extinguish-executor-exceptions
Clean up after a failure in a run callback
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/executor_test.rb | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/activesupport/test/executor_test.rb b/activesupport/test/executor_test.rb index 6db6db4fa8..d9b389461a 100644 --- a/activesupport/test/executor_test.rb +++ b/activesupport/test/executor_test.rb @@ -1,6 +1,9 @@ require 'abstract_unit' class ExecutorTest < ActiveSupport::TestCase + class DummyError < RuntimeError + end + def test_wrap_invokes_callbacks called = [] executor.to_run { called << :run } @@ -35,6 +38,20 @@ class ExecutorTest < ActiveSupport::TestCase assert_equal [:run, :body, :complete], called end + def test_exceptions_unwind + called = [] + executor.to_run { called << :run_1 } + executor.to_run { raise DummyError } + executor.to_run { called << :run_2 } + executor.to_complete { called << :complete } + + assert_raises(DummyError) do + executor.wrap { called << :body } + end + + assert_equal [:run_1, :complete], called + end + def test_avoids_double_wrapping called = [] executor.to_run { called << :run } @@ -51,6 +68,96 @@ class ExecutorTest < ActiveSupport::TestCase assert_equal [:run, :early, :body, :late, :complete], called end + def test_hooks_carry_state + supplied_state = :none + + hook = Class.new do + define_method(:run) do + :some_state + end + + define_method(:complete) do |state| + supplied_state = state + end + end.new + + executor.register_hook(hook) + + executor.wrap { } + + assert_equal :some_state, supplied_state + end + + def test_nil_state_is_sufficient + supplied_state = :none + + hook = Class.new do + define_method(:run) do + nil + end + + define_method(:complete) do |state| + supplied_state = state + end + end.new + + executor.register_hook(hook) + + executor.wrap { } + + assert_equal nil, supplied_state + end + + def test_exception_skips_uninvoked_hook + supplied_state = :none + + hook = Class.new do + define_method(:run) do + :some_state + end + + define_method(:complete) do |state| + supplied_state = state + end + end.new + + executor.to_run do + raise DummyError + end + executor.register_hook(hook) + + assert_raises(DummyError) do + executor.wrap { } + end + + assert_equal :none, supplied_state + end + + def test_exception_unwinds_invoked_hook + supplied_state = :none + + hook = Class.new do + define_method(:run) do + :some_state + end + + define_method(:complete) do |state| + supplied_state = state + end + end.new + + executor.register_hook(hook) + executor.to_run do + raise DummyError + end + + assert_raises(DummyError) do + executor.wrap { } + end + + assert_equal :some_state, supplied_state + end + def test_separate_classes_can_wrap other_executor = Class.new(ActiveSupport::Executor) |