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);
18 # these are shipped with the testsuite
23 # the following modules are not shipped with Perl
27 use AnyEvent::I3 qw(:all);
28 use X11::XCB::Connection;
29 use JSON::XS; # AnyEvent::I3 depends on it, too.
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);
35 # convinience wrapper to write to the log file
37 sub Log { say $log "@_" }
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
54 my $result = GetOptions(
55 "coverage-testing" => \$options{coverage},
56 "valgrind" => \$options{valgrind},
57 "strace" => \$options{strace},
58 "xtrace" => \$options{xtrace},
59 "display=s" => \@displays,
60 "parallel=i" => \$parallel,
64 pod2usage(-verbose => 2, -exitcode => 0) if $help;
66 @displays = split(/,/, join(',', @displays));
67 @displays = map { s/ //g; $_ } @displays;
69 # 2: get a list of all testcases
70 my @testfiles = @ARGV;
72 # if no files were passed on command line, run all tests from t/
73 @testfiles = <t/*.t> if @testfiles == 0;
75 my $numtests = scalar @testfiles;
77 # No displays specified, let’s start some Xdummy instances.
79 @displays = start_xdummy($parallel, $numtests);
82 # 1: create an output directory for this test-run
83 my $outdir = "testsuite-";
84 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
85 $outdir .= `git describe --tags`;
87 mkdir($outdir) or die "Could not create $outdir";
88 unlink("latest") if -e "latest";
89 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
92 # connect to all displays for two reasons:
93 # 1: check if the display actually works
94 # 2: keep the connection open so that i3 is not the only client. this prevents
95 # the X server from exiting (Xdummy will restart it, but not quick enough
98 for my $display (@displays) {
100 my $x = X11::XCB::Connection->new(display => $display);
102 die "Could not connect to display $display\n";
104 # start a TestWorker for each display
105 push @single_worker, worker($display, $x, $outdir, \%options);
109 # Read previous timing information, if available. We will be able to roughly
110 # predict the test duration and schedule a good order for the tests.
111 my $timingsjson = StartXDummy::slurp('.last_run_timings.json');
112 %timings = %{decode_json($timingsjson)} if length($timingsjson) > 0;
114 # Re-order the files so that those which took the longest time in the previous
115 # run will be started at the beginning to not delay the whole run longer than
117 @testfiles = map { $_->[0] }
118 sort { $b->[1] <=> $a->[1] }
119 map { [$_, $timings{$_} // 999] } @testfiles;
121 printf("\nRough time estimate for this run: %.2f seconds\n\n", $timings{GLOBAL})
122 if exists($timings{GLOBAL});
124 # Forget the old timings, we don’t necessarily run the same set of tests as
125 # before. Otherwise we would end up with left-overs.
126 %timings = (GLOBAL => time());
128 my $logfile = "$outdir/complete-run.log";
129 open $log, '>', $logfile or die "Could not create '$logfile': $!";
130 say "Writing logfile to '$logfile'...";
134 my $num = @testfiles;
135 my $harness = TAP::Harness->new({ });
137 my $aggregator = TAP::Parser::Aggregator->new();
138 $aggregator->start();
140 status_init(displays => \@displays, tests => $num);
142 my $single_cv = AE::cv;
144 # We start tests concurrently: For each display, one test gets started. Every
145 # test starts another test after completing.
146 for (@single_worker) {
148 take_job($_, $single_cv, \@testfiles);
155 # print empty lines to seperate failed tests from statuslines
159 my ($test, $output) = @$_;
160 say "no output for $test" unless $output;
161 Log "output for $test:";
163 # print error messages of failed tests
164 say for $output =~ /^not ok.+\n+((?:^#.+\n)+)/mg
168 $harness->summary($aggregator);
172 # 5: Save the timings for better scheduling/prediction next run.
173 $timings{GLOBAL} = time() - $timings{GLOBAL};
174 open(my $fh, '>', '.last_run_timings.json');
175 print $fh encode_json(\%timings);
178 # 6: Print the slowest test files.
179 my @slowest = map { $_->[0] }
180 sort { $b->[1] <=> $a->[1] }
181 map { [$_, $timings{$_}] }
182 grep { !/^GLOBAL$/ } keys %timings;
184 say 'The slowest tests are:';
185 printf("\t%s with %.2f seconds\n", $_, $timings{$_})
186 for @slowest[0..($#slowest > 4 ? 4 : $#slowest)];
188 # When we are running precisely one test, print the output. Makes developing
189 # with a single testcase easier.
190 if ($numtests == 1) {
193 say StartXDummy::slurp($logfile);
201 # Takes a test from the beginning of @testfiles and runs it.
203 # The TAP::Parser (which reads the test output) will get called as soon as
204 # there is some activity on the stdout file descriptor of the test process
205 # (using an AnyEvent->io watcher).
207 # When a test completes and @done contains $num entries, the $cv condvar gets
208 # triggered to finish testing.
211 my ($worker, $cv, $tests) = @_;
213 my $test = shift @$tests
216 my $display = $worker->{display};
218 Log status($display, "$test: starting");
219 $timings{$test} = time();
220 worker_next($worker, $test);
222 # create a TAP::Parser with an in-memory fh
224 my $parser = TAP::Parser->new({
225 source => do { open(my $fh, '<', \$output); $fh },
228 my $ipc = $worker->{ipc};
235 state $tests_completed = 0;
238 sysread($ipc, my $buf, 4096) or die "sysread: $!";
241 $buf = $partial . $buf;
245 # make sure we feed TAP::Parser complete lines so it doesn't blow up
246 if (substr($buf, -1, 1) ne "\n") {
247 my $nl = rindex($buf, "\n");
253 # strip partial from buffer
254 $partial = substr($buf, $nl + 1, '');
257 # count lines before stripping eof-marker otherwise we might
258 # end up with for (1 .. 0) { } which would effectivly skip the loop
259 my $lines = $buf =~ tr/\n//;
260 my $t_eof = $buf =~ s/^$TestWorker::EOF$//m;
265 my $result = $parser->next;
266 if (defined($result) and $result->is_test) {
268 status($display, "$test: [$tests_completed/??] ");
272 return unless $t_eof;
274 Log status($display, "$test: finished");
275 $timings{$test} = time() - $timings{$test};
276 status_completed(scalar @done);
278 $aggregator->add($test, $parser);
279 push @done, [ $test, $output ];
282 take_job($worker, $cv, $tests);
288 $_->() for our @CLEANUP;
292 # must be in a begin block because we C<exit 0> above
295 require Carp; Carp::cluck("Caught SIG$_[0]\n");
297 } for qw(INT TERM QUIT KILL PIPE)
304 complete-run.pl - Run the i3 testsuite
308 complete-run.pl [files...]
312 To run the whole testsuite on a reasonable number of Xdummy instances (your
313 running X11 will not be touched), run:
316 To run only a specific test (useful when developing a new feature), run:
317 ./complete-run t/100-fullscreen.t
325 Specifies which X11 display should be used. Can be specified multiple times and
326 will parallelize the tests:
328 # Run tests on the second X server
329 ./complete-run.pl -d :1
331 # Run four tests in parallel on some Xdummy servers
332 ./complete-run.pl -d :1,:2,:3,:4
334 Note that it is not necessary to specify this anymore. If omitted,
335 complete-run.pl will start (num_cores * 2) Xdummy instances.
339 Runs i3 under valgrind to find memory problems. The output will be available in
340 C<latest/valgrind-for-$test.log>.
344 Runs i3 under strace to trace system calls. The output will be available in
345 C<latest/strace-for-$test.log>.
349 Runs i3 under xtrace to trace X11 requests/replies. The output will be
350 available in C<latest/xtrace-for-$test.log>.
352 =item B<--coverage-testing>
354 Exits i3 cleanly (instead of kill -9) to make coverage testing work properly.
358 Number of Xdummy instances to start (if you don’t want to start num_cores * 2
359 instances for some reason).
361 # Run all tests on a single Xdummy instance
362 ./complete-run.pl -p 1