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