From 69d8ec2f167eb99a1efec4a770ba5951e7c366f0 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 10 Jan 2011 18:07:21 -0800 Subject: coolio: enable async.callback for one-shot body responses 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. --- t/async_chunk_app.ru | 42 ++++++++++---- t/t0402-async-keepalive.sh | 126 ++++++++++++++++++++++++++++++++++++++++++ t/t0402-em-async-keepalive.sh | 86 ---------------------------- 3 files changed, 156 insertions(+), 98 deletions(-) create mode 100644 t/t0402-async-keepalive.sh delete mode 100644 t/t0402-em-async-keepalive.sh (limited to 't') 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 diff --git a/t/t0402-async-keepalive.sh b/t/t0402-async-keepalive.sh new file mode 100644 index 0000000..fdf4cbc --- /dev/null +++ b/t/t0402-async-keepalive.sh @@ -0,0 +1,126 @@ +#!/bin/sh +DELAY=${DELAY-1} +. ./test-lib.sh +case $model in +Coolio|NeverBlock|EventMachine) ;; +*) + t_info "skipping $T since it's not compatible with $model" + exit 0 + ;; +esac + +t_plan 11 "async_chunk_app test for test for $model" + +CONFIG_RU=async_chunk_app.ru + +t_begin "setup and start" && { + rainbows_setup + rtmpfiles a b c curl_err expect + + # this does not does not support Rack::Lint + rainbows -E none -D $CONFIG_RU -c $unicorn_config + rainbows_wait_start + + echo 'Hello World /0' >> $expect + echo 'Hello World /1' >> $expect + echo 'Hello World /2' >> $expect +} + +t_begin "async.callback supports pipelining" && { + rm -f $tmp + t0=$(date +%s) + ( + cat $fifo > $tmp & + printf 'GET /0 HTTP/1.1\r\nHost: example.com\r\n\r\n' + printf 'GET /1 HTTP/1.1\r\nHost: example.com\r\n\r\n' + printf 'GET /2 HTTP/1.0\r\nHost: example.com\r\n\r\n' + wait + ) | socat - TCP:$listen > $fifo + t1=$(date +%s) + elapsed=$(( $t1 - $t0 )) + t_info "elapsed=$elapsed $model.$0 ($t_current)" + test 3 -eq "$(fgrep 'HTTP/1.1 200 OK' $tmp | wc -l)" + test 3 -eq "$(grep '^Hello ' $tmp | wc -l)" + test 3 -eq "$(grep 'World ' $tmp | wc -l)" +} + +t_begin "async.callback supports pipelining with delay $DELAY" && { + rm -f $tmp + t0=$(date +%s) + ( + cat $fifo > $tmp & + printf 'GET /0 HTTP/1.1\r\nX-Delay: %d\r\n' $DELAY + printf 'Host: example.com\r\n\r\n' + printf 'GET /1 HTTP/1.1\r\nX-Delay: %d\r\n' $DELAY + printf 'Host: example.com\r\n\r\n' + printf 'GET /2 HTTP/1.0\r\nX-Delay: %d\r\n' $DELAY + printf 'Host: example.com\r\n\r\n' + wait + ) | socat - TCP:$listen > $fifo + t1=$(date +%s) + elapsed=$(( $t1 - $t0 )) + min=$(( $DELAY * 3 )) + t_info "elapsed=$elapsed $model.$0 ($t_current) min=$min" + test $elapsed -ge $min + test 3 -eq "$(fgrep 'HTTP/1.1 200 OK' $tmp | wc -l)" + test 3 -eq "$(grep '^Hello ' $tmp | wc -l)" + test 3 -eq "$(grep 'World ' $tmp | wc -l)" +} + +t_begin "async.callback supports keepalive" && { + t0=$(date +%s) + curl -v --no-buffer -sSf http://$listen/[0-2] > $tmp 2>> $curl_err + t1=$(date +%s) + elapsed=$(( $t1 - $t0 )) + t_info "elapsed=$elapsed $model.$0 ($t_current)" + cmp $expect $tmp + test 2 -eq "$(fgrep 'Re-using existing connection!' $curl_err |wc -l)" + rm -f $curl_err +} + +t_begin "async.callback supports keepalive with delay $DELAY" && { + t0=$(date +%s) + curl -v --no-buffer -sSf -H "X-Delay: $DELAY" \ + http://$listen/[0-2] > $tmp 2>> $curl_err + t1=$(date +%s) + elapsed=$(( $t1 - $t0 )) + min=$(( $DELAY * 3 )) + t_info "elapsed=$elapsed $model.$0 ($t_current) min=$min" + test $elapsed -ge $min + cmp $expect $tmp + test 2 -eq "$(fgrep 'Re-using existing connection!' $curl_err |wc -l)" + rm -f $curl_err +} + +t_begin "send async requests off in parallel" && { + t0=$(date +%s) + curl --no-buffer -sSf http://$listen/[0-2] > $a 2>> $curl_err & + curl --no-buffer -sSf http://$listen/[0-2] > $b 2>> $curl_err & + curl --no-buffer -sSf http://$listen/[0-2] > $c 2>> $curl_err & +} + +t_begin "wait for curl terminations" && { + wait + t1=$(date +%s) + elapsed=$(( $t1 - $t0 )) + t_info "elapsed=$elapsed" +} + +t_begin "termination signal sent" && { + kill $rainbows_pid +} + +t_begin "no errors from curl" && { + test ! -s $curl_err +} + +t_begin "no errors in stderr" && check_stderr + +t_begin "responses match expected" && { + cmp $expect $a + cmp $expect $b + cmp $expect $c +} + +t_done + diff --git a/t/t0402-em-async-keepalive.sh b/t/t0402-em-async-keepalive.sh deleted file mode 100644 index 24eb678..0000000 --- a/t/t0402-em-async-keepalive.sh +++ /dev/null @@ -1,86 +0,0 @@ -#!/bin/sh -. ./test-lib.sh -case $model in -NeverBlock|EventMachine) ;; -*) - t_info "skipping $T since it's not compatible with $model" - exit 0 - ;; -esac - -t_plan 9 "async_chunk_app test for test for EM" - -CONFIG_RU=async_chunk_app.ru - -t_begin "setup and start" && { - rainbows_setup - rtmpfiles a b c curl_err expect - - # this does not does not support Rack::Lint - rainbows -E none -D $CONFIG_RU -c $unicorn_config - rainbows_wait_start - - echo 'Hello World /0' >> $expect - echo 'Hello World /1' >> $expect - echo 'Hello World /2' >> $expect -} - -t_begin "async.callback supports pipelining" && { - rm -f $tmp - t0=$(date +%s) - ( - cat $fifo > $tmp & - printf 'GET /0 HTTP/1.1\r\nHost: example.com\r\n\r\n' - printf 'GET /1 HTTP/1.1\r\nHost: example.com\r\n\r\n' - printf 'GET /2 HTTP/1.0\r\nHost: example.com\r\n\r\n' - wait - ) | socat - TCP:$listen > $fifo - t1=$(date +%s) - elapsed=$(( $t1 - $t0 )) - t_info "elapsed=$elapsed $model.$0 ($t_current)" - test 3 -eq "$(fgrep 'HTTP/1.1 200 OK' $tmp | wc -l)" -} - -t_begin "async.callback supports keepalive" && { - t0=$(date +%s) - curl -v --no-buffer -sSf http://$listen/[0-2] > $tmp 2>> $curl_err - t1=$(date +%s) - elapsed=$(( $t1 - $t0 )) - t_info "elapsed=$elapsed $model.$0 ($t_current)" - cmp $expect $tmp - test 2 -eq "$(fgrep 'Re-using existing connection!' $curl_err |wc -l)" - rm -f $curl_err -} - -t_begin "send async requests off in parallel" && { - t0=$(date +%s) - curl --no-buffer -sSf http://$listen/[0-2] > $a 2>> $curl_err & - curl --no-buffer -sSf http://$listen/[0-2] > $b 2>> $curl_err & - curl --no-buffer -sSf http://$listen/[0-2] > $c 2>> $curl_err & -} - -t_begin "wait for curl terminations" && { - wait - t1=$(date +%s) - elapsed=$(( $t1 - $t0 )) - t_info "elapsed=$elapsed" -} - -t_begin "termination signal sent" && { - kill $rainbows_pid -} - -t_begin "no errors from curl" && { - test ! -s $curl_err -} - -t_begin "no errors in stderr" && check_stderr - -t_begin "responses match expected" && { - cmp $expect $a - cmp $expect $b - cmp $expect $c -} - -t_done - -- cgit v1.2.3-24-ge0c7