t/lib.perl: fix Perl integration tests w/o installation
[unicorn.git] / t / heartbeat-timeout.t
blob694867a4aa6cc7d370023f45d22dd6e73f5a7a17
1 #!perl -w
2 # Copyright (C) unicorn hackers <unicorn-public@yhbt.net>
3 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
4 use v5.14; BEGIN { require './t/lib.perl' };
5 use autodie;
6 use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
7 mkdir "$tmpdir/alt";
8 my $srv = tcp_server();
9 open my $fh, '>', $u_conf;
10 print $fh <<EOM;
11 pid "$tmpdir/pid"
12 preload_app true
13 stderr_path "$err_log"
14 timeout 3 # WORST FEATURE EVER
15 EOM
16 close $fh;
18 my $ar = unicorn(qw(-E none t/heartbeat-timeout.ru -c), $u_conf, { 3 => $srv });
20 my ($status, $hdr, $wpid) = do_req($srv, 'GET /pid HTTP/1.0');
21 like($status, qr!\AHTTP/1\.[01] 200\b!, 'PID request succeeds');
22 like($wpid, qr/\A[0-9]+\z/, 'worker is running');
24 my $t0 = clock_gettime(CLOCK_MONOTONIC);
25 my $c = tcp_start($srv, 'GET /block-forever HTTP/1.0');
26 vec(my $rvec = '', fileno($c), 1) = 1;
27 is(select($rvec, undef, undef, 6), 1, 'got readiness');
28 $c->blocking(0);
29 is(sysread($c, my $buf, 128), 0, 'got EOF response');
30 my $elapsed = clock_gettime(CLOCK_MONOTONIC) - $t0;
31 ok($elapsed > 3, 'timeout took >3s');
33 my @timeout_err = slurp($err_log);
34 truncate($err_log, 0);
35 is(grep(/timeout \(\d+s > 3s\), killing/, @timeout_err), 1,
36     'noted timeout error') or diag explain(\@timeout_err);
38 # did it respawn?
39 ($status, $hdr, my $new_pid) = do_req($srv, 'GET /pid HTTP/1.0');
40 like($status, qr!\AHTTP/1\.[01] 200\b!, 'PID request succeeds');
41 isnt($new_pid, $wpid, 'spawned new worker');
43 diag 'SIGSTOP for 4 seconds...';
44 $ar->do_kill('STOP');
45 sleep 4;
46 $ar->do_kill('CONT');
47 for my $i (1..2) {
48         ($status, $hdr, my $spid) = do_req($srv, 'GET /pid HTTP/1.0');
49         like($status, qr!\AHTTP/1\.[01] 200\b!,
50                 "PID request succeeds #$i after STOP+CONT");
51         is($new_pid, $spid, "worker pid unchanged after STOP+CONT #$i");
52         if ($i == 1) {
53                 diag 'sleeping 2s to ensure timeout is not delayed';
54                 sleep 2;
55         }
58 $ar->join('TERM');
59 check_stderr;
60 undef $tmpdir;
62 done_testing;