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