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):
19 use File::Basename qw(basename);
20 use File::Temp qw(tempfile tempdir);
24 use Time::HiRes qw(sleep gettimeofday tv_interval);
27 use TAP::Parser::Aggregator;
28 # these are shipped with the testsuite
31 # the following modules are not shipped with Perl
34 use AnyEvent::I3 qw(:all);
37 # We actually use AnyEvent to make sure it loads an event loop implementation.
38 # Afterwards, we overwrite SIGCHLD:
39 my $cv = AnyEvent->condvar;
41 # Install a dummy CHLD handler to overwrite the CHLD handler of AnyEvent.
42 # AnyEvent’s handler wait()s for every child which conflicts with TAP (TAP
43 # needs to get the exit status to determine if a test is successful).
47 # reads in a whole file
49 open my $fh, '<', shift;
54 my $coverage_testing = 0;
59 my $result = GetOptions(
60 "coverage-testing" => \$coverage_testing,
61 "valgrind" => \$valgrind,
62 "display=s" => \@displays,
66 pod2usage(0) if $help;
68 @displays = split(/,/, join(',', @displays));
69 @displays = map { s/ //g; $_ } @displays;
71 @displays = qw(:1) if @displays == 0;
73 # connect to all displays for two reasons:
74 # 1: check if the display actually works
75 # 2: keep the connection open so that i3 is not the only client. this prevents
76 # the X server from exiting (Xdummy will restart it, but not quick enough
80 for my $display (@displays) {
82 my $x = X11::XCB->new($display, $screen);
84 say STDERR "WARNING: Not using X11 display $display, could not connect";
87 push @wdisplays, $display;
91 my $config = slurp('i3-test.config');
93 # 1: get a list of all testcases
94 my @testfiles = @ARGV;
96 # if no files were passed on command line, run all tests from t/
97 @testfiles = <t/*.t> if @testfiles == 0;
99 # 2: create an output directory for this test-run
100 my $outdir = "testsuite-";
101 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
102 $outdir .= `git describe --tags`;
104 mkdir($outdir) or die "Could not create $outdir";
105 unlink("latest") if -e "latest";
106 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
110 my $num = @testfiles;
111 my $harness = TAP::Harness->new({ });
113 my $aggregator = TAP::Parser::Aggregator->new();
114 $aggregator->start();
116 # We start tests concurrently: For each display, one test gets started. Every
117 # test starts another test after completing.
118 take_job($_) for @wdisplays;
121 # Takes a test from the beginning of @testfiles and runs it.
123 # The TAP::Parser (which reads the test output) will get called as soon as
124 # there is some activity on the stdout file descriptor of the test process
125 # (using an AnyEvent->io watcher).
127 # When a test completes and @done contains $num entries, the $cv condvar gets
128 # triggered to finish testing.
133 my $test = shift @testfiles;
135 my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
136 my $logpath = "$outdir/i3-log-for-" . basename($test);
138 my ($fh, $tmpfile) = tempfile('i3-run-cfg.XXXXXX', UNLINK => 1);
140 say $fh "ipc-socket /tmp/nested-$display";
143 my $activate_cv = AnyEvent->condvar;
144 my $time_before_start = [gettimeofday];
148 $activate_cv->send(1);
151 unix_socket_path => "/tmp/nested-$display-activation",
153 configfile => $tmpfile,
156 valgrind => $valgrind,
161 $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
162 say "child died. pid = $pid";
163 undef $child_watcher;
168 my $kill_cv = AnyEvent->condvar;
170 # Don’t bother killing i3 when we haven’t started it
176 # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
177 # files are not written) and fallback to killing it
178 if ($coverage_testing || $valgrind) {
180 say "[$display] Exiting i3 cleanly...";
181 my $i3 = i3("/tmp/nested-$display");
182 $i3->connect->cb(sub {
184 # Could not connect to i3, just kill -9 it
185 kill(9, $pid) or die "Could not kill i3 using kill($pid)";
188 # Connected. Now send exit and continue once that’s acked.
189 $i3->command('exit')->cb(sub {
195 # No coverage testing or valgrind? Just kill -9 i3.
196 kill(9, $pid) or die "Could not kill i3 using kill($pid)";
203 # This will be called as soon as i3 is running and answered to our
205 $activate_cv->cb(sub {
206 my $time_activating = [gettimeofday];
207 my $start_duration = tv_interval($time_before_start, $time_activating);
208 my ($status) = $activate_cv->recv;
210 say "[$display] Not starting i3, testcase does that";
212 say "[$display] i3 startup: took " . sprintf("%.2f", $start_duration) . "s, status = $status";
215 say "[$display] Running $test with logfile $logpath";
218 open(my $spool, '>', \$output);
219 my $parser = TAP::Parser->new({
220 exec => [ 'sh', '-c', qq|DISPLAY=$display LOGPATH="$logpath" OUTDIR="$outdir" VALGRIND=$valgrind /usr/bin/perl -Ilib $test| ],
226 my ($stdout, $stderr) = $parser->get_select_handles;
227 for my $handle ($parser->get_select_handles) {
233 # Ignore activity on stderr (unnecessary with merge => 1,
234 # but let’s keep it in here if we want to use merge => 0
235 # for some reason in the future).
236 return if defined($stderr) and $handle == $stderr;
238 my $result = $parser->next;
239 if (defined($result)) {
240 # TODO: check if we should bail out
244 # $result is not defined, we are done parsing
245 say "[$display] $test finished";
246 close($parser->delete_spool);
247 $aggregator->add($test, $parser);
248 push @done, [ $test, $output ];
250 my $exitcv = $kill_i3->();
253 undef $_ for @watchers;
272 my ($test, $output) = @$_;
273 say "output for $test:";
278 $harness->summary($aggregator);
284 complete-run.pl - Run the i3 testsuite
288 complete-run.pl [files...]
296 Specifies which X11 display should be used. Can be specified multiple times and
297 will parallelize the tests:
299 # Run tests on the second X server
300 ./complete-run.pl -d :1
302 # Run four tests in parallel on some Xdummy servers
303 ./complete-run.pl -d :1,:2,:3,:4
307 Runs i3 under valgrind to find memory problems. The output will be available in
308 C<latest/valgrind.log>.
310 =item B<--coverage-testing>
312 Exits i3 cleanly (instead of kill -9) to make coverage testing work properly.