]> Sergey Matveev's repositories - public-inbox.git/commitdiff
tests: consolidate process spawning code.
authorEric Wong <e@80x24.org>
Wed, 26 Dec 2018 09:07:49 +0000 (09:07 +0000)
committerEric Wong <e@80x24.org>
Sat, 29 Dec 2018 03:45:37 +0000 (03:45 +0000)
IPC::Run provides a nice simplification in several places; and
we already use it (optionally) on a lot of tests.

For the non-test code, we still rely on our vfork-capable
Inline::C stuff since real-world server processes can get large
enough to where vfork is an advantage.  Maybe Perl5 can use
CLONE_VFORK somehow, one day:

  https://rt.perl.org/Ticket/Display.html?id=128227

Ohg V'q engure cbeg choyvp-vaobk gb Ehol :C

t/common.perl
t/git-http-backend.t
t/git.t
t/httpd-corner.t
t/httpd-unix.t
t/httpd.t
t/nntpd.t
t/perf-nntpd.t
t/v2mirror.t
t/v2writable.t

index 5c5fcd84890dbef23a07c2529c28b8341acc6bd7..688e30ad99cbee6c39a186c289a42eca4d3465e3 100644 (file)
@@ -1,6 +1,9 @@
 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
+use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
+use POSIX qw(dup2);
+
 sub stream_to_string {
        my ($res) = @_;
        my $body = $res->[2];
@@ -12,4 +15,28 @@ sub stream_to_string {
        $str;
 }
 
+sub spawn_listener {
+       my ($env, $cmd, $socks) = @_;
+       my $pid = fork;
+       defined $pid or die "fork failed: $!\n";
+       if ($pid == 0) {
+               # pretend to be systemd (cf. sd_listen_fds(3))
+               my $fd = 3; # 3 == SD_LISTEN_FDS_START
+               foreach my $s (@$socks) {
+                       my $fl = fcntl($s, F_GETFD, 0);
+                       if (($fl & FD_CLOEXEC) != FD_CLOEXEC) {
+                               warn "got FD:".fileno($s)." w/o CLOEXEC\n";
+                       }
+                       fcntl($s, F_SETFD, $fl &= ~FD_CLOEXEC);
+                       dup2(fileno($s), $fd++) or die "dup2 failed: $!\n";
+               }
+               $ENV{LISTEN_PID} = $$;
+               $ENV{LISTEN_FDS} = scalar @$socks;
+               %ENV = (%ENV, %$env) if $env;
+               exec @$cmd;
+               die "FAIL: ",join(' ', @$cmd), ": $!\n";
+       }
+       $pid;
+}
+
 1;
index 046a77849fe2e43f758de4c2a481206d412cedd0..4e51f2bf6bfc88d2b7eac758ebf32ce12db1fb00 100644 (file)
@@ -5,7 +5,6 @@ use warnings;
 use Test::More;
 use File::Temp qw/tempdir/;
 use IO::Socket;
-use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
 use POSIX qw(dup2 setsid);
 use Cwd qw(getcwd);
@@ -18,6 +17,7 @@ foreach my $mod (qw(Danga::Socket BSD::Resource
        eval "require $mod";
        plan skip_all => "$mod missing for git-http-backend.t" if $@;
 }
+require './t/common.perl';
 my $psgi = getcwd()."/t/git-http-backend.psgi";
 my $tmpdir = tempdir('pi-git-http-backend-XXXXXX', TMPDIR => 1, CLEANUP => 1);
 my $err = "$tmpdir/stderr.log";
@@ -51,16 +51,9 @@ my $get_maxrss = sub {
 
 {
        ok($sock, 'sock created');
-       $pid = fork;
-       if ($pid == 0) { # pretend to be systemd
-               fcntl($sock, F_SETFD, 0);
-               dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
-               $ENV{LISTEN_PID} = $$;
-               $ENV{LISTEN_FDS} = 1;
-               exec $httpd, "--stdout=$out", "--stderr=$err", $psgi;
-               die "FAIL: $!\n";
-       }
-       ok(defined $pid, 'forked httpd process successfully');
+       my $cmd = [ $httpd, "--stdout=$out", "--stderr=$err", $psgi ];
+       ok(defined($pid = spawn_listener(undef, $cmd, [$sock])),
+          'forked httpd process successfully');
 }
 my $mem_a = $get_maxrss->();
 
diff --git a/t/git.t b/t/git.t
index 7f96293fb25a1511452ce1e242822afa5791b49f..5069ae2c42c68202aea2a0120dbfbd908e86a9a3 100644 (file)
--- a/t/git.t
+++ b/t/git.t
@@ -9,20 +9,15 @@ use Cwd qw/getcwd/;
 use PublicInbox::Spawn qw(popen_rd);
 
 use_ok 'PublicInbox::Git';
+eval { require IPC::Run } or plan skip_all => 'IPC::Run missing';
+
 {
        is(system(qw(git init -q --bare), $dir), 0, 'created git directory');
-       my @cmd = ('git', "--git-dir=$dir", 'fast-import', '--quiet');
+       my $cmd = [ 'git', "--git-dir=$dir", 'fast-import', '--quiet' ];
 
        my $fi_data = getcwd().'/t/git.fast-import-data';
        ok(-r $fi_data, "fast-import data readable (or run test at top level)");
-       my $pid = fork;
-       defined $pid or die "fork failed: $!\n";
-       if ($pid == 0) {
-               open STDIN, '<', $fi_data or die "open $fi_data: $!\n";
-               exec @cmd;
-               die "failed exec: ",join(' ', @cmd),": $!\n";
-       }
-       waitpid $pid, 0;
+       IPC::Run::run($cmd, '<', $fi_data);
        is($?, 0, 'fast-import succeeded');
 }
 
@@ -69,8 +64,7 @@ use_ok 'PublicInbox::Git';
 }
 
 if (1) {
-       use POSIX qw(dup2);
-       my @cmd = ('git', "--git-dir=$dir", qw(hash-object -w --stdin));
+       my $cmd = [ 'git', "--git-dir=$dir", qw(hash-object -w --stdin) ];
 
        # need a big file, use the AGPL-3.0 :p
        my $big_data = getcwd().'/COPYING';
@@ -78,21 +72,8 @@ if (1) {
        my $size = -s $big_data;
        ok($size > 8192, 'file is big enough');
 
-       my ($r, $w);
-       ok(pipe($r, $w), 'created pipe');
-
-       my $pid = fork;
-       defined $pid or die "fork failed: $!\n";
-       if ($pid == 0) {
-               close $r;
-               open STDIN, '<', $big_data or die "open $big_data: $!\n";
-               dup2(fileno($w), 1);
-               exec @cmd;
-               die "failed exec: ",join(' ', @cmd),": $!\n";
-       }
-       close $w;
-       my $n = read $r, my $buf, 41;
-       waitpid $pid, 0;
+       my $buf = '';
+       IPC::Run::run($cmd, '<', $big_data, '>', \$buf);
        is(0, $?, 'hashed object successfully');
        chomp $buf;
 
index a720670ec774e00a651db8767b89ac263de1b04c..aa0698d3cd2b830f0bfc73ec350f74fffc721ca8 100644 (file)
@@ -8,7 +8,7 @@ use Test::More;
 use Time::HiRes qw(gettimeofday tv_interval);
 
 foreach my $mod (qw(Plack::Util Plack::Builder Danga::Socket
-                       HTTP::Date HTTP::Status)) {
+                       HTTP::Date HTTP::Status IPC::Run)) {
        eval "require $mod";
        plan skip_all => "$mod missing for httpd-corner.t" if $@;
 }
@@ -18,9 +18,10 @@ use File::Temp qw/tempdir/;
 use Cwd qw/getcwd/;
 use IO::Socket;
 use IO::Socket::UNIX;
-use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD :seek);
+use Fcntl qw(:seek);
 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
-use POSIX qw(dup2 mkfifo :sys_wait_h);
+use POSIX qw(mkfifo :sys_wait_h);
+require './t/common.perl';
 my $tmpdir = tempdir('httpd-corner-XXXXXX', TMPDIR => 1, CLEANUP => 1);
 my $fifo = "$tmpdir/fifo";
 ok(defined mkfifo($fifo, 0777), 'created FIFO');
@@ -47,33 +48,13 @@ my $pid;
 END { kill 'TERM', $pid if defined $pid };
 my $spawn_httpd = sub {
        my (@args) = @_;
-       $! = 0;
-       my $fl = fcntl($sock, F_GETFD, 0);
-       ok(! $!, 'no error from fcntl(F_GETFD)');
-       is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
-       $pid = fork;
-       if ($pid == 0) {
-               # pretend to be systemd
-               dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
-               dup2(fileno($unix), 4) or die "dup2 failed: $!\n";
-               my $t = IO::Handle->new_from_fd(3, 'r');
-               $t->fcntl(F_SETFD, 0);
-               my $u = IO::Handle->new_from_fd(4, 'r');
-               $u->fcntl(F_SETFD, 0);
-               $ENV{LISTEN_PID} = $$;
-               $ENV{LISTEN_FDS} = 2;
-               exec $httpd, @args, "--stdout=$out", "--stderr=$err", $psgi;
-               die "FAIL: $!\n";
-       }
+       my $cmd = [ $httpd, @args, "--stdout=$out", "--stderr=$err", $psgi ];
+       $pid = spawn_listener(undef, $cmd, [ $sock, $unix ]);
        ok(defined $pid, 'forked httpd process successfully');
 };
 
 {
        ok($sock, 'sock created');
-       $! = 0;
-       my $fl = fcntl($sock, F_GETFD, 0);
-       ok(! $!, 'no error from fcntl(F_GETFD)');
-       is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
        $spawn_httpd->('-W0');
 }
 
@@ -242,7 +223,6 @@ my $check_self = sub {
 };
 
 SKIP: {
-       use POSIX qw(dup2);
        my $have_curl = 0;
        foreach my $p (split(':', $ENV{PATH})) {
                -x "$p/curl" or next;
@@ -254,15 +234,9 @@ SKIP: {
        my $url = 'http://' . $sock->sockhost . ':' . $sock->sockport . '/sha1';
        my ($r, $w);
        pipe($r, $w) or die "pipe: $!";
-       open(my $tout, '+>', undef) or die "open temporary file: $!";
-       my $pid = fork;
-       defined $pid or die "fork: $!";
-       my @cmd = (qw(curl --tcp-nodelay --no-buffer -T- -HExpect: -sS), $url);
-       if ($pid == 0) {
-               dup2(fileno($r), 0) or die "redirect stdin failed: $!\n";
-               dup2(fileno($tout), 1) or die "redirect stdout failed: $!\n";
-               exec(@cmd) or die 'exec `' . join(' '). "' failed: $!\n";
-       }
+       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);
        foreach my $c ('a'..'z') {
                print $w $c or die "failed to write to curl: $!";
@@ -270,11 +244,10 @@ SKIP: {
        }
        close $w or die "close write pipe: $!";
        close $r or die "close read pipe: $!";
-       my $kid = waitpid $pid, 0;
+       IPC::Run::finish($h);
        is($?, 0, 'curl exited successfully');
-       $tout->sysseek(0, SEEK_SET);
-       $tout->sysread(my $buf, 100);
-       is($buf, sha1_hex($str), 'read expected body');
+       is($err, '', 'no errors from curl');
+       is($out, sha1_hex($str), 'read expected body');
 }
 
 {
index b3cf8693118b66cf67f350d7366fd4c3566333de..0a93f204618de2cc59bdcea2fc446fcd08229a45 100644 (file)
@@ -14,7 +14,6 @@ foreach my $mod (qw(Plack::Util Plack::Builder Danga::Socket
 use File::Temp qw/tempdir/;
 use IO::Socket::UNIX;
 use Cwd qw/getcwd/;
-use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD :seek);
 my $tmpdir = tempdir('httpd-unix-XXXXXX', TMPDIR => 1, CLEANUP => 1);
 my $unix = "$tmpdir/unix.sock";
 my $httpd = 'blib/script/public-inbox-httpd';
index f33c0969b5f4bf2174af6fd1ffc6ae14c157969c..44df1642cc51699d407b5285e386b4c0ffe506e7 100644 (file)
--- a/t/httpd.t
+++ b/t/httpd.t
@@ -12,8 +12,8 @@ foreach my $mod (qw(Plack::Util Plack::Builder Danga::Socket
 use File::Temp qw/tempdir/;
 use Cwd qw/getcwd/;
 use IO::Socket;
-use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
+require './t/common.perl';
 
 # FIXME: too much setup
 my $tmpdir = tempdir('pi-httpd-XXXXXX', TMPDIR => 1, CLEANUP => 1);
@@ -64,25 +64,8 @@ EOF
                $im->done($mime);
        }
        ok($sock, 'sock created');
-       $! = 0;
-       my $fl = fcntl($sock, F_GETFD, 0);
-       ok(! $!, 'no error from fcntl(F_GETFD)');
-       is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
-       $pid = fork;
-       if ($pid == 0) {
-               use POSIX qw(dup2);
-               # pretend to be systemd
-               fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
-               dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
-               $ENV{LISTEN_PID} = $$;
-               $ENV{LISTEN_FDS} = 1;
-               exec $httpd, "--stdout=$out", "--stderr=$err";
-               die "FAIL: $!\n";
-       }
-       ok(defined $pid, 'forked httpd process successfully');
-       $! = 0;
-       fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
-       ok(! $!, 'no error from fcntl(F_SETFD)');
+       my $cmd = [ $httpd, "--stdout=$out", "--stderr=$err" ];
+       $pid = spawn_listener(undef, $cmd, [$sock]);
        my $host = $sock->sockhost;
        my $port = $sock->sockport;
        my $conn = IO::Socket::INET->new(PeerAddr => $host,
index ffed437631cde2926e0d7e7efa9ebe6aa1c9cc20..d227b74d1835407507f1f83ba776d5dae796a804 100644 (file)
--- a/t/nntpd.t
+++ b/t/nntpd.t
@@ -12,11 +12,11 @@ require PublicInbox::Msgmap;
 use Cwd;
 use Email::Simple;
 use IO::Socket;
-use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
 use File::Temp qw/tempdir/;
 use Net::NNTP;
 use Sys::Hostname;
+require './t/common.perl';
 
 my $tmpdir = tempdir('pi-nntpd-XXXXXX', TMPDIR => 1, CLEANUP => 1);
 my $home = "$tmpdir/pi-home";
@@ -101,25 +101,9 @@ EOF
        }
 
        ok($sock, 'sock created');
-       $! = 0;
-       my $fl = fcntl($sock, F_GETFD, 0);
-       ok(! $!, 'no error from fcntl(F_GETFD)');
-       is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
-       $pid = fork;
-       if ($pid == 0) {
-               use POSIX qw(dup2);
-               # pretend to be systemd
-               fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
-               dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
-               $ENV{LISTEN_PID} = $$;
-               $ENV{LISTEN_FDS} = 1;
-               exec $nntpd, "--stdout=$out", "--stderr=$err";
-               die "FAIL: $!\n";
-       }
+       my $cmd = [ $nntpd, "--stdout=$out", "--stderr=$err" ];
+       $pid = spawn_listener(undef, $cmd, [ $sock ]);
        ok(defined $pid, 'forked nntpd process successfully');
-       $! = 0;
-       fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
-       ok(! $!, 'no error from fcntl(F_SETFD)');
        my $host_port = $sock->sockhost . ':' . $sock->sockport;
        my $n = Net::NNTP->new($host_port);
        my $list = $n->list;
index f7890d1c3868d04bfee3f55224aeaa62f01d6bea..3ca1ce335768454dc4ea2f22f4d99912833474ee 100644 (file)
@@ -6,13 +6,13 @@ use Test::More;
 use Benchmark qw(:all :hireswallclock);
 use PublicInbox::Inbox;
 use File::Temp qw/tempdir/;
-use POSIX qw(dup2);
-use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
 use Net::NNTP;
 my $pi_dir = $ENV{GIANT_PI_DIR};
 plan skip_all => "GIANT_PI_DIR not defined for $0" unless $pi_dir;
 eval { require PublicInbox::Search };
 my ($host_port, $group, %opts, $s, $pid);
+require './t/common.perl';
+
 END {
        if ($s) {
                $s->print("QUIT\r\n");
@@ -53,21 +53,8 @@ if (($ENV{NNTP_TEST_URL} || '') =~ m!\Anntp://([^/]+)/([^/]+)\z!) {
        my $sock = IO::Socket::INET->new(%opts);
 
        ok($sock, 'sock created');
-       $! = 0;
-       $pid = fork;
-       if ($pid == 0) {
-               # pretend to be systemd
-               my $fl = fcntl($sock, F_GETFD, 0);
-               dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
-               dup2(1, 2) or die "dup2 failed: $!\n";
-               fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
-               $ENV{LISTEN_PID} = $$;
-               $ENV{LISTEN_FDS} = 1;
-               $ENV{PI_CONFIG} = $pi_config;
-               exec $nntpd, '-W0';
-               die "FAIL: $!\n";
-       }
-       ok(defined $pid, 'forked nntpd process successfully');
+       my $cmd = [ $nntpd, '-W0' ];
+       $pid = spawn_listener({ PI_CONFIG => $pi_config }, $cmd, [$sock]);
        $host_port = $sock->sockhost . ':' . $sock->sockport;
 }
 %opts = (
index f95ad0f54b5e527261757ebdc35b6b8129faba03..283b2b228852f132f5ab3a3bb2480d81fa3f876f 100644 (file)
@@ -3,10 +3,12 @@
 use strict;
 use warnings;
 use Test::More;
+require './t/common.perl';
 
 # Integration tests for HTTP cloning + mirroring
 foreach my $mod (qw(Plack::Util Plack::Builder Danga::Socket
-                       HTTP::Date HTTP::Status Search::Xapian DBD::SQLite)) {
+                       HTTP::Date HTTP::Status Search::Xapian DBD::SQLite
+                       IPC::Run)) {
        eval "require $mod";
        plan skip_all => "$mod missing for v2mirror.t" if $@;
 }
@@ -16,7 +18,6 @@ use POSIX qw(dup2);
 use_ok 'PublicInbox::V2Writable';
 use PublicInbox::MIME;
 use PublicInbox::Config;
-use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
 # FIXME: too much setup
 my $tmpdir = tempdir('pi-v2mirror-XXXXXX', TMPDIR => 1, CLEANUP => 1);
 my $script = 'blib/script/public-inbox';
@@ -69,18 +70,9 @@ END { kill 'TERM', $pid if defined $pid };
 $! = 0;
 $sock = IO::Socket::INET->new(%opts);
 ok($sock, 'sock created');
-my $fl = fcntl($sock, F_GETFD, 0);
-$pid = fork;
-if ($pid == 0) {
-       # pretend to be systemd
-       fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
-       dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
-       $ENV{LISTEN_PID} = $$;
-       $ENV{LISTEN_FDS} = 1;
-       exec "$script-httpd", "--stdout=$tmpdir/out", "--stderr=$tmpdir/err";
-       die "FAIL: $!\n";
-}
-ok(defined $pid, 'forked httpd process successfully');
+my $cmd = [ "$script-httpd", "--stdout=$tmpdir/out", "--stderr=$tmpdir/err" ];
+ok(defined($pid = spawn_listener(undef, $cmd, [ $sock ])),
+       'spawned httpd process successfully');
 my ($host, $port) = ($sock->sockhost, $sock->sockport);
 $sock = undef;
 
@@ -150,18 +142,9 @@ is(scalar($mset->items), 0, 'purged message gone from origin');
 
 fetch_each_epoch();
 {
-       open my $err, '+>', "$tmpdir/index-err" or die "open: $!";
-       my $ipid = fork;
-       if ($ipid == 0) {
-               dup2(fileno($err), 2) or die "dup2 failed: $!";
-               exec("$script-index", '--prune', "$tmpdir/m");
-               die "exec fail: $!";
-       }
-       ok($ipid, 'running index..');
-       is(waitpid($ipid, 0), $ipid, 'index --prune done');
-       is($?, 0, 'no error from index');
-       ok(seek($err, 0, 0), 'rewound stderr');
-       $err = eval { local $/; <$err> };
+       my $cmd = [ "$script-index", '--prune', "$tmpdir/m" ];
+       my ($in, $out, $err) = ('', '', '');
+       ok(IPC::Run::run($cmd, \$in, \$out, \$err), '-index --prune');
        like($err, qr/discontiguous range/, 'warned about discontiguous range');
        unlike($err, qr/fatal/, 'no scary fatal error shown');
 }
@@ -192,18 +175,9 @@ is($mibx->git->check($to_purge), undef, 'unindex+prune successful in mirror');
        $v2w->done;
        fetch_each_epoch();
 
-       open my $err, '+>', "$tmpdir/index-err" or die "open: $!";
-       my $ipid = fork;
-       if ($ipid == 0) {
-               dup2(fileno($err), 2) or die "dup2 failed: $!";
-               exec("$script-index", "$tmpdir/m");
-               die "exec fail: $!";
-       }
-       ok($ipid, 'running index');
-       is(waitpid($ipid, 0), $ipid, 'index done');
-       is($?, 0, 'no error from index');
-       ok(seek($err, 0, 0), 'rewound stderr');
-       $err = eval { local $/; <$err> };
+       my ($in, $out, $err) = ('', '', '');
+       my $cmd = [ "$script-index", "$tmpdir/m" ];
+       ok(IPC::Run::run($cmd, \$in, \$out, \$err), 'index ran');
        is($err, '', 'no errors reported by index');
        $mset = $mibx->search->reopen->query('m:1@example.com', {mset => 1});
        is(scalar($mset->items), 0, '1@example.com no longer visible in mirror');
index b3cdbd3e6c0ff2d336852108ed7aef874e2b0d2b..c7eeee997bb7c189c71de5902c3a6105f74c3ec5 100644 (file)
@@ -6,6 +6,7 @@ use Test::More;
 use PublicInbox::MIME;
 use PublicInbox::ContentId qw(content_digest);
 use File::Temp qw/tempdir/;
+require './t/common.perl';
 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
        eval "require $mod";
        plan skip_all => "$mod missing for nntpd.t" if $@;
@@ -129,7 +130,6 @@ if ('ensure git configs are correct') {
 }
 
 SKIP: {
-       use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
        use Net::NNTP;
        use IO::Socket;
        use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
@@ -161,27 +161,9 @@ EOF
        my $pid;
        my $len;
        END { kill 'TERM', $pid if defined $pid };
-       $! = 0;
-       my $fl = fcntl($sock, F_GETFD, 0);
-       ok(! $!, 'no error from fcntl(F_GETFD)');
-       is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
-       $pid = fork;
-       if ($pid == 0) {
-               use POSIX qw(dup2);
-               $ENV{PI_CONFIG} = $pi_config;
-               # pretend to be systemd
-               fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
-               dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
-               $ENV{LISTEN_PID} = $$;
-               $ENV{LISTEN_FDS} = 1;
-               my $nntpd = 'blib/script/public-inbox-nntpd';
-               exec $nntpd, "--stdout=$out", "--stderr=$err";
-               die "FAIL: $!\n";
-       }
-       ok(defined $pid, 'forked nntpd process successfully');
-       $! = 0;
-       fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
-       ok(! $!, 'no error from fcntl(F_SETFD)');
+       my $nntpd = 'blib/script/public-inbox-nntpd';
+       my $cmd = [ $nntpd, "--stdout=$out", "--stderr=$err" ];
+       $pid = spawn_listener({ PI_CONFIG => $pi_config }, $cmd, [ $sock ]);
        my $host_port = $sock->sockhost . ':' . $sock->sockport;
        my $n = Net::NNTP->new($host_port);
        $n->group($group);