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