2 # vim:ts=4:sw=4:expandtab
3 # © 2010-2011 Michael Stapelberg and contributors
8 # the following are modules which ship with Perl (>= 5.10):
11 use File::Temp qw(tempfile tempdir);
16 use TAP::Parser::Aggregator;
17 use Time::HiRes qw(time);
19 # these are shipped with the testsuite
24 # the following modules are not shipped with Perl
28 use AnyEvent::I3 qw(:all);
29 use X11::XCB::Connection;
30 use JSON::XS; # AnyEvent::I3 depends on it, too.
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);
36 # convinience wrapper to write to the log file
38 sub Log { say $log "@_" }
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
54 my $keep_xdummy_output = 0;
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,
67 pod2usage(-verbose => 2, -exitcode => 0) if $help;
69 @displays = split(/,/, join(',', @displays));
70 @displays = map { s/ //g; $_ } @displays;
72 # 2: get a list of all testcases
73 my @testfiles = @ARGV;
75 # if no files were passed on command line, run all tests from t/
76 @testfiles = <t/*.t> if @testfiles == 0;
78 my $numtests = scalar @testfiles;
80 # No displays specified, let’s start some Xdummy instances.
82 @displays = start_xdummy($parallel, $numtests, $keep_xdummy_output);
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`;
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";
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
101 for my $display (@displays) {
103 my $x = X11::XCB::Connection->new(display => $display);
105 die "Could not connect to display $display\n";
107 # start a TestWorker for each display
108 push @single_worker, worker($display, $x, $outdir, \%options);
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;
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
120 @testfiles = map { $_->[0] }
121 sort { $b->[1] <=> $a->[1] }
122 map { [$_, $timings{$_} // 999] } @testfiles;
124 printf("\nRough time estimate for this run: %.2f seconds\n\n", $timings{GLOBAL})
125 if exists($timings{GLOBAL});
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());
131 my $logfile = "$outdir/complete-run.log";
132 open $log, '>', $logfile or die "Could not create '$logfile': $!";
134 say "Writing logfile to '$logfile'...";
138 my $num = @testfiles;
139 my $harness = TAP::Harness->new({ });
141 my $aggregator = TAP::Parser::Aggregator->new();
142 $aggregator->start();
144 status_init(displays => \@displays, tests => $num);
146 my $single_cv = AE::cv;
148 # We start tests concurrently: For each display, one test gets started. Every
149 # test starts another test after completing.
150 for (@single_worker) {
152 take_job($_, $single_cv, \@testfiles);
159 # print empty lines to seperate failed tests from statuslines
163 my ($test, $output) = @$_;
164 say "no output for $test" unless $output;
165 Log "output for $test:";
167 # print error messages of failed tests
168 say for $output =~ /^not ok.+\n+((?:^#.+\n)+)/mg
172 $harness->summary($aggregator);
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);
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;
188 say 'The slowest tests are:';
189 printf("\t%s with %.2f seconds\n", $_, $timings{$_})
190 for @slowest[0..($#slowest > 4 ? 4 : $#slowest)];
192 # When we are running precisely one test, print the output. Makes developing
193 # with a single testcase easier.
194 if ($numtests == 1) {
197 say StartXDummy::slurp($logfile);
205 # Takes a test from the beginning of @testfiles and runs it.
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).
211 # When a test completes and @done contains $num entries, the $cv condvar gets
212 # triggered to finish testing.
215 my ($worker, $cv, $tests) = @_;
217 my $test = shift @$tests
220 my $display = $worker->{display};
222 Log status($display, "$test: starting");
223 $timings{$test} = time();
224 worker_next($worker, $test);
226 # create a TAP::Parser with an in-memory fh
228 my $parser = TAP::Parser->new({
229 source => do { open(my $fh, '<', \$output); $fh },
232 my $ipc = $worker->{ipc};
239 state $tests_completed = 0;
242 sysread($ipc, my $buf, 4096) or die "sysread: $!";
245 $buf = $partial . $buf;
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");
257 # strip partial from buffer
258 $partial = substr($buf, $nl + 1, '');
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;
269 my $result = $parser->next;
270 next unless defined($result);
271 if ($result->is_test) {
273 status($display, "$test: [$tests_completed/??] ");
274 } elsif ($result->is_bailout) {
275 Log status($display, "$test: BAILOUT");
276 status_completed(scalar @done);
278 say "test $test bailed out: " . $result->explanation;
283 return unless $t_eof;
285 Log status($display, "$test: finished");
286 $timings{$test} = time() - $timings{$test};
287 status_completed(scalar @done);
289 $aggregator->add($test, $parser);
290 push @done, [ $test, $output ];
293 take_job($worker, $cv, $tests);
299 $_->() for our @CLEANUP;
303 # must be in a begin block because we C<exit 0> above
306 require Carp; Carp::cluck("Caught SIG$_[0]\n");
308 } for qw(INT TERM QUIT KILL PIPE)
315 complete-run.pl - Run the i3 testsuite
319 complete-run.pl [files...]
323 To run the whole testsuite on a reasonable number of Xdummy instances (your
324 running X11 will not be touched), run:
327 To run only a specific test (useful when developing a new feature), run:
328 ./complete-run t/100-fullscreen.t
336 Specifies which X11 display should be used. Can be specified multiple times and
337 will parallelize the tests:
339 # Run tests on the second X server
340 ./complete-run.pl -d :1
342 # Run four tests in parallel on some Xdummy servers
343 ./complete-run.pl -d :1,:2,:3,:4
345 Note that it is not necessary to specify this anymore. If omitted,
346 complete-run.pl will start (num_cores * 2) Xdummy instances.
350 Runs i3 under valgrind to find memory problems. The output will be available in
351 C<latest/valgrind-for-$test.log>.
355 Runs i3 under strace to trace system calls. The output will be available in
356 C<latest/strace-for-$test.log>.
360 Runs i3 under xtrace to trace X11 requests/replies. The output will be
361 available in C<latest/xtrace-for-$test.log>.
363 =item B<--coverage-testing>
365 Exits i3 cleanly (instead of kill -9) to make coverage testing work properly.
369 Number of Xdummy instances to start (if you don’t want to start num_cores * 2
370 instances for some reason).
372 # Run all tests on a single Xdummy instance
373 ./complete-run.pl -p 1