2 # vim:ts=4:sw=4:expandtab
4 # © 2010-2011 Michael Stapelberg and contributors
6 # syntax: ./complete-run.pl --display :1 --display :2
7 # to run the test suite on the X11 displays :1 and :2
8 # use 'Xdummy :1' and 'Xdummy :2' before to start two
15 # the following are modules which ship with Perl (>= 5.10):
18 use File::Basename qw(basename);
19 use File::Temp qw(tempfile tempdir);
23 use Time::HiRes qw(sleep gettimeofday tv_interval);
26 use TAP::Parser::Aggregator;
27 # these are shipped with the testsuite
30 # the following modules are not shipped with Perl
33 use AnyEvent::I3 qw(:all);
36 # We actually use AnyEvent to make sure it loads an event loop implementation.
37 # Afterwards, we overwrite SIGCHLD:
38 my $cv = AnyEvent->condvar;
40 # Install a dummy CHLD handler to overwrite the CHLD handler of AnyEvent.
41 # AnyEvent’s handler wait()s for every child which conflicts with TAP (TAP
42 # needs to get the exit status to determine if a test is successful).
46 # reads in a whole file
48 open my $fh, '<', shift;
53 my $coverage_testing = 0;
57 my $result = GetOptions(
58 "coverage-testing" => \$coverage_testing,
59 "valgrind" => \$valgrind,
60 "display=s" => \@displays,
63 @displays = split(/,/, join(',', @displays));
64 @displays = map { s/ //g; $_ } @displays;
66 @displays = qw(:1) if @displays == 0;
68 # connect to all displays for two reasons:
69 # 1: check if the display actually works
70 # 2: keep the connection open so that i3 is not the only client. this prevents
71 # the X server from exiting (Xdummy will restart it, but not quick enough
75 for my $display (@displays) {
77 my $x = X11::XCB->new($display, $screen);
79 say STDERR "WARNING: Not using X11 display $display, could not connect";
82 push @wdisplays, $display;
86 my $config = slurp('i3-test.config');
88 # 1: get a list of all testcases
89 my @testfiles = @ARGV;
91 # if no files were passed on command line, run all tests from t/
92 @testfiles = <t/*.t> if @testfiles == 0;
94 # 2: create an output directory for this test-run
95 my $outdir = "testsuite-";
96 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
97 $outdir .= `git describe --tags`;
99 mkdir($outdir) or die "Could not create $outdir";
100 unlink("latest") if -e "latest";
101 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
105 my $num = @testfiles;
106 my $harness = TAP::Harness->new({ });
108 my $aggregator = TAP::Parser::Aggregator->new();
109 $aggregator->start();
111 # We start tests concurrently: For each display, one test gets started. Every
112 # test starts another test after completing.
113 take_job($_) for @wdisplays;
116 # Takes a test from the beginning of @testfiles and runs it.
118 # The TAP::Parser (which reads the test output) will get called as soon as
119 # there is some activity on the stdout file descriptor of the test process
120 # (using an AnyEvent->io watcher).
122 # When a test completes and @done contains $num entries, the $cv condvar gets
123 # triggered to finish testing.
128 my $test = shift @testfiles;
130 my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
131 my $logpath = "$outdir/i3-log-for-" . basename($test);
133 my ($fh, $tmpfile) = tempfile('i3-run-cfg.XXXXXX', UNLINK => 1);
135 say $fh "ipc-socket /tmp/nested-$display";
138 my $activate_cv = AnyEvent->condvar;
139 my $time_before_start = [gettimeofday];
143 $activate_cv->send(1);
146 unix_socket_path => "/tmp/nested-$display-activation",
148 configfile => $tmpfile,
151 valgrind => $valgrind,
156 $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
157 say "child died. pid = $pid";
158 undef $child_watcher;
163 # Don’t bother killing i3 when we haven’t started it
164 return if $dont_start;
166 my $kill_cv = AnyEvent->condvar;
168 # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
169 # files are not written) and fallback to killing it
170 if ($coverage_testing || $valgrind) {
172 say "[$display] Exiting i3 cleanly...";
173 my $i3 = i3("/tmp/nested-$display");
174 $i3->connect->cb(sub {
176 # Could not connect to i3, just kill -9 it
177 kill(9, $pid) or die "Could not kill i3 using kill($pid)";
180 # Connected. Now send exit and continue once that’s acked.
181 $i3->command('exit')->cb(sub {
187 # No coverage testing or valgrind? Just kill -9 i3.
188 kill(9, $pid) or die "Could not kill i3 using kill($pid)";
195 # This will be called as soon as i3 is running and answered to our
197 $activate_cv->cb(sub {
198 my $time_activating = [gettimeofday];
199 my $start_duration = tv_interval($time_before_start, $time_activating);
200 my ($status) = $activate_cv->recv;
202 say "[$display] Not starting i3, testcase does that";
204 say "[$display] i3 startup: took " . sprintf("%.2f", $start_duration) . "s, status = $status";
207 say "[$display] Running $test with logfile $logpath";
210 open(my $spool, '>', \$output);
211 my $parser = TAP::Parser->new({
212 exec => [ 'sh', '-c', qq|DISPLAY=$display LOGPATH="$logpath" OUTDIR="$outdir" VALGRIND=$valgrind /usr/bin/perl -Ilib $test| ],
218 my ($stdout, $stderr) = $parser->get_select_handles;
219 for my $handle ($parser->get_select_handles) {
225 # Ignore activity on stderr (unnecessary with merge => 1,
226 # but let’s keep it in here if we want to use merge => 0
227 # for some reason in the future).
228 return if defined($stderr) and $handle == $stderr;
230 my $result = $parser->next;
231 if (defined($result)) {
232 # TODO: check if we should bail out
236 # $result is not defined, we are done parsing
237 say "[$display] $test finished";
238 close($parser->delete_spool);
239 $aggregator->add($test, $parser);
240 push @done, [ $test, $output ];
242 my $exitcv = $kill_i3->();
245 undef $_ for @watchers;
264 my ($test, $output) = @$_;
265 say "output for $test:";
270 $harness->summary($aggregator);