]> Sergey Matveev's repositories - public-inbox.git/blob - t/run.perl
b1a0d2fe154c844b1c4e198468e5061cfd117288
[public-inbox.git] / t / run.perl
1 #!/usr/bin/perl -w
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>
4 #
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).
8 #
9 # *.t files run by this should not rely on global state.
10 #
11 # Usage: $PERL -I lib -w t/run.perl -j4
12 # Or via prove(1): prove -lvw t/run.perl :: -j4
13 use strict;
14 use PublicInbox::TestCommon;
15 use Cwd qw(getcwd);
16 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
17 use Errno qw(EINTR);
18 use POSIX qw(_POSIX_PIPE_BUF WNOHANG);
19 my $jobs = 1;
20 my $repeat = 1;
21 $| = 1;
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} // 2) == 0) {
30         die "$0 is not compatible with TEST_RUN_MODE=0\n";
31 }
32 my @tests = scalar(@ARGV) ? @ARGV : glob('t/*.t');
33 my $cwd = getcwd();
34 open OLDOUT, '>&STDOUT' or die "dup STDOUT: $!";
35 open OLDERR, '>&STDERR' or die "dup STDERR: $!";
36 OLDOUT->autoflush(1);
37 OLDERR->autoflush(1);
38
39 key2sub($_) for @tests; # precache
40
41 if ($shuffle) {
42         require List::Util;
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};
47         @tests = sort {
48                 ($t->{$b}->{elapsed} // 0) <=> ($t->{$a}->{elapsed} // 0)
49         } @tests;
50 }
51
52 our $tb = Test::More->builder;
53
54 sub DIE (;$) {
55         print OLDERR @_;
56         exit(1);
57 }
58
59 our ($worker, $worker_test);
60
61 sub test_status () {
62         $? = 255 if $? == 0 && !$tb->is_passing;
63         my $status = $? ? 'not ok' : 'ok';
64         chdir($cwd) or DIE "chdir($cwd): $!";
65         if ($log_suffix ne '') {
66                 my $log = $worker_test;
67                 $log =~ s/\.t\z/$log_suffix/;
68                 if (open my $fh, '<', $log) {
69                         my @not_ok = grep(!/^(?:ok |[ \t]*#)/ms, <$fh>);
70                         pop @not_ok if $not_ok[-1] =~ /^[0-9]+\.\.[0-9]+$/;
71                         print OLDERR map { "# $log: $_" } @not_ok;
72                 } else {
73                         print OLDERR "could not open: $log: $!\n";
74                 }
75                 print OLDOUT "$status $worker_test\n";
76         }
77 }
78
79 # Test::Builder or Test2::Hub may call exit() from plan(skip_all => ...)
80 END { test_status() if (defined($worker_test) && $worker == $$) }
81
82 sub run_test ($) {
83         my ($test) = @_;
84         my $log_fh;
85         if ($log_suffix ne '') {
86                 my $log = $test;
87                 $log =~ s/\.[^\.]+\z/$log_suffix/ or DIE "can't log for $test";
88                 open $log_fh, '>', $log or DIE "open $log: $!";
89                 $log_fh->autoflush(1);
90                 $tb->output($log_fh);
91                 $tb->failure_output($log_fh);
92                 $tb->todo_output($log_fh);
93                 open STDOUT, '>&', $log_fh or DIE "1>$log: $!";
94                 open STDERR, '>&', $log_fh or DIE "2>$log: $!";
95         }
96         $worker_test = $test;
97         run_script([$test]);
98         test_status();
99         $worker_test = undef;
100         push @err, "$test ($?)" if $?;
101 }
102
103 sub UINT_SIZE () { 4 }
104
105 # worker processes will SIGUSR1 the producer process when it
106 # sees EOF on the pipe.  On FreeBSD 11.2 and Perl 5.30.0,
107 # sys/ioctl.ph gives the wrong value for FIONREAD().
108 my $producer = $$;
109 my $eof; # we stop respawning if true
110
111 my $start_worker = sub {
112         my ($i, $j, $rd, $todo) = @_;
113         defined(my $pid = fork) or DIE "fork: $!";
114         if ($pid == 0) {
115                 $worker = $$;
116                 while (1) {
117                         my $r = sysread($rd, my $buf, UINT_SIZE);
118                         if (!defined($r)) {
119                                 next if $! == EINTR;
120                                 DIE "sysread: $!";
121                         }
122                         last if $r == 0;
123                         DIE "short read $r" if $r != UINT_SIZE;
124                         my $t = unpack('I', $buf);
125                         run_test($todo->[$t]);
126                         $tb->reset;
127                 }
128                 kill 'USR1', $producer if !$eof; # sets $eof in $producer
129                 DIE join('', map { "E: $_\n" } @err) if @err;
130                 exit(0);
131         } else {
132                 $pids{$pid} = $j;
133         }
134 };
135
136 # negative $repeat means loop forever:
137 for (my $i = $repeat; $i != 0; $i--) {
138         my @todo = $shuffle ? List::Util::shuffle(@tests) : @tests;
139
140         # single-producer, multi-consumer queue relying on POSIX semantics
141         pipe(my ($rd, $wr)) or DIE "pipe: $!";
142
143         # fill the queue before forking so children can start earlier
144         my $n = (_POSIX_PIPE_BUF / UINT_SIZE);
145         if ($n >= $#todo) {
146                 print $wr join('', map { pack('I', $_) } (0..$#todo)) or DIE;
147                 close $wr or die;
148                 $wr = undef;
149         } else { # write what we can...
150                 $wr->autoflush(1);
151                 print $wr join('', map { pack('I', $_) } (0..$n)) or DIE;
152                 $n += 1; # and send more ($n..$#todo), later
153         }
154         $eof = undef;
155         local $SIG{USR1} = sub { $eof = 1 };
156         my $sigchld = sub {
157                 my ($sig) = @_;
158                 my $flags = $sig ? WNOHANG : 0;
159                 while (1) {
160                         my $pid = waitpid(-1, $flags) or return;
161                         return if $pid < 0;
162                         my $j = delete $pids{$pid};
163                         if (!defined($j)) {
164                                 push @err, "reaped unknown $pid ($?)";
165                                 next;
166                         }
167                         push @err, "job[$j] ($?)" if $?;
168                         # skip_all can exit(0), respawn if needed:
169                         if (!$eof) {
170                                 print OLDERR "# respawning job[$j]\n";
171                                 $start_worker->($i, $j, $rd, \@todo);
172                         }
173                 }
174         };
175
176         # start the workers to consume the queue
177         for (my $j = 0; $j < $jobs; $j++) {
178                 $start_worker->($i, $j, $rd, \@todo);
179         }
180
181         if ($wr) {
182                 local $SIG{CHLD} = $sigchld;
183                 # too many tests to fit in the pipe before starting workers,
184                 # send the rest now the workers are running
185                 print $wr join('', map { pack('I', $_) } ($n..$#todo)) or DIE;
186                 close $wr or die;
187         }
188
189         $sigchld->(0) while scalar(keys(%pids));
190         DIE join('', map { "E: $_\n" } @err) if @err;
191 }
192
193 print OLDOUT "1..".($repeat * scalar(@tests))."\n" if $repeat >= 0;