From 295a6c616f8840bc04617a377c04c3422aeebddc Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 5 Jun 2023 10:12:32 +0000 Subject: [PATCH 03/23] port t0018-write-on-close.sh to Perl 5 This doesn't require restarting, so it's a perfect candidate. --- t/integration.ru | 15 +++++++++++++++ t/integration.t | 14 +++++++++++++- t/lib.perl | 2 +- t/t0018-write-on-close.sh | 23 ----------------------- t/write-on-close.ru | 11 ----------- 5 files changed, 29 insertions(+), 36 deletions(-) delete mode 100755 t/t0018-write-on-close.sh delete mode 100644 t/write-on-close.ru diff --git a/t/integration.ru b/t/integration.ru index 5183217..12f5d48 100644 --- a/t/integration.ru +++ b/t/integration.ru @@ -18,6 +18,20 @@ def restore_status_code [ 200, {}, [] ] end +class WriteOnClose + def each(&block) + @callback = block + end + + def close + @callback.call "7\r\nGoodbye\r\n0\r\n\r\n" + end +end + +def write_on_close + [ 200, { 'transfer-encoding' => 'chunked' }, WriteOnClose.new ] +end + run(lambda do |env| case env['REQUEST_METHOD'] when 'GET' @@ -26,6 +40,7 @@ def restore_status_code when '/rack-3-array-headers'; [ 200, { 'x-r3' => %w(a b c) }, [] ] when '/nil-header-value'; [ 200, { 'X-Nil' => nil }, [] ] when '/unknown-status-pass-through'; [ '666 I AM THE BEAST', {}, [] ] + when '/write_on_close'; write_on_close end # case PATH_INFO (GET) when 'POST' case env['PATH_INFO'] diff --git a/t/integration.t b/t/integration.t index e876c71..3ab5c90 100644 --- a/t/integration.t +++ b/t/integration.t @@ -4,6 +4,7 @@ use v5.14; BEGIN { require './t/lib.perl' }; my $srv = tcp_server(); +my $host_port = tcp_host_port($srv); my $t0 = time; my $ar = unicorn(qw(-E none t/integration.ru), { 3 => $srv }); @@ -66,8 +67,19 @@ if ('TODO: ensure Rack::Utils::HTTP_STATUS_CODES is available') { is($status, $orig_200_status, 'original status restored'); } +SKIP: { + eval { require HTTP::Tiny } or skip "HTTP::Tiny missing: $@", 1; + my $ht = HTTP::Tiny->new; + my $res = $ht->get("http://$host_port/write_on_close"); + is($res->{content}, 'Goodbye', 'write-on-close body read'); +} # ... more stuff here undef $ar; -diag slurp("$tmpdir/err.log") if $ENV{V}; +my @log = slurp("$tmpdir/err.log"); +diag("@log") if $ENV{V}; +my @err = grep(!/NameError.*Unicorn::Waiter/, grep(/error/i, @log)); +is_deeply(\@err, [], 'no unexpected errors in stderr'); +is_deeply([grep(/SIGKILL/, @log)], [], 'no SIGKILL in stderr'); + done_testing; diff --git a/t/lib.perl b/t/lib.perl index dd9c6b7..12deaf8 100644 --- a/t/lib.perl +++ b/t/lib.perl @@ -10,7 +10,7 @@ use POSIX qw(dup2 _exit setpgid :signal_h SEEK_SET F_SETFD); use File::Temp 0.19 (); # 0.19 for ->newdir our ($tmpdir, $errfh); our @EXPORT = qw(unicorn slurp tcp_server tcp_connect unicorn $tmpdir $errfh - SEEK_SET); + SEEK_SET tcp_host_port); my ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!); $tmpdir = File::Temp->newdir("unicorn-$base-XXXX", TMPDIR => 1); diff --git a/t/t0018-write-on-close.sh b/t/t0018-write-on-close.sh deleted file mode 100755 index 3afefea..0000000 --- a/t/t0018-write-on-close.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/sh -. ./test-lib.sh -t_plan 4 "write-on-close tests for funky response-bodies" - -t_begin "setup and start" && { - unicorn_setup - unicorn -D -c $unicorn_config write-on-close.ru - unicorn_wait_start -} - -t_begin "write-on-close response body succeeds" && { - test xGoodbye = x"$(curl -sSf http://$listen/)" -} - -t_begin "killing succeeds" && { - kill $unicorn_pid -} - -t_begin "check stderr" && { - check_stderr -} - -t_done diff --git a/t/write-on-close.ru b/t/write-on-close.ru deleted file mode 100644 index 725c4d6..0000000 --- a/t/write-on-close.ru +++ /dev/null @@ -1,11 +0,0 @@ -class WriteOnClose - def each(&block) - @callback = block - end - - def close - @callback.call "7\r\nGoodbye\r\n0\r\n\r\n" - end -end -use Rack::ContentType, "text/plain" -run(lambda { |_| [ 200, { 'transfer-encoding' => 'chunked' }, WriteOnClose.new ] })