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):
12 use File::Basename qw(basename);
13 use File::Temp qw(tempfile tempdir);
17 use Time::HiRes qw(sleep gettimeofday tv_interval);
20 use TAP::Parser::Aggregator;
21 # these are shipped with the testsuite
25 # the following modules are not shipped with Perl
28 use AnyEvent::I3 qw(:all);
31 # We actually use AnyEvent to make sure it loads an event loop implementation.
32 # Afterwards, we overwrite SIGCHLD:
33 my $cv = AnyEvent->condvar;
35 # Install a dummy CHLD handler to overwrite the CHLD handler of AnyEvent.
36 # AnyEvent’s handler wait()s for every child which conflicts with TAP (TAP
37 # needs to get the exit status to determine if a test is successful).
41 # reads in a whole file
43 open(my $fh, '<', shift);
48 my $coverage_testing = 0;
51 # Number of tests to run in parallel. Important to know how many Xdummy
52 # instances we need to start (unless @displays are given). Defaults to
58 my $result = GetOptions(
59 "coverage-testing" => \$coverage_testing,
60 "valgrind" => \$valgrind,
61 "display=s" => \@displays,
62 "parallel=i" => \$parallel,
66 pod2usage(-verbose => 2, -exitcode => 0) if $help;
68 @displays = split(/,/, join(',', @displays));
69 @displays = map { s/ //g; $_ } @displays;
71 # No displays specified, let’s start some Xdummy instances.
73 my ($displays, $pids) = start_xdummy($parallel);
74 @displays = @$displays;
78 # connect to all displays for two reasons:
79 # 1: check if the display actually works
80 # 2: keep the connection open so that i3 is not the only client. this prevents
81 # the X server from exiting (Xdummy will restart it, but not quick enough
85 for my $display (@displays) {
87 my $x = X11::XCB->new($display, $screen);
89 say STDERR "WARNING: Not using X11 display $display, could not connect";
92 push @wdisplays, $display;
96 die "No usable displays found" if @wdisplays == 0;
98 my $config = slurp('i3-test.config');
100 # 1: get a list of all testcases
101 my @testfiles = @ARGV;
103 # if no files were passed on command line, run all tests from t/
104 @testfiles = <t/*.t> if @testfiles == 0;
106 # 2: create an output directory for this test-run
107 my $outdir = "testsuite-";
108 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
109 $outdir .= `git describe --tags`;
111 mkdir($outdir) or die "Could not create $outdir";
112 unlink("latest") if -e "latest";
113 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
117 my $num = @testfiles;
118 my $harness = TAP::Harness->new({ });
120 my $aggregator = TAP::Parser::Aggregator->new();
121 $aggregator->start();
123 # We start tests concurrently: For each display, one test gets started. Every
124 # test starts another test after completing.
125 take_job($_) for @wdisplays;
128 # Takes a test from the beginning of @testfiles and runs it.
130 # The TAP::Parser (which reads the test output) will get called as soon as
131 # there is some activity on the stdout file descriptor of the test process
132 # (using an AnyEvent->io watcher).
134 # When a test completes and @done contains $num entries, the $cv condvar gets
135 # triggered to finish testing.
140 my $test = shift @testfiles;
142 my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
143 my $logpath = "$outdir/i3-log-for-" . basename($test);
145 my ($fh, $tmpfile) = tempfile('i3-run-cfg.XXXXXX', UNLINK => 1);
147 say $fh "ipc-socket /tmp/nested-$display";
150 my $activate_cv = AnyEvent->condvar;
151 my $time_before_start = [gettimeofday];
155 $activate_cv->send(1);
158 unix_socket_path => "/tmp/nested-$display-activation",
160 configfile => $tmpfile,
163 valgrind => $valgrind,
168 $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
169 say "child died. pid = $pid";
170 undef $child_watcher;
175 my $kill_cv = AnyEvent->condvar;
177 # Don’t bother killing i3 when we haven’t started it
183 # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
184 # files are not written) and fallback to killing it
185 if ($coverage_testing || $valgrind) {
187 say "[$display] Exiting i3 cleanly...";
188 my $i3 = i3("/tmp/nested-$display");
189 $i3->connect->cb(sub {
191 # Could not connect to i3, just kill -9 it
192 kill(9, $pid) or die "Could not kill i3 using kill($pid)";
195 # Connected. Now send exit and continue once that’s acked.
196 $i3->command('exit')->cb(sub {
202 # No coverage testing or valgrind? Just kill -9 i3.
203 kill(9, $pid) or die "Could not kill i3 using kill($pid)";
210 # This will be called as soon as i3 is running and answered to our
212 $activate_cv->cb(sub {
213 my $time_activating = [gettimeofday];
214 my $start_duration = tv_interval($time_before_start, $time_activating);
215 my ($status) = $activate_cv->recv;
217 say "[$display] Not starting i3, testcase does that";
219 say "[$display] i3 startup: took " . sprintf("%.2f", $start_duration) . "s, status = $status";
222 say "[$display] Running $test with logfile $logpath";
225 open(my $spool, '>', \$output);
226 my $parser = TAP::Parser->new({
227 exec => [ 'sh', '-c', qq|DISPLAY=$display LOGPATH="$logpath" OUTDIR="$outdir" VALGRIND=$valgrind /usr/bin/perl -Ilib $test| ],
233 my ($stdout, $stderr) = $parser->get_select_handles;
234 for my $handle ($parser->get_select_handles) {
240 # Ignore activity on stderr (unnecessary with merge => 1,
241 # but let’s keep it in here if we want to use merge => 0
242 # for some reason in the future).
243 return if defined($stderr) and $handle == $stderr;
245 my $result = $parser->next;
246 if (defined($result)) {
247 # TODO: check if we should bail out
251 # $result is not defined, we are done parsing
252 say "[$display] $test finished";
253 close($parser->delete_spool);
254 $aggregator->add($test, $parser);
255 push @done, [ $test, $output ];
257 my $exitcv = $kill_i3->();
260 undef $_ for @watchers;
278 # Disable buffering to make sure the output and summary appear before we exit.
282 my ($test, $output) = @$_;
283 say "output for $test:";
288 $harness->summary($aggregator);
290 kill(15, $_) for @childpids;
296 complete-run.pl - Run the i3 testsuite
300 complete-run.pl [files...]
304 To run the whole testsuite on a reasonable number of Xdummy instances (your
305 running X11 will not be touched), run:
308 To run only a specific test (useful when developing a new feature), run:
309 ./complete-run t/100-fullscreen.t
317 Specifies which X11 display should be used. Can be specified multiple times and
318 will parallelize the tests:
320 # Run tests on the second X server
321 ./complete-run.pl -d :1
323 # Run four tests in parallel on some Xdummy servers
324 ./complete-run.pl -d :1,:2,:3,:4
326 Note that it is not necessary to specify this anymore. If omitted,
327 complete-run.pl will start (num_cores * 2) Xdummy instances.
331 Runs i3 under valgrind to find memory problems. The output will be available in
332 C<latest/valgrind.log>.
334 =item B<--coverage-testing>
336 Exits i3 cleanly (instead of kill -9) to make coverage testing work properly.
340 Number of Xdummy instances to start (if you don’t want to start num_cores * 2
341 instances for some reason).
343 # Run all tests on a single Xdummy instance
344 ./complete-run.pl -p 1