Class: Phronomy::Testing::FakeScheduler
- Inherits:
-
Object
- Object
- Phronomy::Testing::FakeScheduler
- Defined in:
- lib/phronomy/testing/fake_scheduler.rb
Overview
A deterministic event dispatcher for use in tests.
Wraps a Thread::Queue and dispatches events one at a time via #tick or drains all pending events via #tick_until_idle. Tests can inspect queue depth and verify event ordering without wall-clock sleeps.
Instance Attribute Summary collapse
-
#dispatched ⇒ Array
readonly
All events dispatched so far (in order).
Instance Method Summary collapse
-
#idle? ⇒ Boolean
private
Returns true when the queue is empty.
-
#initialize ⇒ FakeScheduler
constructor
A new instance of FakeScheduler.
-
#on(klass) {|event| ... } ⇒ self
private
Register a handler block for events of the given class.
-
#post(event) ⇒ self
private
Enqueue an event for later dispatch.
-
#queue_depth ⇒ Integer
private
Returns the number of events waiting to be dispatched.
-
#tick ⇒ Object?
private
Dispatch the next queued event.
-
#tick_until_idle(max_ticks: 1000) ⇒ Integer
private
Dispatch events until the queue is empty.
Constructor Details
#initialize ⇒ FakeScheduler
Returns a new instance of FakeScheduler.
24 25 26 27 28 |
# File 'lib/phronomy/testing/fake_scheduler.rb', line 24 def initialize @queue = Thread::Queue.new @dispatched = [] @handlers = {} end |
Instance Attribute Details
#dispatched ⇒ Array (readonly)
Returns all events dispatched so far (in order).
22 23 24 |
# File 'lib/phronomy/testing/fake_scheduler.rb', line 22 def dispatched @dispatched end |
Instance Method Details
#idle? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns true when the queue is empty.
99 100 101 |
# File 'lib/phronomy/testing/fake_scheduler.rb', line 99 def idle? @queue.empty? end |
#on(klass) {|event| ... } ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Register a handler block for events of the given class. Use +:any+ to handle all event types.
91 92 93 94 |
# File 'lib/phronomy/testing/fake_scheduler.rb', line 91 def on(klass, &block) @handlers[klass] = block self end |
#post(event) ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Enqueue an event for later dispatch.
35 36 37 38 |
# File 'lib/phronomy/testing/fake_scheduler.rb', line 35 def post(event) @queue.push(event) self end |
#queue_depth ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the number of events waiting to be dispatched.
80 81 82 |
# File 'lib/phronomy/testing/fake_scheduler.rb', line 80 def queue_depth @queue.size end |
#tick ⇒ Object?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Dispatch the next queued event. Calls the registered handler (if any) and records the event. Returns the dispatched event, or +nil+ if the queue is empty.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/phronomy/testing/fake_scheduler.rb', line 46 def tick return nil if @queue.empty? event = begin @queue.pop(true) rescue nil end return nil unless event @dispatched << event handler = @handlers[event.class] || @handlers[:any] handler&.call(event) event end |
#tick_until_idle(max_ticks: 1000) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Dispatch events until the queue is empty. Bounded by +max_ticks+ to prevent infinite loops.
68 69 70 71 72 73 74 75 |
# File 'lib/phronomy/testing/fake_scheduler.rb', line 68 def tick_until_idle(max_ticks: 1000) count = 0 while !@queue.empty? && count < max_ticks tick count += 1 end count end |