state_machines 0.301.0 → 0.301.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +8 -0
- data/lib/state_machines/async_mode/async_event_extensions.rb +5 -9
- data/lib/state_machines/async_mode/async_events.rb +25 -49
- data/lib/state_machines/async_mode/async_machine.rb +1 -3
- data/lib/state_machines/async_mode/async_transition_collection.rb +17 -17
- data/lib/state_machines/helper_module.rb +1 -0
- data/lib/state_machines/machine/configuration.rb +4 -3
- data/lib/state_machines/machine/helper_generators.rb +3 -1
- data/lib/state_machines/machine/utilities.rb +1 -1
- data/lib/state_machines/path.rb +1 -0
- data/lib/state_machines/path_collection.rb +1 -0
- data/lib/state_machines/state_context.rb +1 -0
- data/lib/state_machines/syntax_validator.rb +3 -3
- data/lib/state_machines/test_helper.rb +18 -26
- data/lib/state_machines/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4f3fa05a4995ba6d3ff323c8cbf7303a061bf082237353066c01b023129d4c7f
|
|
4
|
+
data.tar.gz: c56a60d5f74f1da251670ff4f5c782d68837a6db2f2f4c200d744adebe620b7f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f6ef8c77f4652d1281e4b66252e0f72395efc2143180b1e91cb437ac7934ca5d9238f3e9b56c5ab3ca7ede1e3a4123fe3ffde899b02f565601b056e7477f0884
|
|
7
|
+
data.tar.gz: 643d203b7719acd77ec70df09bd4c46b722127bf0529e3108285ac5120ea5b0269fe195e09448664cdabcf70a86232a6496ff8f33e8e4943bc64d806b00c28d6
|
data/README.md
CHANGED
|
@@ -321,6 +321,14 @@ exception: a success whose `after_transition` raises, or a callback or guard
|
|
|
321
321
|
that raises before any outcome, leaves the reader at `nil`. (An action that
|
|
322
322
|
raises is `:action_failed`.)
|
|
323
323
|
|
|
324
|
+
Whether an `after_transition` callback that raises undoes the transition
|
|
325
|
+
depends on the action. Without an action hook, the callback runs after the
|
|
326
|
+
transition is complete: the new state stays and the reader is `nil`. When the
|
|
327
|
+
machine hooks its action (the ORM integrations hook `save`), `after_transition`
|
|
328
|
+
callbacks run inside that action, so a raise counts as the action failing: the
|
|
329
|
+
state rolls back with it and the reader is `:action_failed`. The exception is
|
|
330
|
+
re-raised in both cases.
|
|
331
|
+
|
|
324
332
|
A copy made with `dup` or `clone` starts with no reasons, since it has made no
|
|
325
333
|
attempts. A frozen object records nothing: the event still returns `false`, and
|
|
326
334
|
the reader returns `nil`.
|
|
@@ -9,17 +9,15 @@ module StateMachines
|
|
|
9
9
|
result = super
|
|
10
10
|
|
|
11
11
|
# If this is an async-enabled machine and we're defining an event method
|
|
12
|
-
if scope == :instance && method !~ /_async
|
|
12
|
+
if scope == :instance && method !~ /_async!?$/ && machine.async_mode_enabled?
|
|
13
13
|
qualified_name = method.to_s
|
|
14
14
|
|
|
15
15
|
# Create async version that returns a task
|
|
16
|
-
machine.define_helper(scope, "#{qualified_name}_async") do |
|
|
16
|
+
machine.define_helper(scope, "#{qualified_name}_async") do |_machine, object, *method_args, **kwargs|
|
|
17
17
|
# Find the machine that has this event
|
|
18
18
|
target_machine = object.class.state_machines.values.find { |m| m.events[name] }
|
|
19
19
|
|
|
20
|
-
unless defined?(::Async::Task) && ::Async::Task.current?
|
|
21
|
-
raise RuntimeError, "#{qualified_name}_async must be called within an Async context"
|
|
22
|
-
end
|
|
20
|
+
raise "#{qualified_name}_async must be called within an Async context" unless defined?(::Async::Task) && ::Async::Task.current?
|
|
23
21
|
|
|
24
22
|
Async do
|
|
25
23
|
target_machine.events[name].fire(object, *method_args, **kwargs)
|
|
@@ -27,13 +25,11 @@ module StateMachines
|
|
|
27
25
|
end
|
|
28
26
|
|
|
29
27
|
# Create async bang version that raises exceptions when awaited
|
|
30
|
-
machine.define_helper(scope, "#{qualified_name}_async!") do |
|
|
28
|
+
machine.define_helper(scope, "#{qualified_name}_async!") do |_machine, object, *method_args, **kwargs|
|
|
31
29
|
# Find the machine that has this event
|
|
32
30
|
target_machine = object.class.state_machines.values.find { |m| m.events[name] }
|
|
33
31
|
|
|
34
|
-
unless defined?(::Async::Task) && ::Async::Task.current?
|
|
35
|
-
raise RuntimeError, "#{qualified_name}_async! must be called within an Async context"
|
|
36
|
-
end
|
|
32
|
+
raise "#{qualified_name}_async! must be called within an Async context" unless defined?(::Async::Task) && ::Async::Task.current?
|
|
37
33
|
|
|
38
34
|
Async do
|
|
39
35
|
# Use fire method which will raise exceptions on invalid transitions
|
|
@@ -18,14 +18,10 @@ module StateMachines
|
|
|
18
18
|
# Find the machine that has this event
|
|
19
19
|
machine = self.class.state_machines.values.find { |m| m.events[event_name] }
|
|
20
20
|
|
|
21
|
-
unless machine
|
|
22
|
-
raise ArgumentError, "Event #{event_name} not found in any state machine"
|
|
23
|
-
end
|
|
21
|
+
raise ArgumentError, "Event #{event_name} not found in any state machine" unless machine
|
|
24
22
|
|
|
25
23
|
# Must be called within an Async context
|
|
26
|
-
unless defined?(::Async::Task) && ::Async::Task.current?
|
|
27
|
-
raise RuntimeError, "async_fire_event must be called within an Async context. Use: Async { vehicle.async_fire_event(:event) }"
|
|
28
|
-
end
|
|
24
|
+
raise 'async_fire_event must be called within an Async context. Use: Async { vehicle.async_fire_event(:event) }' unless defined?(::Async::Task) && ::Async::Task.current?
|
|
29
25
|
|
|
30
26
|
Async do
|
|
31
27
|
machine.events[event_name].fire(self, *)
|
|
@@ -55,12 +51,11 @@ module StateMachines
|
|
|
55
51
|
# result = vehicle.fire_event_async(:ignite) # => #<StateMachines::Transition ...> or false
|
|
56
52
|
def fire_event_async(event_name, *)
|
|
57
53
|
raise NoMethodError, "undefined method `fire_event_async' for #{self}" unless has_async_machines?
|
|
54
|
+
|
|
58
55
|
# Find the machine that has this event
|
|
59
56
|
machine = self.class.state_machines.values.find { |m| m.events[event_name] }
|
|
60
57
|
|
|
61
|
-
unless machine
|
|
62
|
-
raise ArgumentError, "Event #{event_name} not found in any state machine"
|
|
63
|
-
end
|
|
58
|
+
raise ArgumentError, "Event #{event_name} not found in any state machine" unless machine
|
|
64
59
|
|
|
65
60
|
if defined?(::Async::Task) && ::Async::Task.current?
|
|
66
61
|
# Already in async context, just fire directly
|
|
@@ -82,6 +77,7 @@ module StateMachines
|
|
|
82
77
|
# results = vehicle.fire_events_async(:ignite, :buy_insurance) # => [#<StateMachines::Transition ...>, #<StateMachines::Transition ...>]
|
|
83
78
|
def fire_events_async(*event_names)
|
|
84
79
|
raise NoMethodError, "undefined method `fire_events_async' for #{self}" unless has_async_machines?
|
|
80
|
+
|
|
85
81
|
if defined?(::Async::Task) && ::Async::Task.current?
|
|
86
82
|
# Already in async context, run concurrently
|
|
87
83
|
tasks = event_names.map { |event_name| async_fire_event(event_name) }
|
|
@@ -112,14 +108,10 @@ module StateMachines
|
|
|
112
108
|
# Find the machine that has this event
|
|
113
109
|
machine = self.class.state_machines.values.find { |m| m.events[event_name] }
|
|
114
110
|
|
|
115
|
-
unless machine
|
|
116
|
-
raise ArgumentError, "Event #{event_name} not found in any state machine"
|
|
117
|
-
end
|
|
111
|
+
raise ArgumentError, "Event #{event_name} not found in any state machine" unless machine
|
|
118
112
|
|
|
119
113
|
# Must be called within an Async context
|
|
120
|
-
unless defined?(::Async::Task) && ::Async::Task.current?
|
|
121
|
-
raise RuntimeError, "async_fire_event! must be called within an Async context. Use: Async { vehicle.async_fire_event!(:event) }"
|
|
122
|
-
end
|
|
114
|
+
raise 'async_fire_event! must be called within an Async context. Use: Async { vehicle.async_fire_event!(:event) }' unless defined?(::Async::Task) && ::Async::Task.current?
|
|
123
115
|
|
|
124
116
|
Async do
|
|
125
117
|
fire_with_bang(machine, event_name, *)
|
|
@@ -138,12 +130,11 @@ module StateMachines
|
|
|
138
130
|
# end
|
|
139
131
|
def fire_event_async!(event_name, *)
|
|
140
132
|
raise NoMethodError, "undefined method `fire_event_async!' for #{self}" unless has_async_machines?
|
|
133
|
+
|
|
141
134
|
# Find the machine that has this event
|
|
142
135
|
machine = self.class.state_machines.values.find { |m| m.events[event_name] }
|
|
143
136
|
|
|
144
|
-
unless machine
|
|
145
|
-
raise ArgumentError, "Event #{event_name} not found in any state machine"
|
|
146
|
-
end
|
|
137
|
+
raise ArgumentError, "Event #{event_name} not found in any state machine" unless machine
|
|
147
138
|
|
|
148
139
|
if defined?(::Async::Task) && ::Async::Task.current?
|
|
149
140
|
# Already in async context, just fire directly with bang behavior
|
|
@@ -167,17 +158,13 @@ module StateMachines
|
|
|
167
158
|
base_method = method_str.chomp('_async!').to_sym
|
|
168
159
|
|
|
169
160
|
# Check if the base method exists and this machine is async-enabled
|
|
170
|
-
if respond_to?(base_method) && async_method_for_event?(base_method)
|
|
171
|
-
return handle_individual_event_async_bang(base_method, *, **)
|
|
172
|
-
end
|
|
161
|
+
return handle_individual_event_async_bang(base_method, *, **) if respond_to?(base_method) && async_method_for_event?(base_method)
|
|
173
162
|
elsif method_str.end_with?('_async')
|
|
174
163
|
# Remove the _async suffix to get the base event method
|
|
175
164
|
base_method = method_str.chomp('_async').to_sym
|
|
176
165
|
|
|
177
166
|
# Check if the base method exists and this machine is async-enabled
|
|
178
|
-
if respond_to?(base_method) && async_method_for_event?(base_method)
|
|
179
|
-
return handle_individual_event_async(base_method, *, **)
|
|
180
|
-
end
|
|
167
|
+
return handle_individual_event_async(base_method, *, **) if respond_to?(base_method) && async_method_for_event?(base_method)
|
|
181
168
|
end
|
|
182
169
|
|
|
183
170
|
# If not an async method, call the original method_missing
|
|
@@ -201,7 +188,7 @@ module StateMachines
|
|
|
201
188
|
|
|
202
189
|
# Check if this object has any async-enabled state machines
|
|
203
190
|
def has_async_machines?
|
|
204
|
-
self.class.state_machines.any? { |
|
|
191
|
+
self.class.state_machines.any? { |_name, machine| machine.async_mode_enabled? }
|
|
205
192
|
end
|
|
206
193
|
|
|
207
194
|
private
|
|
@@ -214,15 +201,13 @@ module StateMachines
|
|
|
214
201
|
# Check if this event method should have async versions
|
|
215
202
|
def async_method_for_event?(event_method)
|
|
216
203
|
# Find which machine contains this event
|
|
217
|
-
self.class.state_machines.each do |
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
end
|
|
225
|
-
end
|
|
204
|
+
self.class.state_machines.each do |_name, machine|
|
|
205
|
+
next unless machine.async_mode_enabled?
|
|
206
|
+
|
|
207
|
+
# Check if this event method belongs to this machine
|
|
208
|
+
machine.events.each do |event|
|
|
209
|
+
qualified_name = event.qualified_name
|
|
210
|
+
return true if qualified_name.to_sym == event_method || :"#{qualified_name}!" == event_method
|
|
226
211
|
end
|
|
227
212
|
end
|
|
228
213
|
false
|
|
@@ -230,10 +215,7 @@ module StateMachines
|
|
|
230
215
|
|
|
231
216
|
# Handle individual event async methods (returns task)
|
|
232
217
|
def handle_individual_event_async(event_method, *, **)
|
|
233
|
-
|
|
234
|
-
unless defined?(::Async::Task) && ::Async::Task.current?
|
|
235
|
-
raise RuntimeError, "#{event_method}_async must be called within an Async context"
|
|
236
|
-
end
|
|
218
|
+
raise "#{event_method}_async must be called within an Async context" unless defined?(::Async::Task) && ::Async::Task.current?
|
|
237
219
|
|
|
238
220
|
Async do
|
|
239
221
|
send(event_method, *, **)
|
|
@@ -243,11 +225,9 @@ module StateMachines
|
|
|
243
225
|
# Handle individual event async bang methods (returns task, raises on failure)
|
|
244
226
|
def handle_individual_event_async_bang(event_method, *, **)
|
|
245
227
|
# Extract event name from method and use bang version
|
|
246
|
-
bang_method = "#{event_method}!"
|
|
228
|
+
bang_method = :"#{event_method}!"
|
|
247
229
|
|
|
248
|
-
unless defined?(::Async::Task) && ::Async::Task.current?
|
|
249
|
-
raise RuntimeError, "#{event_method}_async! must be called within an Async context"
|
|
250
|
-
end
|
|
230
|
+
raise "#{event_method}_async! must be called within an Async context" unless defined?(::Async::Task) && ::Async::Task.current?
|
|
251
231
|
|
|
252
232
|
Async do
|
|
253
233
|
send(bang_method, *, **)
|
|
@@ -259,12 +239,10 @@ module StateMachines
|
|
|
259
239
|
method_str = method_name.to_s
|
|
260
240
|
|
|
261
241
|
# Find the machine and event for this method
|
|
262
|
-
self.class.state_machines.each do |
|
|
242
|
+
self.class.state_machines.each do |_name, machine|
|
|
263
243
|
machine.events.each do |event|
|
|
264
244
|
qualified_name = event.qualified_name
|
|
265
|
-
if qualified_name.to_s == method_str || "#{qualified_name}!"
|
|
266
|
-
return event.name
|
|
267
|
-
end
|
|
245
|
+
return event.name if qualified_name.to_s == method_str || "#{qualified_name}!" == method_str
|
|
268
246
|
end
|
|
269
247
|
end
|
|
270
248
|
|
|
@@ -282,9 +260,7 @@ module StateMachines
|
|
|
282
260
|
# results = vehicle.fire_events_concurrent(:ignite, :buy_insurance)
|
|
283
261
|
# end
|
|
284
262
|
def fire_events_concurrent(*event_names)
|
|
285
|
-
unless defined?(::Async::Task) && ::Async::Task.current?
|
|
286
|
-
raise RuntimeError, "fire_events_concurrent must be called within an Async context"
|
|
287
|
-
end
|
|
263
|
+
raise 'fire_events_concurrent must be called within an Async context' unless defined?(::Async::Task) && ::Async::Task.current?
|
|
288
264
|
|
|
289
265
|
tasks = async_fire_events(*event_names)
|
|
290
266
|
tasks.map(&:wait)
|
|
@@ -25,9 +25,7 @@ module StateMachines
|
|
|
25
25
|
# Fires an event asynchronously on the given object
|
|
26
26
|
# Returns an Async::Task for concurrent execution
|
|
27
27
|
def async_fire_event(object, event_name, *)
|
|
28
|
-
unless defined?(::Async::Task) && ::Async::Task.current?
|
|
29
|
-
raise RuntimeError, "async_fire_event must be called within an Async context"
|
|
30
|
-
end
|
|
28
|
+
raise 'async_fire_event must be called within an Async context' unless defined?(::Async::Task) && ::Async::Task.current?
|
|
31
29
|
|
|
32
30
|
Async do
|
|
33
31
|
events[event_name].fire(object, *)
|
|
@@ -39,24 +39,24 @@ module StateMachines
|
|
|
39
39
|
def run_actions(&)
|
|
40
40
|
catch_exceptions do
|
|
41
41
|
@success = if block_given?
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
result = yield
|
|
43
|
+
actions.each { |action| results[action] = result }
|
|
44
|
+
!!result
|
|
45
|
+
else
|
|
46
|
+
actions.compact.each do |action|
|
|
47
|
+
next if skip_actions
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
49
|
+
# Use thread-safe write for results
|
|
50
|
+
if object.respond_to?(:state_machine_mutex)
|
|
51
|
+
object.state_machine_mutex.with_write_lock do
|
|
52
|
+
results[action] = object.send(action)
|
|
53
|
+
end
|
|
54
|
+
else
|
|
55
|
+
results[action] = object.send(action)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
results.values.all?
|
|
59
|
+
end
|
|
60
60
|
end
|
|
61
61
|
end
|
|
62
62
|
end
|
|
@@ -6,21 +6,22 @@ module StateMachines
|
|
|
6
6
|
# Initializes a new state machine with the given configuration.
|
|
7
7
|
def initialize(owner_class, *args, &)
|
|
8
8
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
# Find an integration that matches this machine's owner class
|
|
11
11
|
@integration = if options.include?(:integration)
|
|
12
12
|
options[:integration] && StateMachines::Integrations.find_by_name(options[:integration])
|
|
13
13
|
else
|
|
14
14
|
StateMachines::Integrations.match(owner_class)
|
|
15
15
|
end
|
|
16
|
-
|
|
16
|
+
|
|
17
17
|
# Validate options including integration-specific options
|
|
18
|
-
valid_keys = [
|
|
18
|
+
valid_keys = %i[attribute initial initialize action plural namespace integration messages use_transactions async]
|
|
19
19
|
valid_keys += @integration.integration_options if @integration
|
|
20
20
|
StateMachines::OptionsValidator.assert_valid_keys!(options, valid_keys)
|
|
21
21
|
|
|
22
22
|
if @integration
|
|
23
23
|
extend @integration
|
|
24
|
+
|
|
24
25
|
options = (@integration.defaults || {}).merge(options)
|
|
25
26
|
end
|
|
26
27
|
|
|
@@ -38,7 +38,9 @@ module StateMachines
|
|
|
38
38
|
# Adds predicate method to the owner class for determining the name of the
|
|
39
39
|
# current state
|
|
40
40
|
def define_state_predicate
|
|
41
|
-
|
|
41
|
+
# Interpolated into the helper source, so it must be a literal boolean
|
|
42
|
+
# (the finder answers the ancestor owning the method)
|
|
43
|
+
call_super = owner_class_ancestor_has_method?(:instance, "#{name}?") ? true : false # rubocop:disable Style/IfWithBooleanLiteralBranches
|
|
42
44
|
define_helper :instance, <<-END_EVAL, __FILE__, __LINE__ + 1
|
|
43
45
|
def #{name}?(*args)
|
|
44
46
|
args.empty? && (#{call_super} || defined?(super)) ? super : self.class.state_machine(#{name.inspect}).states.matches?(self, *args)
|
|
@@ -13,7 +13,7 @@ module StateMachines
|
|
|
13
13
|
# base class that may have originally defined the machine.
|
|
14
14
|
def sibling_machines
|
|
15
15
|
owner_class.state_machines.each_with_object([]) do |(name, machine), machines|
|
|
16
|
-
machines <<
|
|
16
|
+
machines << owner_class.state_machine(name) {} if machine.attribute == attribute && machine != self
|
|
17
17
|
end
|
|
18
18
|
end
|
|
19
19
|
|
data/lib/state_machines/path.rb
CHANGED
|
@@ -22,6 +22,7 @@ module StateMachines
|
|
|
22
22
|
# * <tt>:guard</tt> - Whether to guard transitions with the if/unless
|
|
23
23
|
# conditionals defined for each one
|
|
24
24
|
def initialize(object, machine, options = {})
|
|
25
|
+
super()
|
|
25
26
|
StateMachines::OptionsValidator.assert_valid_keys!(options, :target, :guard)
|
|
26
27
|
|
|
27
28
|
@object = object
|
|
@@ -27,6 +27,7 @@ module StateMachines
|
|
|
27
27
|
# * <tt>:guard</tt> - Whether to guard transitions with the if/unless
|
|
28
28
|
# conditionals defined for each one
|
|
29
29
|
def initialize(object, machine, options = {})
|
|
30
|
+
super()
|
|
30
31
|
options = { deep: false, from: machine.states.match!(object).name }.merge(options)
|
|
31
32
|
StateMachines::OptionsValidator.assert_valid_keys!(options, :from, :to, :deep, :guard)
|
|
32
33
|
|
|
@@ -42,9 +42,9 @@ module StateMachines
|
|
|
42
42
|
module UniversalBackend
|
|
43
43
|
def validate!(code, filename)
|
|
44
44
|
code = code.b
|
|
45
|
-
code.sub!(/\A(?:\xef\xbb\xbf)?(\s*\#.*$)*(\n)?/n)
|
|
46
|
-
"
|
|
47
|
-
|
|
45
|
+
code.sub!(/\A(?:\xef\xbb\xbf)?(\s*\#.*$)*(\n)?/n) do
|
|
46
|
+
"#{::Regexp.last_match(0)}#{"\n" if ::Regexp.last_match(1) && !::Regexp.last_match(2)}BEGIN{throw tag, :ok}\n"
|
|
47
|
+
end
|
|
48
48
|
code = code.force_encoding(Encoding::UTF_8)
|
|
49
49
|
catch { |tag| eval(code, binding, filename, __LINE__ - 1) } == :ok
|
|
50
50
|
end
|
|
@@ -403,9 +403,7 @@ module StateMachines
|
|
|
403
403
|
async_machines = []
|
|
404
404
|
|
|
405
405
|
object.class.state_machines.each do |name, machine|
|
|
406
|
-
if machine.respond_to?(:async_mode_enabled?) && machine.async_mode_enabled?
|
|
407
|
-
async_machines << name
|
|
408
|
-
end
|
|
406
|
+
async_machines << name if machine.respond_to?(:async_mode_enabled?) && machine.async_mode_enabled?
|
|
409
407
|
end
|
|
410
408
|
|
|
411
409
|
default_message = "Expected all state machines to be sync-only, but these have async enabled: #{async_machines.inspect}"
|
|
@@ -448,36 +446,31 @@ module StateMachines
|
|
|
448
446
|
_sm_assert(correct_final_state, message || default_message)
|
|
449
447
|
end
|
|
450
448
|
|
|
451
|
-
# Assert that event execution is immediate
|
|
449
|
+
# Assert that event execution is immediate: the state has changed when the
|
|
450
|
+
# bang event returns, and it returned no async task to wait on. Decided by
|
|
451
|
+
# what the call did, not by how long it took, so a slow runner (a cold
|
|
452
|
+
# JIT) cannot fail it.
|
|
452
453
|
#
|
|
453
454
|
# @param object [Object] The object with state machines
|
|
454
455
|
# @param event [Symbol] The event to trigger
|
|
455
456
|
# @param machine_name [Symbol] The name of the state machine (defaults to :state)
|
|
456
457
|
# @param message [String, nil] Custom failure message
|
|
457
458
|
# @return [void]
|
|
458
|
-
# @raise [AssertionError] If
|
|
459
|
+
# @raise [AssertionError] If the event did not complete synchronously
|
|
459
460
|
#
|
|
460
461
|
# @example
|
|
461
462
|
# car = Car.new
|
|
462
463
|
# assert_sm_immediate_execution(car, :start)
|
|
463
464
|
def assert_sm_immediate_execution(object, event, machine_name = :state, message = nil)
|
|
464
465
|
initial_state = object.send(machine_name)
|
|
465
|
-
|
|
466
|
-
# Record start time and execute
|
|
467
|
-
start_time = Time.now
|
|
468
|
-
object.send("#{event}!")
|
|
469
|
-
execution_time = Time.now - start_time
|
|
470
|
-
|
|
466
|
+
result = object.send("#{event}!")
|
|
471
467
|
final_state = object.send(machine_name)
|
|
472
|
-
state_changed = initial_state != final_state
|
|
473
468
|
|
|
474
|
-
|
|
475
|
-
|
|
469
|
+
deferred = defined?(::Async::Task) && result.is_a?(::Async::Task)
|
|
470
|
+
default_message = "Expected immediate sync execution of '#{event}', but it returned #{result.class} (async)"
|
|
476
471
|
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
_sm_assert(state_changed, 'Event should trigger state change')
|
|
480
|
-
_sm_assert(is_immediate, message || default_message)
|
|
472
|
+
_sm_assert(initial_state != final_state, message || "Event #{event} should change #{machine_name} before returning")
|
|
473
|
+
_sm_assert(!deferred, message || default_message)
|
|
481
474
|
end
|
|
482
475
|
|
|
483
476
|
# === Async Mode Assertions ===
|
|
@@ -518,7 +511,7 @@ module StateMachines
|
|
|
518
511
|
async_methods = %i[fire_event_async fire_events_async fire_event_async! async_fire_event]
|
|
519
512
|
available_async_methods = async_methods.select { |method| object.respond_to?(method) }
|
|
520
513
|
|
|
521
|
-
default_message =
|
|
514
|
+
default_message = 'Expected async methods to be available, but found none'
|
|
522
515
|
|
|
523
516
|
_sm_refute_empty(available_async_methods, message || default_message)
|
|
524
517
|
end
|
|
@@ -547,11 +540,11 @@ module StateMachines
|
|
|
547
540
|
_sm_assert_empty(non_async_machines, message || default_message)
|
|
548
541
|
else
|
|
549
542
|
# Check that at least one machine has async
|
|
550
|
-
async_machines = object.class.state_machines.select do |
|
|
543
|
+
async_machines = object.class.state_machines.select do |_name, machine|
|
|
551
544
|
machine.respond_to?(:async_mode_enabled?) && machine.async_mode_enabled?
|
|
552
545
|
end
|
|
553
546
|
|
|
554
|
-
default_message =
|
|
547
|
+
default_message = 'Expected at least one state machine to have async enabled, but none found'
|
|
555
548
|
|
|
556
549
|
_sm_refute_empty(async_machines, message || default_message)
|
|
557
550
|
end
|
|
@@ -569,15 +562,14 @@ module StateMachines
|
|
|
569
562
|
# drone = AutonomousDrone.new
|
|
570
563
|
# assert_sm_async_event_methods(drone, :launch) # Checks launch_async and launch_async!
|
|
571
564
|
def assert_sm_async_event_methods(object, event, message = nil)
|
|
572
|
-
async_method = "#{event}_async"
|
|
573
|
-
async_bang_method = "#{event}_async!"
|
|
565
|
+
async_method = :"#{event}_async"
|
|
566
|
+
async_bang_method = :"#{event}_async!"
|
|
574
567
|
|
|
575
568
|
has_async = object.respond_to?(async_method)
|
|
576
569
|
has_async_bang = object.respond_to?(async_bang_method)
|
|
577
570
|
|
|
578
|
-
|
|
579
|
-
_sm_assert(
|
|
580
|
-
_sm_assert(has_async_bang, "Missing #{async_bang_method} method")
|
|
571
|
+
_sm_assert(has_async, message || "Missing #{async_method} method")
|
|
572
|
+
_sm_assert(has_async_bang, message || "Missing #{async_bang_method} method")
|
|
581
573
|
end
|
|
582
574
|
|
|
583
575
|
# Assert that an object has thread-safe state methods when async is enabled
|