Async mode for state_machines: state machines
whose events run as async tasks, with per-object
thread-safe state access (concurrent-ruby) and transitions claimed before they are performed, so
concurrent transitions out of one state perform once.
This gem owns async mode. The core state_machines gem ships none of it: without this gem loaded,
state_machine ..., async: true raises ArgumentError naming state_machines-async.
CRuby (MRI) 4.0+ only. The async gem needs the Fiber scheduler and the io-event native extension,
which JRuby and TruffleRuby lack, so require 'state_machines-async' raises LoadError there
instead of running async machines synchronously.
gem 'state_machines-async'require 'state_machines-async'Enable async mode per machine with async: true; machines without it stay synchronous, and a
class can mix both:
class AutonomousDrone
state_machine :status, async: true, initial: :docked do
event :launch do
transition docked: :flying
end
event :land do
transition flying: :docked
end
end
# Synchronous machine on the same class
state_machine :weapons, initial: :disarmed do
event :arm do
transition disarmed: :armed
end
end
endAn integration can make its machines async by default (@defaults = { async: true }); async: false
on a machine overrides that. async: is also accepted when reopening a machine (state_machine :status, async: true), and
configure_async_mode!(enabled) switches it from inside the machine definition.
drone = AutonomousDrone.new
Async do
task = drone.launch_async # => Async::Task
task.wait # => #<StateMachines::Transition ...> or false
drone.land_async!.wait # raises StateMachines::InvalidTransition on failure
drone.async_fire_event(:launch) # => Async::Task
drone.fire_events_concurrent(:land) # => [result, ...]
end
# Waits for the result, entering a reactor if needed
drone.fire_event_async(:launch) # => transition or false
drone.fire_event_async!(:land) # raises on failure
drone.fire_events_async(:launch, :arm) # => [result, result]
# Outside an Async context the task-returning methods raise
drone.launch_async # => RuntimeError: launch_async must be called within an Async context- Each object with an async machine gets its own
Concurrent::ReentrantReadWriteLock, created on demand (state_machine_mutex,read_state_safely,write_state_safely). Adup/clonegets its own lock, and marshalling leaves the lock out. - Async machines claim their transitions (
Machine#claims_transitions?): of concurrent transitions out of one state, from threads or from fibers of one reactor, the first is performed and the others are refused with the:contendedreason (see the core README's Refusal Reasons). Machine#create_async_transition_collectionbuilds a collection withperform_async(in a reactor) andperform_threaded.
StateMachines::Async::TestHelper includes the core's StateMachines::TestHelper and adds the
async assertions, so a test case includes only it:
require 'state_machines/async/test_helper'
class DroneTest < Minitest::Test
include StateMachines::Async::TestHelper
def test_modes
drone = AutonomousDrone.new
assert_sm_async_mode(drone, :status)
assert_sm_sync_mode(drone, :weapons)
assert_sm_has_async(drone, %i[status])
assert_sm_async_methods(drone)
assert_sm_async_event_methods(drone, :launch)
assert_sm_thread_safe_methods(drone)
end
endAlso available: assert_sm_all_sync, assert_sm_no_async_methods.
bundle install
bundle exec rake test # padded_cellMIT, see LICENSE.txt.