From d67284a692683bca59effd9c0670bd5dd47e4fa3 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 7 Sep 2023 23:53:58 +0000 Subject: [PATCH 03/11] tests: port t/heartbeat-timeout to Perl 5 I absolutely detest and regret adding this feature, but I'm hell bent on supporting it until the end of days because we don't break compatibility. --- t/heartbeat-timeout.ru | 2 +- t/heartbeat-timeout.t | 69 ++++++++++++++++++++++++++++++++++++ t/t0004-heartbeat-timeout.sh | 69 ------------------------------------ 3 files changed, 70 insertions(+), 70 deletions(-) create mode 100644 t/heartbeat-timeout.t delete mode 100755 t/t0004-heartbeat-timeout.sh diff --git a/t/heartbeat-timeout.ru b/t/heartbeat-timeout.ru index 20a79380..3eeb5d64 100644 --- a/t/heartbeat-timeout.ru +++ b/t/heartbeat-timeout.ru @@ -7,6 +7,6 @@ sleep # in case STOP signal is not received in time [ 500, headers, [ "Should never get here\n" ] ] else - [ 200, headers, [ "#$$\n" ] ] + [ 200, headers, [ "#$$" ] ] end } diff --git a/t/heartbeat-timeout.t b/t/heartbeat-timeout.t new file mode 100644 index 00000000..1fcf21a2 --- /dev/null +++ b/t/heartbeat-timeout.t @@ -0,0 +1,69 @@ +#!perl -w +# Copyright (C) unicorn hackers +# License: GPL-3.0+ +use v5.14; BEGIN { require './t/lib.perl' }; +use autodie; +use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC); +mkdir "$tmpdir/alt"; +my $srv = tcp_server(); +my $u_conf = "$tmpdir/u.conf.rb"; +open my $fh, '>', $u_conf; +print $fh < $srv }); + +my $c = tcp_start($srv, 'GET /pid HTTP/1.0'); +my ($status, $hdr) = slurp_hdr($c); +like($status, qr!\AHTTP/1\.[01] 200\b!, 'PID request succeeds'); +my $wpid = do { local $/; <$c> }; +like($wpid, qr/\A[0-9]+\z/, 'worker is running'); + +my $t0 = clock_gettime(CLOCK_MONOTONIC); +$c = tcp_start($srv, 'GET /block-forever HTTP/1.0'); +vec(my $rvec = '', fileno($c), 1) = 1; +is(select($rvec, undef, undef, 6), 1, 'got readiness'); +$c->blocking(0); +is(sysread($c, my $buf, 128), 0, 'got EOF response'); +my $elapsed = clock_gettime(CLOCK_MONOTONIC) - $t0; +ok($elapsed > 3, 'timeout took >3s'); + +my @timeout_err = slurp($err_log); +truncate($err_log, 0); +is(grep(/timeout \(\d+s > 3s\), killing/, @timeout_err), 1, + 'noted timeout error') or diag explain(\@timeout_err); + +# did it respawn? +$c = tcp_start($srv, 'GET /pid HTTP/1.0'); +($status, $hdr) = slurp_hdr($c); +like($status, qr!\AHTTP/1\.[01] 200\b!, 'PID request succeeds'); +my $new_pid = do { local $/; <$c> }; +isnt($new_pid, $wpid, 'spawned new worker'); + +diag 'SIGSTOP for 4 seconds...'; +$ar->do_kill('STOP'); +sleep 4; +$ar->do_kill('CONT'); +for my $i (1..2) { + $c = tcp_start($srv, 'GET /pid HTTP/1.0'); + ($status, $hdr) = slurp_hdr($c); + like($status, qr!\AHTTP/1\.[01] 200\b!, + "PID request succeeds #$i after STOP+CONT"); + my $spid = do { local $/; <$c> }; + is($new_pid, $spid, "worker pid unchanged after STOP+CONT #$i"); + if ($i == 1) { + diag 'sleeping 2s to ensure timeout is not delayed'; + sleep 2; + } +} + +$ar->join('TERM'); +check_stderr; +undef $tmpdir; + +done_testing; diff --git a/t/t0004-heartbeat-timeout.sh b/t/t0004-heartbeat-timeout.sh deleted file mode 100755 index 29652837..00000000