Class: AsyncRackTest::ResyncApp

Inherits:
Object
  • Object
show all
Defined in:
lib/async_rack_test/resync_app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ ResyncApp

app => The async app that this is meant to wrap. opts

:timeout => Number of seconds before giving up on the async app.


10
11
12
13
# File 'lib/async_rack_test/resync_app.rb', line 10

def initialize(app, opts={})
  @app = app
  @timeout = opts[:timeout] || 5
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



5
6
7
# File 'lib/async_rack_test/resync_app.rb', line 5

def app
  @app
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/async_rack_test/resync_app.rb', line 15

def call(env)
  result = nil
  env['async.callback'] = method(:write_async_response)
  EM.run do
    response = app.call(env)
    if response[0] == -1
      EM.add_periodic_timer(0.1) do
        unless @async_response.nil?
          result = @async_response
          EM.stop
        end
      end
      EM.add_timer(@timeout) do
        raise Timeout, "Did not receive a response from the app within #{@timeout} seconds--app: #{app.inspect}"
      end
    else
      result = response
      EM.stop
    end
  end
  result
end