2 # Copyright (C) 2019-2021 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
15 use IO::Handle; # ->autoflush
16 use PublicInbox::TestCommon;
17 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
20 use POSIX qw(_POSIX_PIPE_BUF WNOHANG);
24 our $log_suffix = '.log';
25 my ($shuffle, %pids, @err);
26 GetOptions('j|jobs=i' => \$jobs,
27 'repeat=i' => \$repeat,
28 'log=s' => \$log_suffix,
29 's|shuffle' => \$shuffle,
30 ) or die "Usage: $0 [-j JOBS] [--log=SUFFIX] [--repeat RUNS]";
31 if (($ENV{TEST_RUN_MODE} // 2) == 0) {
32 die "$0 is not compatible with TEST_RUN_MODE=0\n";
34 my @tests = scalar(@ARGV) ? @ARGV : glob('t/*.t');
35 open my $cwd_fh, '<', '.' or die "open .: $!";
36 open my $OLDOUT, '>&STDOUT' or die "dup STDOUT: $!";
37 open my $OLDERR, '>&STDERR' or die "dup STDERR: $!";
38 $OLDOUT->autoflush(1);
39 $OLDERR->autoflush(1);
41 key2sub($_) for @tests; # precache
45 } elsif (open(my $prove_state, '<', '.prove') && eval { require YAML::XS }) {
46 # reuse "prove --state=save" data to start slowest tests, first
47 my $state = YAML::XS::Load(do { local $/; <$prove_state> });
48 my $t = $state->{tests};
50 ($t->{$b}->{elapsed} // 0) <=> ($t->{$a}->{elapsed} // 0)
54 our $tb = Test::More->builder;
61 our ($worker, $worker_test);
64 $? = 255 if $? == 0 && !$tb->is_passing;
65 my $status = $? ? 'not ok' : 'ok';
66 chdir($cwd_fh) or DIE "fchdir: $!";
67 if ($log_suffix ne '') {
68 my $log = $worker_test;
69 $log =~ s/\.t\z/$log_suffix/;
71 if (open my $fh, '<', $log) {
72 my @not_ok = grep(!/^(?:ok |[ \t]*#)/ms, <$fh>);
73 my $last = $not_ok[-1] // '';
74 pop @not_ok if $last =~ /^[0-9]+\.\.[0-9]+$/;
76 print $OLDERR map { $pfx.$_ } @not_ok;
77 seek($fh, 0, SEEK_SET) or die "seek: $!";
79 # show unique skip texts and the number of times
80 # each text was skipped
82 my @sk = (<$fh> =~ m/^ok [0-9]+ (# skip [^\n]+)/mgs);
85 my @err = grep { !$nr{$_}++ } @sk;
86 print $OLDERR "$pfx$_ ($nr{$_})\n" for @err;
87 $skip = ' # total skipped: '.scalar(@sk);
90 print $OLDERR "could not open: $log: $!\n";
92 print $OLDOUT "$status $worker_test$skip\n";
96 # Test::Builder or Test2::Hub may call exit() from plan(skip_all => ...)
97 END { test_status() if (defined($worker_test) && $worker == $$) }
102 if ($log_suffix ne '') {
104 $log =~ s/\.[^\.]+\z/$log_suffix/ or DIE "can't log for $test";
105 open $log_fh, '>', $log or DIE "open $log: $!";
106 $log_fh->autoflush(1);
107 $tb->output($log_fh);
108 $tb->failure_output($log_fh);
109 $tb->todo_output($log_fh);
110 open STDOUT, '>&', $log_fh or DIE "1>$log: $!";
111 open STDERR, '>&', $log_fh or DIE "2>$log: $!";
113 $worker_test = $test;
116 $worker_test = undef;
117 push @err, "$test ($?)" if $?;
120 sub UINT_SIZE () { 4 }
122 # worker processes will SIGUSR1 the producer process when it
123 # sees EOF on the pipe. On FreeBSD 11.2 and Perl 5.30.0,
124 # sys/ioctl.ph gives the wrong value for FIONREAD().
126 my $eof; # we stop respawning if true
128 my $start_worker = sub {
129 my ($j, $rd, $wr, $todo) = @_;
130 my $pid = fork // DIE "fork: $!";
135 my $r = sysread($rd, my $buf, UINT_SIZE);
141 DIE "short read $r" if $r != UINT_SIZE;
142 my $t = unpack('I', $buf);
143 run_test($todo->[$t]);
146 kill 'USR1', $producer if !$eof; # sets $eof in $producer
147 DIE join('', map { "E: $_\n" } @err) if @err;
154 # negative $repeat means loop forever:
155 for (my $i = $repeat; $i != 0; $i--) {
156 my @todo = $shuffle ? List::Util::shuffle(@tests) : @tests;
158 # single-producer, multi-consumer queue relying on POSIX pipe semantics
159 # POSIX.1-2008 stipulates a regular file should work, but Linux <3.14
160 # had broken read(2) semantics according to the read(2) manpage
161 pipe(my ($rd, $wr)) or DIE "pipe: $!";
163 # fill the queue before forking so children can start earlier
164 my $n = (_POSIX_PIPE_BUF / UINT_SIZE);
166 print $wr join('', map { pack('I', $_) } (0..$#todo)) or DIE;
168 } else { # write what we can...
170 print $wr join('', map { pack('I', $_) } (0..$n)) or DIE;
171 $n += 1; # and send more ($n..$#todo), later
174 local $SIG{USR1} = sub { $eof = 1 };
177 my $flags = $sig ? WNOHANG : 0;
179 my $pid = waitpid(-1, $flags) or return;
181 my $j = delete $pids{$pid};
183 push @err, "reaped unknown $pid ($?)";
186 push @err, "job[$j] ($?)" if $?;
187 # skip_all can exit(0), respawn if needed:
189 print $OLDERR "# respawning job[$j]\n";
190 $start_worker->($j, $rd, $wr, \@todo);
195 # start the workers to consume the queue
196 for (my $j = 0; $j < $jobs; $j++) {
197 $start_worker->($j, $rd, $wr, \@todo);
200 local $SIG{CHLD} = $sigchld;
201 # too many tests to fit in the pipe before starting workers,
202 # send the rest now the workers are running
203 print $wr join('', map { pack('I', $_) } ($n..$#todo)) or DIE;
207 $sigchld->(0) while scalar(keys(%pids));
208 DIE join('', map { "E: $_\n" } @err) if @err;
211 print $OLDOUT "1..".($repeat * scalar(@tests))."\n" if $repeat >= 0;