2 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 # Parallel test runner which preloads code and reuses worker processes
6 # to give a nice speedup over prove(1). It also generates per-test
7 # .log files (similar to automake tests).
9 # *.t files run by this should not rely on global state.
11 # Usage: $PERL -I lib -w t/run.perl -j4
12 # Or via prove(1): prove -lvw t/run.perl :: -j4
14 use PublicInbox::TestCommon;
16 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
18 use POSIX qw(_POSIX_PIPE_BUF WNOHANG);
22 our $log_suffix = '.log';
23 my ($shuffle, %pids, @err);
24 GetOptions('j|jobs=i' => \$jobs,
25 'repeat=i' => \$repeat,
26 'log=s' => \$log_suffix,
27 's|shuffle' => \$shuffle,
28 ) or die "Usage: $0 [-j JOBS] [--log=SUFFIX] [--repeat RUNS]";
29 if (($ENV{TEST_RUN_MODE} // 1) == 0) {
30 die "$0 is not compatible with TEST_RUN_MODE=0\n";
32 my @tests = scalar(@ARGV) ? @ARGV : glob('t/*.t');
34 open OLDOUT, '>&STDOUT' or die "dup STDOUT: $!";
35 open OLDERR, '>&STDERR' or die "dup STDERR: $!";
39 key2sub($_) for @tests; # precache
43 } elsif (open(my $prove_state, '<', '.prove') && eval { require YAML::XS }) {
44 # reuse "prove --state=save" data to start slowest tests, first
45 my $state = YAML::XS::Load(do { local $/; <$prove_state> });
46 my $t = $state->{tests};
48 ($t->{$b}->{elapsed} // 0) <=> ($t->{$a}->{elapsed} // 0)
52 our $tb = Test::More->builder;
59 our ($worker, $worker_test);
62 $? = 255 if $? == 0 && !$tb->is_passing;
63 my $status = $? ? 'not ok' : 'ok';
64 print OLDOUT "$status $worker_test\n" if $log_suffix ne '';
67 # Test::Builder or Test2::Hub may call exit() from plan(skip_all => ...)
68 END { test_status() if (defined($worker_test) && $worker == $$) }
73 if ($log_suffix ne '') {
75 $log =~ s/\.[^\.]+\z/$log_suffix/ or DIE "can't log for $test";
76 open $log_fh, '>', $log or DIE "open $log: $!";
77 $log_fh->autoflush(1);
79 $tb->failure_output($log_fh);
80 $tb->todo_output($log_fh);
81 open STDOUT, '>&', $log_fh or DIE "1>$log: $!";
82 open STDERR, '>&', $log_fh or DIE "2>$log: $!";
88 push @err, "$test ($?)" if $?;
91 sub UINT_SIZE () { 4 }
93 # worker processes will SIGUSR1 the producer process when it
94 # sees EOF on the pipe. On FreeBSD 11.2 and Perl 5.30.0,
95 # sys/ioctl.ph gives the wrong value for FIONREAD().
97 my $eof; # we stop respawning if true
99 my $start_worker = sub {
100 my ($i, $j, $rd, $todo) = @_;
101 defined(my $pid = fork) or DIE "fork: $!";
105 my $r = sysread($rd, my $buf, UINT_SIZE);
111 DIE "short read $r" if $r != UINT_SIZE;
112 my $t = unpack('I', $buf);
113 run_test($todo->[$t]);
115 chdir($cwd) or DIE "chdir: $!";
117 kill 'USR1', $producer if !$eof; # sets $eof in $producer
118 DIE join('', map { "E: $_\n" } @err) if @err;
125 # negative $repeat means loop forever:
126 for (my $i = $repeat; $i != 0; $i--) {
127 my @todo = $shuffle ? List::Util::shuffle(@tests) : @tests;
129 # single-producer, multi-consumer queue relying on POSIX semantics
130 pipe(my ($rd, $wr)) or DIE "pipe: $!";
132 # fill the queue before forking so children can start earlier
133 my $n = (_POSIX_PIPE_BUF / UINT_SIZE);
135 print $wr join('', map { pack('I', $_) } (0..$#todo)) or DIE;
138 } else { # write what we can...
140 print $wr join('', map { pack('I', $_) } (0..$n)) or DIE;
141 $n += 1; # and send more ($n..$#todo), later
144 local $SIG{USR1} = sub { $eof = 1 };
147 my $flags = $sig ? WNOHANG : 0;
149 my $pid = waitpid(-1, $flags) or return;
151 my $j = delete $pids{$pid};
153 push @err, "reaped unknown $pid ($?)";
156 push @err, "job[$j] ($?)" if $?;
157 # skip_all can exit(0), respawn if needed:
159 print OLDERR "# respawning job[$j]\n";
160 $start_worker->($i, $j, $rd, \@todo);
165 # start the workers to consume the queue
166 for (my $j = 0; $j < $jobs; $j++) {
167 $start_worker->($i, $j, $rd, \@todo);
171 local $SIG{CHLD} = $sigchld;
172 # too many tests to fit in the pipe before starting workers,
173 # send the rest now the workers are running
174 print $wr join('', map { pack('I', $_) } ($n..$#todo)) or DIE;
178 $sigchld->(0) while scalar(keys(%pids));
179 DIE join('', map { "E: $_\n" } @err) if @err;
182 print OLDOUT "1..".($repeat * scalar(@tests))."\n" if $repeat >= 0;