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.
7 8 9 10 11 |
# File 'lib/freddy/sync_response_container.rb', line 7 def initialize(on_timeout) @mutex = Mutex.new @resource = ConditionVariable.new @on_timeout = on_timeout end |
Instance Method Details
#call(response, delivery) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/freddy/sync_response_container.rb', line 13 def call(response, delivery) raise StandardError, 'unexpected nil value for response' if response.nil? @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, @response else @response end end |