class AsyncRackTest::ResyncApp

Attributes

app[R]

Public Class Methods

new(app, opts={}) click to toggle source

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

:timeout => Number of seconds before giving up on the async app.
# File lib/async_rack_test/resync_app.rb, line 10
def initialize(app, opts={})
  @app = app
  @timeout = opts[:timeout] || 5
end

Public Instance Methods

call(env) click to toggle source
# 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 = catch(:async) { app.call(env) }
    if response.nil? || 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

Private Instance Methods

write_async_response(response) click to toggle source
# File lib/async_rack_test/resync_app.rb, line 40
def write_async_response(response)
  @async_response = response
end