#!/usr/bin/perl -w
-# Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
+# Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
#
# Parallel test runner which preloads code and reuses worker processes
use v5.10.1;
use IO::Handle; # ->autoflush
use PublicInbox::TestCommon;
-use Cwd qw(getcwd);
use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
use Errno qw(EINTR);
use Fcntl qw(:seek);
die "$0 is not compatible with TEST_RUN_MODE=0\n";
}
my @tests = scalar(@ARGV) ? @ARGV : glob('t/*.t');
-my $cwd = getcwd();
+open my $cwd_fh, '<', '.' or die "open .: $!";
open my $OLDOUT, '>&STDOUT' or die "dup STDOUT: $!";
open my $OLDERR, '>&STDERR' or die "dup STDERR: $!";
$OLDOUT->autoflush(1);
sub test_status () {
$? = 255 if $? == 0 && !$tb->is_passing;
my $status = $? ? 'not ok' : 'ok';
- chdir($cwd) or DIE "chdir($cwd): $!";
+ chdir($cwd_fh) or DIE "fchdir: $!";
if ($log_suffix ne '') {
my $log = $worker_test;
$log =~ s/\.t\z/$log_suffix/;
my $eof; # we stop respawning if true
my $start_worker = sub {
- my ($i, $j, $rd, $todo) = @_;
- defined(my $pid = fork) or DIE "fork: $!";
+ my ($j, $rd, $wr, $todo) = @_;
+ my $pid = fork // DIE "fork: $!";
if ($pid == 0) {
+ close $wr if $wr;
$worker = $$;
while (1) {
my $r = sysread($rd, my $buf, UINT_SIZE);
for (my $i = $repeat; $i != 0; $i--) {
my @todo = $shuffle ? List::Util::shuffle(@tests) : @tests;
- # single-producer, multi-consumer queue relying on POSIX semantics
+ # single-producer, multi-consumer queue relying on POSIX pipe semantics
+ # POSIX.1-2008 stipulates a regular file should work, but Linux <3.14
+ # had broken read(2) semantics according to the read(2) manpage
pipe(my ($rd, $wr)) or DIE "pipe: $!";
# fill the queue before forking so children can start earlier
my $n = (_POSIX_PIPE_BUF / UINT_SIZE);
if ($n >= $#todo) {
print $wr join('', map { pack('I', $_) } (0..$#todo)) or DIE;
- close $wr or die;
- $wr = undef;
+ undef $wr;
} else { # write what we can...
$wr->autoflush(1);
print $wr join('', map { pack('I', $_) } (0..$n)) or DIE;
# skip_all can exit(0), respawn if needed:
if (!$eof) {
print $OLDERR "# respawning job[$j]\n";
- $start_worker->($i, $j, $rd, \@todo);
+ $start_worker->($j, $rd, $wr, \@todo);
}
}
};
# start the workers to consume the queue
for (my $j = 0; $j < $jobs; $j++) {
- $start_worker->($i, $j, $rd, \@todo);
+ $start_worker->($j, $rd, $wr, \@todo);
}
-
if ($wr) {
local $SIG{CHLD} = $sigchld;
# too many tests to fit in the pipe before starting workers,
# send the rest now the workers are running
print $wr join('', map { pack('I', $_) } ($n..$#todo)) or DIE;
- close $wr or die;
+ undef $wr;
}
$sigchld->(0) while scalar(keys(%pids));