]> git.sur5r.net Git - i3/i3/blob - testcases/complete-run.pl
020e2f90b737ab3499be140f58af4c6fb6c95c44
[i3/i3] / testcases / complete-run.pl
1 #!/usr/bin/env perl
2 # vim:ts=4:sw=4:expandtab
3 # © 2010-2011 Michael Stapelberg and contributors
4 package complete_run;
5 use strict;
6 use warnings;
7 use v5.10;
8 # the following are modules which ship with Perl (>= 5.10):
9 use Pod::Usage;
10 use Cwd qw(abs_path);
11 use File::Temp qw(tempfile tempdir);
12 use Getopt::Long;
13 use POSIX ();
14 use TAP::Harness;
15 use TAP::Parser;
16 use TAP::Parser::Aggregator;
17 use Time::HiRes qw(time);
18 # these are shipped with the testsuite
19 use lib qw(lib);
20 use StartXDummy;
21 use StatusLine;
22 use TestWorker;
23 # the following modules are not shipped with Perl
24 use AnyEvent;
25 use AnyEvent::Util;
26 use AnyEvent::Handle;
27 use AnyEvent::I3 qw(:all);
28 use X11::XCB::Connection;
29 use JSON::XS; # AnyEvent::I3 depends on it, too.
30
31 # Close superfluous file descriptors which were passed by running in a VIM
32 # subshell or situations like that.
33 AnyEvent::Util::close_all_fds_except(0, 1, 2);
34
35 # convinience wrapper to write to the log file
36 my $log;
37 sub Log { say $log "@_" }
38
39 my %timings;
40 my $help = 0;
41 # Number of tests to run in parallel. Important to know how many Xdummy
42 # instances we need to start (unless @displays are given). Defaults to
43 # num_cores * 2.
44 my $parallel = undef;
45 my @displays = ();
46 my %options = (
47     valgrind => 0,
48     strace => 0,
49     coverage => 0,
50     restart => 0,
51 );
52
53 my $result = GetOptions(
54     "coverage-testing" => \$options{coverage},
55     "valgrind" => \$options{valgrind},
56     "strace" => \$options{strace},
57     "display=s" => \@displays,
58     "parallel=i" => \$parallel,
59     "help|?" => \$help,
60 );
61
62 pod2usage(-verbose => 2, -exitcode => 0) if $help;
63
64 @displays = split(/,/, join(',', @displays));
65 @displays = map { s/ //g; $_ } @displays;
66
67 # 2: get a list of all testcases
68 my @testfiles = @ARGV;
69
70 # if no files were passed on command line, run all tests from t/
71 @testfiles = <t/*.t> if @testfiles == 0;
72
73 my $numtests = scalar @testfiles;
74
75 # No displays specified, let’s start some Xdummy instances.
76 if (@displays == 0) {
77     @displays = start_xdummy($parallel, $numtests);
78 }
79
80 # 1: create an output directory for this test-run
81 my $outdir = "testsuite-";
82 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
83 $outdir .= `git describe --tags`;
84 chomp($outdir);
85 mkdir($outdir) or die "Could not create $outdir";
86 unlink("latest") if -e "latest";
87 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
88
89
90 # connect to all displays for two reasons:
91 # 1: check if the display actually works
92 # 2: keep the connection open so that i3 is not the only client. this prevents
93 #    the X server from exiting (Xdummy will restart it, but not quick enough
94 #    sometimes)
95 my @single_worker;
96 for my $display (@displays) {
97     my $screen;
98     my $x = X11::XCB::Connection->new(display => $display);
99     if ($x->has_error) {
100         die "Could not connect to display $display\n";
101     } else {
102         # start a TestWorker for each display
103         push @single_worker, worker($display, $x, $outdir, \%options);
104     }
105 }
106
107 # Read previous timing information, if available. We will be able to roughly
108 # predict the test duration and schedule a good order for the tests.
109 my $timingsjson = StartXDummy::slurp('.last_run_timings.json');
110 %timings = %{decode_json($timingsjson)} if length($timingsjson) > 0;
111
112 # Re-order the files so that those which took the longest time in the previous
113 # run will be started at the beginning to not delay the whole run longer than
114 # necessary.
115 @testfiles = map  { $_->[0] }
116              sort { $b->[1] <=> $a->[1] }
117              map  { [$_, $timings{$_} // 999] } @testfiles;
118
119 printf("\nRough time estimate for this run: %.2f seconds\n\n", $timings{GLOBAL})
120     if exists($timings{GLOBAL});
121
122 # Forget the old timings, we don’t necessarily run the same set of tests as
123 # before. Otherwise we would end up with left-overs.
124 %timings = (GLOBAL => time());
125
126 my $logfile = "$outdir/complete-run.log";
127 open $log, '>', $logfile or die "Could not create '$logfile': $!";
128 say "Writing logfile to '$logfile'...";
129
130 # 3: run all tests
131 my @done;
132 my $num = @testfiles;
133 my $harness = TAP::Harness->new({ });
134
135 my $aggregator = TAP::Parser::Aggregator->new();
136 $aggregator->start();
137
138 status_init(displays => \@displays, tests => $num);
139
140 my $single_cv = AE::cv;
141
142 # We start tests concurrently: For each display, one test gets started. Every
143 # test starts another test after completing.
144 for (@single_worker) {
145     $single_cv->begin;
146     take_job($_, $single_cv, \@testfiles);
147 }
148
149 $single_cv->recv;
150
151 $aggregator->stop();
152
153 # print empty lines to seperate failed tests from statuslines
154 print "\n\n";
155
156 for (@done) {
157     my ($test, $output) = @$_;
158     say "no output for $test" unless $output;
159     Log "output for $test:";
160     Log $output;
161     # print error messages of failed tests
162     say for $output =~ /^not ok.+\n+((?:^#.+\n)+)/mg
163 }
164
165 # 4: print summary
166 $harness->summary($aggregator);
167
168 close $log;
169
170 # 5: Save the timings for better scheduling/prediction next run.
171 $timings{GLOBAL} = time() - $timings{GLOBAL};
172 open(my $fh, '>', '.last_run_timings.json');
173 print $fh encode_json(\%timings);
174 close($fh);
175
176 # 6: Print the slowest test files.
177 my @slowest = map  { $_->[0] }
178               sort { $b->[1] <=> $a->[1] }
179               map  { [$_, $timings{$_}] }
180               grep { !/^GLOBAL$/ } keys %timings;
181 say '';
182 say 'The slowest tests are:';
183 printf("\t%s with %.2f seconds\n", $_, $timings{$_})
184     for @slowest[0..($#slowest > 4 ? 4 : $#slowest)];
185
186 # When we are running precisely one test, print the output. Makes developing
187 # with a single testcase easier.
188 if ($numtests == 1) {
189     say '';
190     say 'Test output:';
191     say StartXDummy::slurp($logfile);
192 }
193
194 END { cleanup() }
195
196 exit 0;
197
198 #
199 # Takes a test from the beginning of @testfiles and runs it.
200 #
201 # The TAP::Parser (which reads the test output) will get called as soon as
202 # there is some activity on the stdout file descriptor of the test process
203 # (using an AnyEvent->io watcher).
204 #
205 # When a test completes and @done contains $num entries, the $cv condvar gets
206 # triggered to finish testing.
207 #
208 sub take_job {
209     my ($worker, $cv, $tests) = @_;
210
211     my $test = shift @$tests
212         or return $cv->end;
213
214     my $display = $worker->{display};
215
216     Log status($display, "$test: starting");
217     $timings{$test} = time();
218     worker_next($worker, $test);
219
220     # create a TAP::Parser with an in-memory fh
221     my $output;
222     my $parser = TAP::Parser->new({
223         source => do { open(my $fh, '<', \$output); $fh },
224     });
225
226     my $ipc = $worker->{ipc};
227
228     my $w;
229     $w = AnyEvent->io(
230         fh => $ipc,
231         poll => 'r',
232         cb => sub {
233             state $tests_completed = 0;
234             state $partial = '';
235
236             sysread($ipc, my $buf, 4096) or die "sysread: $!";
237
238             if ($partial) {
239                 $buf = $partial . $buf;
240                 $partial = '';
241             }
242
243             # make sure we feed TAP::Parser complete lines so it doesn't blow up
244             if (substr($buf, -1, 1) ne "\n") {
245                 my $nl = rindex($buf, "\n");
246                 if ($nl == -1) {
247                     $partial = $buf;
248                     return;
249                 }
250
251                 # strip partial from buffer
252                 $partial = substr($buf, $nl + 1, '');
253             }
254
255             # count lines before stripping eof-marker otherwise we might
256             # end up with for (1 .. 0) { } which would effectivly skip the loop
257             my $lines = $buf =~ tr/\n//;
258             my $t_eof = $buf =~ s/^$TestWorker::EOF$//m;
259
260             $output .= $buf;
261
262             for (1 .. $lines) {
263                 my $result = $parser->next;
264                 if (defined($result) and $result->is_test) {
265                     $tests_completed++;
266                     status($display, "$test: [$tests_completed/??] ");
267                 }
268             }
269
270             return unless $t_eof;
271
272             Log status($display, "$test: finished");
273             $timings{$test} = time() - $timings{$test};
274             status_completed(scalar @done);
275
276             $aggregator->add($test, $parser);
277             push @done, [ $test, $output ];
278
279             undef $w;
280             take_job($worker, $cv, $tests);
281         }
282     );
283 }
284
285 sub cleanup {
286     $_->() for our @CLEANUP;
287     exit;
288 }
289
290 # must be in a begin block because we C<exit 0> above
291 BEGIN {
292     $SIG{$_} = sub {
293         require Carp; Carp::cluck("Caught SIG$_[0]\n");
294         cleanup();
295     } for qw(INT TERM QUIT KILL PIPE)
296 }
297
298 __END__
299
300 =head1 NAME
301
302 complete-run.pl - Run the i3 testsuite
303
304 =head1 SYNOPSIS
305
306 complete-run.pl [files...]
307
308 =head1 EXAMPLE
309
310 To run the whole testsuite on a reasonable number of Xdummy instances (your
311 running X11 will not be touched), run:
312   ./complete-run.pl
313
314 To run only a specific test (useful when developing a new feature), run:
315   ./complete-run t/100-fullscreen.t
316
317 =head1 OPTIONS
318
319 =over 8
320
321 =item B<--display>
322
323 Specifies which X11 display should be used. Can be specified multiple times and
324 will parallelize the tests:
325
326   # Run tests on the second X server
327   ./complete-run.pl -d :1
328
329   # Run four tests in parallel on some Xdummy servers
330   ./complete-run.pl -d :1,:2,:3,:4
331
332 Note that it is not necessary to specify this anymore. If omitted,
333 complete-run.pl will start (num_cores * 2) Xdummy instances.
334
335 =item B<--valgrind>
336
337 Runs i3 under valgrind to find memory problems. The output will be available in
338 C<latest/valgrind-for-$test.log>.
339
340 =item B<--strace>
341
342 Runs i3 under strace to trace system calls. The output will be available in
343 C<latest/strace-for-$test.log>.
344
345 =item B<--coverage-testing>
346
347 Exits i3 cleanly (instead of kill -9) to make coverage testing work properly.
348
349 =item B<--parallel>
350
351 Number of Xdummy instances to start (if you don’t want to start num_cores * 2
352 instances for some reason).
353
354   # Run all tests on a single Xdummy instance
355   ./complete-run.pl -p 1