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 ⇒ SyncResponseContainer
constructor
A new instance of SyncResponseContainer.
- #on_timeout(&block) ⇒ Object
- #wait_for_response(timeout) ⇒ Object
Constructor Details
#initialize ⇒ SyncResponseContainer
Returns a new instance of SyncResponseContainer.
6 7 8 |
# File 'lib/freddy/sync_response_container.rb', line 6 def initialize @mutex = Mutex.new end |
Instance Method Details
#call(response, delivery) ⇒ Object
10 11 12 13 14 |
# File 'lib/freddy/sync_response_container.rb', line 10 def call(response, delivery) @response = response @delivery = delivery @mutex.synchronize { @waiting.wakeup } end |
#on_timeout(&block) ⇒ Object
16 17 18 |
# File 'lib/freddy/sync_response_container.rb', line 16 def on_timeout(&block) @on_timeout = block end |
#wait_for_response(timeout) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/freddy/sync_response_container.rb', line 20 def wait_for_response(timeout) @mutex.synchronize do @waiting = Thread.current @mutex.sleep(timeout) end if !defined?(@response) @on_timeout.call raise TimeoutError.new( error: 'RequestTimeout', message: 'Timed out waiting for response' ) elsif @response.nil? raise StandardError, 'unexpected nil value for response' elsif !@delivery || @delivery.type == 'error' raise InvalidRequestError.new(@response) else @response end end |