Class: Concurrent::ImmediateExecutor
- Inherits:
-
Object
- Object
- Concurrent::ImmediateExecutor
- Defined in:
- lib/concurrent/executor/immediate_executor.rb
Overview
Intended for use primarily in testing and debugging.
An executor service which runs all operations on the current thread, blocking as necessary. Operations are performed in the order they are received and no two operations can be performed simultaneously.
This executor service exists mainly for testing an debugging. When used
it immediately runs every #post operation on the current thread, blocking
that thread until the operation is complete. This can be very beneficial
during testing because it makes all operations deterministic.
Direct Known Subclasses
Instance Method Summary collapse
-
#<<(task) ⇒ self
Submit a task to the executor for asynchronous processing.
-
#initialize ⇒ ImmediateExecutor
constructor
Creates a new executor.
-
#post(*args) { ... } ⇒ Boolean
Submit a task to the executor for asynchronous processing.
- #running? ⇒ Boolean
- #shutdown ⇒ Object (also: #kill)
- #shutdown? ⇒ Boolean
- #shuttingdown? ⇒ Boolean
- #wait_for_termination(timeout = nil) ⇒ Object
Constructor Details
#initialize ⇒ ImmediateExecutor
Creates a new executor
20 21 22 |
# File 'lib/concurrent/executor/immediate_executor.rb', line 20 def initialize @stopped = Concurrent::Event.new end |
Instance Method Details
#<<(task) ⇒ self
Submit a task to the executor for asynchronous processing.
33 34 35 36 |
# File 'lib/concurrent/executor/immediate_executor.rb', line 33 def <<(task) post(&task) self end |
#post(*args) { ... } ⇒ Boolean
Submit a task to the executor for asynchronous processing.
25 26 27 28 29 30 |
# File 'lib/concurrent/executor/immediate_executor.rb', line 25 def post(*args, &task) raise ArgumentError.new('no block given') unless block_given? return false unless running? task.call(*args) true end |
#running? ⇒ Boolean
39 40 41 |
# File 'lib/concurrent/executor/immediate_executor.rb', line 39 def running? ! shutdown? end |
#shutdown ⇒ Object Also known as: kill
54 55 56 57 |
# File 'lib/concurrent/executor/immediate_executor.rb', line 54 def shutdown @stopped.set true end |
#shutdown? ⇒ Boolean
49 50 51 |
# File 'lib/concurrent/executor/immediate_executor.rb', line 49 def shutdown? @stopped.set? end |
#shuttingdown? ⇒ Boolean
44 45 46 |
# File 'lib/concurrent/executor/immediate_executor.rb', line 44 def shuttingdown? false end |
#wait_for_termination(timeout = nil) ⇒ Object
61 62 63 |
# File 'lib/concurrent/executor/immediate_executor.rb', line 61 def wait_for_termination(timeout = nil) @stopped.wait(timeout) end |