aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/executor_test.rb
blob: 3026f002c31a7e2444fe5d5de34860ef9196d4ac (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# frozen_string_literal: true

require "abstract_unit"

class ExecutorTest < ActiveSupport::TestCase
  class DummyError < RuntimeError
  end

  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_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 }
    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_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_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_hook_insertion_order
    invoked = []
    supplied_state = []

    hook_class = Class.new do
      attr_accessor :letter

      define_method(:initialize) do |letter|
        self.letter = letter
      end

      define_method(:run) do
        invoked << :"run_#{letter}"
        :"state_#{letter}"
      end

      define_method(:complete) do |state|
        invoked << :"complete_#{letter}"
        supplied_state << state
      end
    end

    executor.register_hook(hook_class.new(:a))
    executor.register_hook(hook_class.new(:b))
    executor.register_hook(hook_class.new(:c), outer: true)
    executor.register_hook(hook_class.new(:d))

    executor.wrap { }

    assert_equal [:run_c, :run_a, :run_b, :run_d, :complete_a, :complete_b, :complete_d, :complete_c], invoked
    assert_equal [:state_a, :state_b, :state_d, :state_c], supplied_state
  end

  def test_class_serial_is_unaffected
    skip if !defined?(RubyVM)

    hook = Class.new do
      define_method(:run) do
        nil
      end

      define_method(:complete) do |state|
        nil
      end
    end.new

    executor.register_hook(hook)

    before = RubyVM.stat(:class_serial)
    executor.wrap { }
    executor.wrap { }
    executor.wrap { }
    after = RubyVM.stat(:class_serial)

    assert_equal before, after
  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