Created
November 28, 2016 11:38
-
-
Save JoshCheek/10deb07277b6c85efc7b5e65c007785d to your computer and use it in GitHub Desktop.
Contemplating a binary finish criteria for SiB's EventStream::Consumer
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
# A binary implementation of https://github.com/JoshCheek/seeing_is_believing/blob/7cf75d540344c3377d8cbf66d12853c2b23c763b/lib/seeing_is_believing/event_stream/consumer.rb#L10-L50 | |
class Fixnum | |
def inspect | |
sprintf '%04b', self # => "0001", "0010", "0100", "1000", "1111", "0000", "0000", "0001", "0001", "0101", "0101", "1101", "1101", "1111", "1111" | |
end # => :inspect | |
end # => :inspect | |
class FinishCriteria | |
FinishCriteria = [ | |
EventThreadFinished = 0b0001, # => 0001 | |
StdoutThreadFinished = 0b0010, # => 0010 | |
StderrThreadFinished = 0b0100, # => 0100 | |
ProcessExited = 0b1000, # => 1000 | |
].reduce(0, :|) # => 1111 | |
def initialize | |
@met_criteria = 0 # => 0000 | |
end # => :initialize | |
# finish criteria are satisfied, | |
# we can stop processing events | |
def satisfied? | |
@met_criteria == FinishCriteria # => false, true | |
end # => :satisfied? | |
def event_thread_finished! | |
@met_criteria |= EventThreadFinished # => 0001 | |
end # => :event_thread_finished! | |
def stdout_thread_finished! | |
@met_criteria |= StdoutThreadFinished # => 1111 | |
end # => :stdout_thread_finished! | |
def stderr_thread_finished! | |
@met_criteria |= StderrThreadFinished # => 0101 | |
end # => :stderr_thread_finished! | |
def received_exitstatus! | |
@met_criteria |= ProcessExited | |
end # => :received_exitstatus! | |
def received_timeout! | |
@met_criteria |= ProcessExited # => 1101 | |
end # => :received_timeout! | |
end # => :received_timeout! | |
f = FinishCriteria.new # => #<FinishCriteria:0x007f956297b5e0 @met_criteria=0000> | |
f.event_thread_finished! # => 0001 | |
f.stderr_thread_finished! # => 0101 | |
f.received_timeout! # => 1101 | |
f.satisfied? # => false | |
f.stdout_thread_finished! # => 1111 | |
f.satisfied? # => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment