]> Sergey Matveev's repositories - public-inbox.git/blobdiff - t/httpd-corner.t
t/*.t: disable nntpd/httpd worker processes in most tests
[public-inbox.git] / t / httpd-corner.t
index f5937e7640187a525245927965204667174f54a0..75573c3ef23a423a1238320b2b70cea592834a19 100644 (file)
@@ -6,10 +6,9 @@ use strict;
 use warnings;
 use Test::More;
 use Time::HiRes qw(gettimeofday tv_interval);
-use PublicInbox::Spawn qw(which);
+use PublicInbox::Spawn qw(which spawn);
 
-foreach my $mod (qw(Plack::Util Plack::Builder
-                       HTTP::Date HTTP::Status IPC::Run)) {
+foreach my $mod (qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status)) {
        eval "require $mod";
        plan skip_all => "$mod missing for httpd-corner.t" if $@;
 }
@@ -29,7 +28,10 @@ my $err = "$tmpdir/stderr.log";
 my $out = "$tmpdir/stdout.log";
 my $httpd = 'blib/script/public-inbox-httpd';
 my $psgi = "./t/httpd-corner.psgi";
-my $sock = tcp_server();
+my $sock = tcp_server() or die;
+
+# make sure stdin is not a pipe for lsof test to check for leaking pipes
+open(STDIN, '<', '/dev/null') or die 'no /dev/null: $!';
 
 # Make sure we don't clobber socket options set by systemd or similar
 # using socket activation:
@@ -61,9 +63,22 @@ my $spawn_httpd = sub {
        ok(defined $pid, 'forked httpd process successfully');
 };
 
-{
-       ok($sock, 'sock created');
-       $spawn_httpd->('-W0');
+$spawn_httpd->();
+if ('test worker death') {
+       my $conn = conn_for($sock, 'killed worker');
+       $conn->write("GET /pid HTTP/1.0\r\n\r\n");
+       ok($conn->read(my $buf, 8192), 'read response');
+       my ($head, $body) = split(/\r\n\r\n/, $buf);
+       like($body, qr/\A[0-9]+\z/, '/pid response');
+       my $pid = $body;
+       is(kill('KILL', $pid), 1, 'killed worker');
+
+       $conn = conn_for($sock, 'respawned worker');
+       $conn->write("GET /pid HTTP/1.0\r\n\r\n");
+       ok($conn->read($buf, 8192), 'read response');
+       ($head, $body) = split(/\r\n\r\n/, $buf);
+       like($body, qr/\A[0-9]+\z/, '/pid response');
+       isnt($body, $pid, 'respawned worker');
 }
 
 {
@@ -141,7 +156,7 @@ SKIP: {
        $conn->write($payload . "\r\n0\r\n\r\nGET /empty HTTP/1.0\r\n\r\n");
        $conn->read(my $buf, 4096);
        my $lim = 0;
-       $lim++ while ($conn->read($buf, 4096, bytes::length($buf)) && $lim < 9);
+       $lim++ while ($conn->read($buf, 4096, length($buf)) && $lim < 9);
        my $exp = sha1_hex($payload);
        like($buf, qr!\r\n\r\n${exp}HTTP/1\.0 200 OK\r\n!s,
                'chunk parser can handled pipelined requests');
@@ -247,19 +262,21 @@ SKIP: {
        my ($r, $w);
        pipe($r, $w) or die "pipe: $!";
        my $cmd = [qw(curl --tcp-nodelay --no-buffer -T- -HExpect: -sS), $url];
-       my ($out, $err) = ('', '');
-       my $h = IPC::Run::start($cmd, $r, \$out, \$err);
-       $w->autoflush(1);
+       open my $cout, '+>', undef or die;
+       open my $cerr, '>', undef or die;
+       my $rdr = { 0 => fileno($r), 1 => fileno($cout), 2 => fileno($cerr) };
+       my $pid = spawn($cmd, undef, $rdr);
+       close $r or die "close read pipe: $!";
        foreach my $c ('a'..'z') {
                print $w $c or die "failed to write to curl: $!";
                delay();
        }
        close $w or die "close write pipe: $!";
-       close $r or die "close read pipe: $!";
-       IPC::Run::finish($h);
+       waitpid($pid, 0);
        is($?, 0, 'curl exited successfully');
-       is($err, '', 'no errors from curl');
-       is($out, sha1_hex($str), 'read expected body');
+       is(-s $cerr, 0, 'no errors from curl');
+       $cout->seek(0, SEEK_SET);
+       is(<$cout>, sha1_hex($str), 'read expected body');
 
        open my $fh, '-|', qw(curl -sS), "$base/async-big" or die $!;
        my $n = 0;
@@ -269,6 +286,7 @@ SKIP: {
                $n += $r;
                $buf =~ /\A\0+\z/ or $non_zero++;
        }
+       close $fh or die "curl errored out \$?=$?";
        is($n, 30 * 1024 * 1024, 'got expected output from curl');
        is($non_zero, 0, 'read all zeros');
 }