Class: Freddy::SyncResponseContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/freddy/sync_response_container.rb

Instance Method Summary collapse

Constructor Details

#initializeSyncResponseContainer

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

#wait_for_response(timeout) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/freddy/sync_response_container.rb', line 16

def wait_for_response(timeout)
  @mutex.synchronize do
    @waiting = Thread.current
    @mutex.sleep(timeout)
  end

  if @response.nil?
    raise Timeout::Error, 'execution expired'
  elsif @response[:error] == 'RequestTimeout'
    raise TimeoutError.new(@response)
  elsif !@delivery || @delivery.type == 'error'
    raise InvalidRequestError.new(@response)
  else
    @response
  end
end