aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/executor_test.rb
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2016-02-22 11:55:52 +1030
committerMatthew Draper <matthew@trebex.net>2016-03-02 02:14:20 +1030
commitd3c9d808e3e242155a44fd2a89ef272cfade8fe8 (patch)
treef24199aec76f44fa8b6536c0664382672ad10e10 /activesupport/test/executor_test.rb
parent664a13e6fb8281107da0da75e7cd91bba1425f76 (diff)
downloadrails-d3c9d808e3e242155a44fd2a89ef272cfade8fe8.tar.gz
rails-d3c9d808e3e242155a44fd2a89ef272cfade8fe8.tar.bz2
rails-d3c9d808e3e242155a44fd2a89ef272cfade8fe8.zip
Publish AS::Executor and AS::Reloader APIs
These should allow external code to run blocks of user code to do "work", at a similar unit size to a web request, without needing to get intimate with ActionDipatch.
Diffstat (limited to 'activesupport/test/executor_test.rb')
-rw-r--r--activesupport/test/executor_test.rb76
1 files changed, 76 insertions, 0 deletions
diff --git a/activesupport/test/executor_test.rb b/activesupport/test/executor_test.rb
new file mode 100644
index 0000000000..6db6db4fa8
--- /dev/null
+++ b/activesupport/test/executor_test.rb
@@ -0,0 +1,76 @@
+require 'abstract_unit'
+
+class ExecutorTest < ActiveSupport::TestCase
+ def test_wrap_invokes_callbacks
+ called = []
+ executor.to_run { called << :run }
+ executor.to_complete { called << :complete }
+
+ executor.wrap do
+ called << :body
+ end
+
+ assert_equal [:run, :body, :complete], called
+ end
+
+ def test_callbacks_share_state
+ result = false
+ executor.to_run { @foo = true }
+ executor.to_complete { result = @foo }
+
+ executor.wrap { }
+
+ assert result
+ end
+
+ def test_separated_calls_invoke_callbacks
+ called = []
+ executor.to_run { called << :run }
+ executor.to_complete { called << :complete }
+
+ state = executor.run!
+ called << :body
+ state.complete!
+
+ assert_equal [:run, :body, :complete], called
+ end
+
+ def test_avoids_double_wrapping
+ called = []
+ executor.to_run { called << :run }
+ executor.to_complete { called << :complete }
+
+ executor.wrap do
+ called << :early
+ executor.wrap do
+ called << :body
+ end
+ called << :late
+ end
+
+ assert_equal [:run, :early, :body, :late, :complete], called
+ end
+
+ def test_separate_classes_can_wrap
+ other_executor = Class.new(ActiveSupport::Executor)
+
+ called = []
+ executor.to_run { called << :run }
+ executor.to_complete { called << :complete }
+ other_executor.to_run { called << :other_run }
+ other_executor.to_complete { called << :other_complete }
+
+ executor.wrap do
+ other_executor.wrap do
+ called << :body
+ end
+ end
+
+ assert_equal [:run, :other_run, :body, :other_complete, :complete], called
+ end
+
+ private
+ def executor
+ @executor ||= Class.new(ActiveSupport::Executor)
+ end
+end