Skip to content

Commit

Permalink
Merge pull request #2 from paviensky/master
Browse files Browse the repository at this point in the history
support for :async
  • Loading branch information
thoughtless committed Sep 30, 2013
2 parents f0fab6d + 93daca4 commit 380163b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/async_rack_test/resync_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def call(env)
result = nil
env['async.callback'] = method(:write_async_response)
EM.run do
response = app.call(env)
if response[0] == -1
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
Expand Down
22 changes: 22 additions & 0 deletions spec/async_rack_test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ def app
@result.should == [200, {}, []]
end

it "should return when the passed in app throws :async", :slow => true do
@trigger_async = nil
async_app = Proc.new do |env|
EM.add_periodic_timer(0.01) do
if @trigger_async # This lets us control when the async behavior actually happens.
env['async.callback'].call [200, {}, []]
end
end
throw :async
end
resync_app = AsyncRackTest::ResyncApp.new(async_app)

@result = nil
thread = Thread.new { @result = resync_app.call({}) }

sleep 1 # Give some time to guarantee the thread has had time to run.
@result.should be_nil
@trigger_async = true
thread.join
@result.should == [200, {}, []]
end

it "should return instantly when the passed in app is not async", :slow => true do
@trigger_async = nil
async_app = Proc.new do |env|
Expand Down

0 comments on commit 380163b

Please sign in to comment.