Class: Freddy::SyncResponseContainer
- Inherits:
-
Object
- Object
- Freddy::SyncResponseContainer
- Defined in:
- lib/freddy/sync_response_container.rb
Instance Method Summary collapse
- #call(response, delivery) ⇒ Object
-
#initialize(on_timeout) ⇒ SyncResponseContainer
constructor
A new instance of SyncResponseContainer.
- #wait_for_response(timeout) ⇒ Object
Constructor Details
#initialize(on_timeout) ⇒ SyncResponseContainer
Returns a new instance of SyncResponseContainer.
5 6 7 8 9 |
# File 'lib/freddy/sync_response_container.rb', line 5 def initialize(on_timeout) @mutex = Mutex.new @resource = ConditionVariable.new @on_timeout = on_timeout end |
Instance Method Details
#call(response, delivery) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/freddy/sync_response_container.rb', line 11 def call(response, delivery) if response.nil? raise StandardError, 'unexpected nil value for response' end @response = response @delivery = delivery @mutex.synchronize { @resource.signal } end |
#wait_for_response(timeout) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/freddy/sync_response_container.rb', line 21 def wait_for_response(timeout) @mutex.synchronize { @response || @resource.wait(@mutex, timeout) } if !@response @on_timeout.call raise TimeoutError.new( error: 'RequestTimeout', message: 'Timed out waiting for response' ) elsif !@delivery || @delivery.type == 'error' raise InvalidRequestError.new(@response) else @response end end |