From 0cd65fa1e01be369b270c72053cf21a3d6bcb45f Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 19 Jul 2010 10:10:05 +0000 Subject: ensure stream response bodies get closed Some middlewares such as Clogger rely on wrapping the body having the close method called on it for logging. --- t/close-pipe-response.ru | 26 ++++++++++++++++++++++++++ t/t0031-close-pipe-response.sh | 30 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 t/close-pipe-response.ru create mode 100644 t/t0031-close-pipe-response.sh (limited to 't') diff --git a/t/close-pipe-response.ru b/t/close-pipe-response.ru new file mode 100644 index 0000000..96116d4 --- /dev/null +++ b/t/close-pipe-response.ru @@ -0,0 +1,26 @@ +# must be run without Rack::Lint since that clobbers to_path +class CloseWrapper < Struct.new(:to_io) + def each(&block) + to_io.each(&block) + end + + def close + ::File.open(ENV['fifo'], 'wb') do |fp| + fp.syswrite("CLOSING #{to_io}\n") + if to_io.respond_to?(:close) && ! to_io.closed? + to_io.close + end + end + end +end +use Rainbows::DevFdResponse +run(lambda { |env| + body = 'hello world' + io = IO.popen("echo '#{body}'", 'rb') + [ 200, + { + 'Content-Length' => (body.size + 1).to_s, + 'Content-Type' => 'application/octet-stream', + }, + CloseWrapper[io] ] +}) diff --git a/t/t0031-close-pipe-response.sh b/t/t0031-close-pipe-response.sh new file mode 100644 index 0000000..7439b5f --- /dev/null +++ b/t/t0031-close-pipe-response.sh @@ -0,0 +1,30 @@ +#!/bin/sh +. ./test-lib.sh + +t_plan 5 "close pipe response for $model" + +t_begin "setup and startup" && { + rtmpfiles err out + rainbows_setup $model + export fifo + rainbows -E none -D close-pipe-response.ru -c $unicorn_config + rainbows_wait_start +} + +t_begin "single request matches" && { + cat $fifo > $out & + test x'hello world' = x"$(curl -sSfv 2> $err http://$listen/)" +} + +t_begin "body.close called" && { + wait # for cat $fifo + grep CLOSING $out || die "body.close not logged" +} + +t_begin "shutdown server" && { + kill -QUIT $rainbows_pid +} + +t_begin "check stderr" && check_stderr + +t_done -- cgit v1.2.3-24-ge0c7