aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/executor_test.rb
blob: 6db6db4fa86d34835857d36acb8f5e957cba006d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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