]> git.sur5r.net Git - i3/i3/blob - testcases/complete-run.pl
Merge branch 'master' into next
[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 # When the user specifies displays, we don’t run multi-monitor tests at all
76 # (because we don’t know which displaynumber is the X-Server with multiple
77 # monitors).
78 my $multidpy = undef;
79
80 # No displays specified, let’s start some Xdummy instances.
81 if (@displays == 0) {
82     my $dpyref;
83     ($dpyref, $multidpy) = start_xdummy($parallel, $numtests);
84     @displays = @$dpyref;
85 }
86
87 # 1: create an output directory for this test-run
88 my $outdir = "testsuite-";
89 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
90 $outdir .= `git describe --tags`;
91 chomp($outdir);
92 mkdir($outdir) or die "Could not create $outdir";
93 unlink("latest") if -e "latest";
94 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
95
96
97 # connect to all displays for two reasons:
98 # 1: check if the display actually works
99 # 2: keep the connection open so that i3 is not the only client. this prevents
100 #    the X server from exiting (Xdummy will restart it, but not quick enough
101 #    sometimes)
102 my @single_worker;
103 for my $display (@displays) {
104     my $screen;
105     my $x = X11::XCB::Connection->new(display => $display);
106     if ($x->has_error) {
107         die "Could not connect to display $display\n";
108     } else {
109         # start a TestWorker for each display
110         push @single_worker, worker($display, $x, $outdir, \%options);
111     }
112 }
113
114 my @multi_worker;
115 if (defined($multidpy)) {
116     my $x = X11::XCB::Connection->new(display => $multidpy);
117     if ($x->has_error) {
118         die "Could not connect to multi-monitor display $multidpy\n";
119     } else {
120         push @multi_worker, worker($multidpy, $x, $outdir, \%options);
121     }
122 }
123
124 # Read previous timing information, if available. We will be able to roughly
125 # predict the test duration and schedule a good order for the tests.
126 my $timingsjson = StartXDummy::slurp('.last_run_timings.json');
127 %timings = %{decode_json($timingsjson)} if length($timingsjson) > 0;
128
129 # Re-order the files so that those which took the longest time in the previous
130 # run will be started at the beginning to not delay the whole run longer than
131 # necessary.
132 @testfiles = map  { $_->[0] }
133              sort { $b->[1] <=> $a->[1] }
134              map  { [$_, $timings{$_} // 999] } @testfiles;
135
136 printf("\nRough time estimate for this run: %.2f seconds\n\n", $timings{GLOBAL})
137     if exists($timings{GLOBAL});
138
139 # Forget the old timings, we don’t necessarily run the same set of tests as
140 # before. Otherwise we would end up with left-overs.
141 %timings = (GLOBAL => time());
142
143 my $logfile = "$outdir/complete-run.log";
144 open $log, '>', $logfile or die "Could not create '$logfile': $!";
145 say "Writing logfile to '$logfile'...";
146
147 # 3: run all tests
148 my @done;
149 my $num = @testfiles;
150 my $harness = TAP::Harness->new({ });
151
152 my @single_monitor_tests = grep { m,^t/([0-9]+)-, && $1 < 500 } @testfiles;
153 my @multi_monitor_tests = grep { m,^t/([0-9]+)-, && $1 >= 500 } @testfiles;
154
155 my $aggregator = TAP::Parser::Aggregator->new();
156 $aggregator->start();
157
158 status_init(displays => [ @displays, $multidpy ], tests => $num);
159
160 my $single_cv = AE::cv;
161 my $multi_cv = AE::cv;
162
163 # We start tests concurrently: For each display, one test gets started. Every
164 # test starts another test after completing.
165 for (@single_worker) {
166     $single_cv->begin;
167     take_job($_, $single_cv, \@single_monitor_tests);
168 }
169 for (@multi_worker) {
170     $multi_cv->begin;
171     take_job($_, $multi_cv, \@multi_monitor_tests);
172 }
173
174 $single_cv->recv;
175 $multi_cv->recv;
176
177 $aggregator->stop();
178
179 # print empty lines to seperate failed tests from statuslines
180 print "\n\n";
181
182 for (@done) {
183     my ($test, $output) = @$_;
184     say "no output for $test" unless $output;
185     Log "output for $test:";
186     Log $output;
187     # print error messages of failed tests
188     say for $output =~ /^not ok.+\n+((?:^#.+\n)+)/mg
189 }
190
191 # 4: print summary
192 $harness->summary($aggregator);
193
194 close $log;
195
196 # 5: Save the timings for better scheduling/prediction next run.
197 $timings{GLOBAL} = time() - $timings{GLOBAL};
198 open(my $fh, '>', '.last_run_timings.json');
199 print $fh encode_json(\%timings);
200 close($fh);
201
202 # 6: Print the slowest test files.
203 my @slowest = map  { $_->[0] }
204               sort { $b->[1] <=> $a->[1] }
205               map  { [$_, $timings{$_}] }
206               grep { !/^GLOBAL$/ } keys %timings;
207 say '';
208 say 'The slowest tests are:';
209 printf("\t%s with %.2f seconds\n", $_, $timings{$_})
210     for @slowest[0..($#slowest > 4 ? 4 : $#slowest)];
211
212 # When we are running precisely one test, print the output. Makes developing
213 # with a single testcase easier.
214 if ($numtests == 1) {
215     say '';
216     say 'Test output:';
217     say StartXDummy::slurp($logfile);
218 }
219
220 END { cleanup() }
221
222 exit 0;
223
224 #
225 # Takes a test from the beginning of @testfiles and runs it.
226 #
227 # The TAP::Parser (which reads the test output) will get called as soon as
228 # there is some activity on the stdout file descriptor of the test process
229 # (using an AnyEvent->io watcher).
230 #
231 # When a test completes and @done contains $num entries, the $cv condvar gets
232 # triggered to finish testing.
233 #
234 sub take_job {
235     my ($worker, $cv, $tests) = @_;
236
237     my $test = shift @$tests
238         or return $cv->end;
239
240     my $display = $worker->{display};
241
242     Log status($display, "$test: starting");
243     $timings{$test} = time();
244     worker_next($worker, $test);
245
246     # create a TAP::Parser with an in-memory fh
247     my $output;
248     my $parser = TAP::Parser->new({
249         source => do { open(my $fh, '<', \$output); $fh },
250     });
251
252     my $ipc = $worker->{ipc};
253
254     my $w;
255     $w = AnyEvent->io(
256         fh => $ipc,
257         poll => 'r',
258         cb => sub {
259             state $tests_completed = 0;
260             state $partial = '';
261
262             sysread($ipc, my $buf, 4096) or die "sysread: $!";
263
264             if ($partial) {
265                 $buf = $partial . $buf;
266                 $partial = '';
267             }
268
269             # make sure we feed TAP::Parser complete lines so it doesn't blow up
270             if (substr($buf, -1, 1) ne "\n") {
271                 my $nl = rindex($buf, "\n");
272                 if ($nl == -1) {
273                     $partial = $buf;
274                     return;
275                 }
276
277                 # strip partial from buffer
278                 $partial = substr($buf, $nl + 1, '');
279             }
280
281             # count lines before stripping eof-marker otherwise we might
282             # end up with for (1 .. 0) { } which would effectivly skip the loop
283             my $lines = $buf =~ tr/\n//;
284             my $t_eof = $buf =~ s/^$TestWorker::EOF$//m;
285
286             $output .= $buf;
287
288             for (1 .. $lines) {
289                 my $result = $parser->next;
290                 if (defined($result) and $result->is_test) {
291                     $tests_completed++;
292                     status($display, "$test: [$tests_completed/??] ");
293                 }
294             }
295
296             return unless $t_eof;
297
298             Log status($display, "$test: finished");
299             $timings{$test} = time() - $timings{$test};
300             status_completed(scalar @done);
301
302             $aggregator->add($test, $parser);
303             push @done, [ $test, $output ];
304
305             undef $w;
306             take_job($worker, $cv, $tests);
307         }
308     );
309 }
310
311 sub cleanup {
312     $_->() for our @CLEANUP;
313     exit;
314 }
315
316 # must be in a begin block because we C<exit 0> above
317 BEGIN {
318     $SIG{$_} = sub {
319         require Carp; Carp::cluck("Caught SIG$_[0]\n");
320         cleanup();
321     } for qw(INT TERM QUIT KILL PIPE)
322 }
323
324 __END__
325
326 =head1 NAME
327
328 complete-run.pl - Run the i3 testsuite
329
330 =head1 SYNOPSIS
331
332 complete-run.pl [files...]
333
334 =head1 EXAMPLE
335
336 To run the whole testsuite on a reasonable number of Xdummy instances (your
337 running X11 will not be touched), run:
338   ./complete-run.pl
339
340 To run only a specific test (useful when developing a new feature), run:
341   ./complete-run t/100-fullscreen.t
342
343 =head1 OPTIONS
344
345 =over 8
346
347 =item B<--display>
348
349 Specifies which X11 display should be used. Can be specified multiple times and
350 will parallelize the tests:
351
352   # Run tests on the second X server
353   ./complete-run.pl -d :1
354
355   # Run four tests in parallel on some Xdummy servers
356   ./complete-run.pl -d :1,:2,:3,:4
357
358 Note that it is not necessary to specify this anymore. If omitted,
359 complete-run.pl will start (num_cores * 2) Xdummy instances.
360
361 =item B<--valgrind>
362
363 Runs i3 under valgrind to find memory problems. The output will be available in
364 C<latest/valgrind-for-$test.log>.
365
366 =item B<--strace>
367
368 Runs i3 under strace to trace system calls. The output will be available in
369 C<latest/strace-for-$test.log>.
370
371 =item B<--coverage-testing>
372
373 Exits i3 cleanly (instead of kill -9) to make coverage testing work properly.
374
375 =item B<--parallel>
376
377 Number of Xdummy instances to start (if you don’t want to start num_cores * 2
378 instances for some reason).
379
380   # Run all tests on a single Xdummy instance
381   ./complete-run.pl -p 1