diff options
author | Eric Wong <normalperson@yhbt.net> | 2011-01-10 18:07:21 -0800 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2011-01-11 13:24:23 -0800 |
commit | 69d8ec2f167eb99a1efec4a770ba5951e7c366f0 (patch) | |
tree | 4a809940fdcfa1f596d48401f7d7e2426ae3453b /t/async_chunk_app.ru | |
parent | bf6eb8f1f79ed57ee317b2e6f73c70a59708739f (diff) | |
download | rainbows-69d8ec2f167eb99a1efec4a770ba5951e7c366f0.tar.gz |
The lack of an equivlent to EM::Deferrable prevents us from doing streaming/trickling responses, but a one-shot body should work fine for Coolio and generating dynamic responses.
Diffstat (limited to 't/async_chunk_app.ru')
-rw-r--r-- | t/async_chunk_app.ru | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/t/async_chunk_app.ru b/t/async_chunk_app.ru index 26b9915..007d7b2 100644 --- a/t/async_chunk_app.ru +++ b/t/async_chunk_app.ru @@ -17,27 +17,45 @@ class DeferrableChunkBody def finish @body_callback.call("0\r\n\r\n") end -end +end if defined?(EventMachine) class AsyncChunkApp def call(env) - body = DeferrableChunkBody.new - body.callback { body.finish } headers = { 'Content-Type' => 'text/plain', 'Transfer-Encoding' => 'chunked', } - EM.next_tick { - env['async.callback'].call([ 200, headers, body ]) - } - EM.add_timer(1) { - body.call "Hello " + delay = env["HTTP_X_DELAY"].to_i + + case env["rainbows.model"] + when :EventMachine, :NeverBlock + body = DeferrableChunkBody.new + body.callback { body.finish } + task = lambda { + env['async.callback'].call([ 200, headers, body ]) + EM.add_timer(1) { + body.call "Hello " - EM.add_timer(1) { - body.call "World #{env['PATH_INFO']}\n" - body.succeed + EM.add_timer(1) { + body.call "World #{env['PATH_INFO']}\n" + body.succeed + } + } } - } + delay == 0 ? EM.next_tick(&task) : EM.add_timer(delay, &task) + when :Coolio + # Cool.io only does one-shot responses due to the lack of the + # equivalent of EM::Deferrables + body = [ "Hello ", "World #{env['PATH_INFO']}\n", '' ].map do |chunk| + "#{chunk.size.to_s(16)}\r\n#{chunk}\r\n" + end + + next_tick = Coolio::TimerWatcher.new(delay, false) + next_tick.on_timer { env['async.callback'].call([ 200, headers, body ]) } + next_tick.attach(Coolio::Loop.default) + else + raise "Not supported: #{env['rainbows.model']}" + end nil end end |