From b61427f5393f86bbae97a139da83a1b1606386ad Mon Sep 17 00:00:00 2001 From: Radek Paviensky Date: Mon, 25 Mar 2013 19:53:14 +0100 Subject: [PATCH 1/2] support for throw :async --- lib/async_rack_test/resync_app.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/async_rack_test/resync_app.rb b/lib/async_rack_test/resync_app.rb index ee28b7b..ed462f3 100644 --- a/lib/async_rack_test/resync_app.rb +++ b/lib/async_rack_test/resync_app.rb @@ -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 From 93daca439852df1d854e293c52e4cee52f705aa3 Mon Sep 17 00:00:00 2001 From: Radek Paviensky Date: Tue, 26 Mar 2013 01:45:45 +0100 Subject: [PATCH 2/2] spec for :async support --- spec/async_rack_test_spec.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/spec/async_rack_test_spec.rb b/spec/async_rack_test_spec.rb index 37779f9..84155ed 100644 --- a/spec/async_rack_test_spec.rb +++ b/spec/async_rack_test_spec.rb @@ -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|