]> Sergey Matveev's repositories - public-inbox.git/blob - t/run.perl
tests: check-run: show skipped tests
[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 Fcntl qw(:seek);
19 use POSIX qw(_POSIX_PIPE_BUF WNOHANG);
20 my $jobs = 1;
21 my $repeat = 1;
22 $| = 1;
23 our $log_suffix = '.log';
24 my ($shuffle, %pids, @err);
25 GetOptions('j|jobs=i' => \$jobs,
26         'repeat=i' => \$repeat,
27         'log=s' => \$log_suffix,
28         's|shuffle' => \$shuffle,
29 ) or die "Usage: $0 [-j JOBS] [--log=SUFFIX] [--repeat RUNS]";
30 if (($ENV{TEST_RUN_MODE} // 2) == 0) {
31         die "$0 is not compatible with TEST_RUN_MODE=0\n";
32 }
33 my @tests = scalar(@ARGV) ? @ARGV : glob('t/*.t');
34 my $cwd = getcwd();
35 open OLDOUT, '>&STDOUT' or die "dup STDOUT: $!";
36 open OLDERR, '>&STDERR' or die "dup STDERR: $!";
37 OLDOUT->autoflush(1);
38 OLDERR->autoflush(1);
39
40 key2sub($_) for @tests; # precache
41
42 if ($shuffle) {
43         require List::Util;
44 } elsif (open(my $prove_state, '<', '.prove') && eval { require YAML::XS }) {
45         # reuse "prove --state=save" data to start slowest tests, first
46         my $state = YAML::XS::Load(do { local $/; <$prove_state> });
47         my $t = $state->{tests};
48         @tests = sort {
49                 ($t->{$b}->{elapsed} // 0) <=> ($t->{$a}->{elapsed} // 0)
50         } @tests;
51 }
52
53 our $tb = Test::More->builder;
54
55 sub DIE (;$) {
56         print OLDERR @_;
57         exit(1);
58 }
59
60 our ($worker, $worker_test);
61
62 sub test_status () {
63         $? = 255 if $? == 0 && !$tb->is_passing;
64         my $status = $? ? 'not ok' : 'ok';
65         chdir($cwd) or DIE "chdir($cwd): $!";
66         if ($log_suffix ne '') {
67                 my $log = $worker_test;
68                 $log =~ s/\.t\z/$log_suffix/;
69                 my $skip = '';
70                 if (open my $fh, '<', $log) {
71                         my @not_ok = grep(!/^(?:ok |[ \t]*#)/ms, <$fh>);
72                         pop @not_ok if $not_ok[-1] =~ /^[0-9]+\.\.[0-9]+$/;
73                         my $pfx = "# $log: ";
74                         print OLDERR map { $pfx.$_ } @not_ok;
75                         seek($fh, 0, SEEK_SET) or die "seek: $!";
76
77                         # show unique skip texts and the number of times
78                         # each text was skipped
79                         local $/;
80                         my @sk = (<$fh> =~ m/^ok [0-9]+ (# skip [^\n]+)/mgs);
81                         if (@sk) {
82                                 my %nr;
83                                 $nr{$_}++ for @sk;
84                                 for (@sk) {
85                                         my $n = delete $nr{$_} or next;
86                                         print OLDERR "$pfx$_ ($n)\n";
87                                 }
88                                 $skip = ' # total skipped: '.scalar(@sk);
89                         }
90                 } else {
91                         print OLDERR "could not open: $log: $!\n";
92                 }
93                 print OLDOUT "$status $worker_test$skip\n";
94         }
95 }
96
97 # Test::Builder or Test2::Hub may call exit() from plan(skip_all => ...)
98 END { test_status() if (defined($worker_test) && $worker == $$) }
99
100 sub run_test ($) {
101         my ($test) = @_;
102         my $log_fh;
103         if ($log_suffix ne '') {
104                 my $log = $test;
105                 $log =~ s/\.[^\.]+\z/$log_suffix/ or DIE "can't log for $test";
106                 open $log_fh, '>', $log or DIE "open $log: $!";
107                 $log_fh->autoflush(1);
108                 $tb->output($log_fh);
109                 $tb->failure_output($log_fh);
110                 $tb->todo_output($log_fh);
111                 open STDOUT, '>&', $log_fh or DIE "1>$log: $!";
112                 open STDERR, '>&', $log_fh or DIE "2>$log: $!";
113         }
114         $worker_test = $test;
115         run_script([$test]);
116         test_status();
117         $worker_test = undef;
118         push @err, "$test ($?)" if $?;
119 }
120
121 sub UINT_SIZE () { 4 }
122
123 # worker processes will SIGUSR1 the producer process when it
124 # sees EOF on the pipe.  On FreeBSD 11.2 and Perl 5.30.0,
125 # sys/ioctl.ph gives the wrong value for FIONREAD().
126 my $producer = $$;
127 my $eof; # we stop respawning if true
128
129 my $start_worker = sub {
130         my ($i, $j, $rd, $todo) = @_;
131         defined(my $pid = fork) or DIE "fork: $!";
132         if ($pid == 0) {
133                 $worker = $$;
134                 while (1) {
135                         my $r = sysread($rd, my $buf, UINT_SIZE);
136                         if (!defined($r)) {
137                                 next if $! == EINTR;
138                                 DIE "sysread: $!";
139                         }
140                         last if $r == 0;
141                         DIE "short read $r" if $r != UINT_SIZE;
142                         my $t = unpack('I', $buf);
143                         run_test($todo->[$t]);
144                         $tb->reset;
145                 }
146                 kill 'USR1', $producer if !$eof; # sets $eof in $producer
147                 DIE join('', map { "E: $_\n" } @err) if @err;
148                 exit(0);
149         } else {
150                 $pids{$pid} = $j;
151         }
152 };
153
154 # negative $repeat means loop forever:
155 for (my $i = $repeat; $i != 0; $i--) {
156         my @todo = $shuffle ? List::Util::shuffle(@tests) : @tests;
157
158         # single-producer, multi-consumer queue relying on POSIX semantics
159         pipe(my ($rd, $wr)) or DIE "pipe: $!";
160
161         # fill the queue before forking so children can start earlier
162         my $n = (_POSIX_PIPE_BUF / UINT_SIZE);
163         if ($n >= $#todo) {
164                 print $wr join('', map { pack('I', $_) } (0..$#todo)) or DIE;
165                 close $wr or die;
166                 $wr = undef;
167         } else { # write what we can...
168                 $wr->autoflush(1);
169                 print $wr join('', map { pack('I', $_) } (0..$n)) or DIE;
170                 $n += 1; # and send more ($n..$#todo), later
171         }
172         $eof = undef;
173         local $SIG{USR1} = sub { $eof = 1 };
174         my $sigchld = sub {
175                 my ($sig) = @_;
176                 my $flags = $sig ? WNOHANG : 0;
177                 while (1) {
178                         my $pid = waitpid(-1, $flags) or return;
179                         return if $pid < 0;
180                         my $j = delete $pids{$pid};
181                         if (!defined($j)) {
182                                 push @err, "reaped unknown $pid ($?)";
183                                 next;
184                         }
185                         push @err, "job[$j] ($?)" if $?;
186                         # skip_all can exit(0), respawn if needed:
187                         if (!$eof) {
188                                 print OLDERR "# respawning job[$j]\n";
189                                 $start_worker->($i, $j, $rd, \@todo);
190                         }
191                 }
192         };
193
194         # start the workers to consume the queue
195         for (my $j = 0; $j < $jobs; $j++) {
196                 $start_worker->($i, $j, $rd, \@todo);
197         }
198
199         if ($wr) {
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;
204                 close $wr or die;
205         }
206
207         $sigchld->(0) while scalar(keys(%pids));
208         DIE join('', map { "E: $_\n" } @err) if @err;
209 }
210
211 print OLDOUT "1..".($repeat * scalar(@tests))."\n" if $repeat >= 0;