Class: Phronomy::Task::ImmediateBackend
- Defined in:
- lib/phronomy/task/immediate_backend.rb
Overview
Synchronous task backend that executes the block on the calling thread.
Used by Runtime::FakeScheduler to allow tests to verify cooperative scheduling behaviour without spawning additional Threads. The block runs to completion before #initialize returns, so #await and #join always return immediately.
Thread count invariant: +ImmediateBackend+ never creates a new Thread.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
private
Always +false+ — block has already completed by the time the task is visible to callers.
-
#await ⇒ Object
private
Returns the block's return value, or re-raises its exception.
-
#cancel! ⇒ self
private
No-op: the block has already completed.
- #completed_error ⇒ Exception? private
- #completed_value ⇒ Object? private
-
#initialize(task:, &block) ⇒ ImmediateBackend
constructor
private
Executes +block+ synchronously on the calling thread.
-
#join(_limit = nil) ⇒ self
private
Returns immediately — nothing to wait for.
Constructor Details
#initialize(task:, &block) ⇒ ImmediateBackend
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.
Executes +block+ synchronously on the calling thread. Saves and restores +Task.current+ so nested ImmediateBackend tasks compose correctly.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/phronomy/task/immediate_backend.rb', line 21 def initialize(task:, &block) super @value = nil @error = nil previous_task = Thread.current[:phronomy_current_task] Thread.current[:phronomy_current_task] = task task.transition!(:running) begin @value = block.call task.transition!(:completed, value: @value) rescue CancellationError => e task.transition!(:cancelled, error: e) @error = e rescue => e task.transition!(:failed, error: e) @error = e ensure task.transition!(:cancelled) unless task.done? Thread.current[:phronomy_current_task] = previous_task end end |
Instance Method Details
#alive? ⇒ 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.
Always +false+ — block has already completed by the time the task is visible to callers.
69 70 71 |
# File 'lib/phronomy/task/immediate_backend.rb', line 69 def alive? false end |
#await ⇒ 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.
Returns the block's return value, or re-raises its exception.
47 48 49 50 51 |
# File 'lib/phronomy/task/immediate_backend.rb', line 47 def await raise @error if @error @value end |
#cancel! ⇒ 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.
No-op: the block has already completed.
76 77 78 |
# File 'lib/phronomy/task/immediate_backend.rb', line 76 def cancel! self end |
#completed_error ⇒ Exception?
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.
61 62 63 |
# File 'lib/phronomy/task/immediate_backend.rb', line 61 def completed_error @error end |
#completed_value ⇒ 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.
55 56 57 |
# File 'lib/phronomy/task/immediate_backend.rb', line 55 def completed_value @value end |
#join(_limit = nil) ⇒ 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.
Returns immediately — nothing to wait for.
84 85 86 |
# File 'lib/phronomy/task/immediate_backend.rb', line 84 def join(_limit = nil) self end |