Simple state transition check code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StateTrack | |
def initialize | |
@state = :initialized | |
end | |
# Expected state transition: | |
# :initialized -> :starting -> :running -> :shuttingdown -> :terminated | |
def process_event(ev, *args) | |
case [ev, @state] | |
when [:on_start, :initialized] | |
@state = :starting | |
when [:on_run, :starting] | |
@state = :running | |
when [:on_shutdown, :running] | |
@state = :shuttingdown | |
when [:on_terminate, :shuttingdown] | |
@state = :terminated | |
else | |
raise "Unknown state transition: #{ev} #{@state}" | |
end | |
end | |
end | |
s = new StateTrack | |
s.process_event(:on_start) | |
s.process_event(:on_run) | |
s.process_event(:on_shutdown) | |
s.process_event(:on_terminate) | |
s2 = new StateTrack | |
s.process_event(:on_start) | |
s.process_event(:on_shutdown) # raise error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment