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
16 use IO::Scalar; # not in core :\
17 use File::Temp qw(tempfile tempdir);
24 use TAP::Parser::Aggregator;
25 use File::Basename qw(basename);
26 use AnyEvent::I3 qw(:all);
29 use Time::HiRes qw(sleep gettimeofday tv_interval);
31 use IO::Socket::UNIX; # core
35 # install a dummy CHLD handler to overwrite the CHLD handler of AnyEvent / EV
36 # XXX: we could maybe also use a different loop than the default loop in EV?
40 # reads in a whole file
42 open my $fh, '<', shift;
47 my $coverage_testing = 0;
50 my $result = GetOptions(
51 "coverage-testing" => \$coverage_testing,
52 "display=s" => \@displays,
55 @displays = split(/,/, join(',', @displays));
56 @displays = map { s/ //g; $_ } @displays;
58 @displays = qw(:1) if @displays == 0;
60 # connect to all displays for two reasons:
61 # 1: check if the display actually works
62 # 2: keep the connection open so that i3 is not the only client. this prevents
63 # the X server from exiting (Xdummy will restart it, but not quick enough
67 for my $display (@displays) {
69 my $x = X11::XCB->new($display, $screen);
71 say STDERR "WARNING: Not using X11 display $display, could not connect";
74 push @wdisplays, $display;
78 my $config = slurp('i3-test.config');
80 # 1: get a list of all testcases
81 my @testfiles = @ARGV;
83 # if no files were passed on command line, run all tests from t/
84 @testfiles = <t/*.t> if @testfiles == 0;
86 # 2: create an output directory for this test-run
87 my $outdir = "testsuite-";
88 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
89 $outdir .= `git describe --tags`;
91 mkdir($outdir) or die "Could not create $outdir";
92 unlink("latest") if -e "latest";
93 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
98 my $harness = TAP::Harness->new({ });
100 my $aggregator = TAP::Parser::Aggregator->new();
101 $aggregator->start();
103 my $cv = AnyEvent->condvar;
105 # We start tests concurrently: For each display, one test gets started. Every
106 # test starts another test after completing.
107 take_job($_) for @wdisplays;
110 # Takes a test from the beginning of @testfiles and runs it.
112 # The TAP::Parser (which reads the test output) will get called as soon as
113 # there is some activity on the stdout file descriptor of the test process
114 # (using an AnyEvent->io watcher).
116 # When a test completes and @done contains $num entries, the $cv condvar gets
117 # triggered to finish testing.
122 my $test = shift @testfiles;
124 my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
125 my $logpath = "$outdir/i3-log-for-" . basename($test);
127 my ($fh, $tmpfile) = tempfile('i3-run-cfg.XXXXXX', UNLINK => 1);
129 say $fh "ipc-socket /tmp/nested-$display";
132 my $activate_cv = AnyEvent->condvar;
133 my $time_before_start = [gettimeofday];
135 # remove the old unix socket
136 unlink("/tmp/nested-$display-activation");
138 # pass all file descriptors up to three to the children.
139 # we need to set this flag before opening the socket.
140 open(my $fdtest, '<', '/dev/null');
141 $^F = fileno($fdtest);
143 my $socket = IO::Socket::UNIX->new(
145 Local => "/tmp/nested-$display-activation",
149 if (!defined($pid)) {
150 die "could not fork()";
153 $ENV{LISTEN_PID} = $$;
154 $ENV{LISTEN_FDS} = 1;
155 $ENV{DISPLAY} = $display;
159 POSIX::dup2(fileno($socket), 3);
162 my $i3cmd = abs_path("../i3") . " -V -d all --disable-signalhandler";
163 my $cmd = "exec $i3cmd -c $tmpfile >$logpath 2>&1";
164 exec "/bin/sh", '-c', $cmd;
166 # if we are still here, i3 could not be found or exec failed. bail out.
171 $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
172 say "child died. pid = $pid";
173 undef $child_watcher;
176 # close the socket, the child process should be the only one which keeps a file
177 # descriptor on the listening socket.
180 # We now connect (will succeed immediately) and send a request afterwards.
181 # As soon as the reply is there, i3 is considered ready.
182 my $cl = IO::Socket::UNIX->new(Peer => "/tmp/nested-$display-activation");
184 $hdl = AnyEvent::Handle->new(fh => $cl, on_error => sub { $activate_cv->send(0) });
186 # send a get_tree message without payload
187 $hdl->push_write('i3-ipc' . pack("LL", 0, 4));
190 $hdl->push_read(chunk => 1, => sub {
192 $activate_cv->send(1);
200 $pid = $start_i3->() unless $dont_start;
203 # Don’t bother killing i3 when we haven’t started it
204 return if $dont_start;
206 # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
207 # files are not written) and fallback to killing it
208 if ($coverage_testing) {
211 say "Exiting i3 cleanly...";
212 i3("/tmp/nested-$display")->command('exit')->recv;
218 say "[$display] killing i3";
219 kill(9, $pid) or die "could not kill i3";
222 # This will be called as soon as i3 is running and answered to our
224 $activate_cv->cb(sub {
225 my $time_activating = [gettimeofday];
226 my $start_duration = tv_interval($time_before_start, $time_activating);
227 my ($status) = $activate_cv->recv;
229 say "[$display] Not starting i3, testcase does that";
231 say "[$display] i3 startup: took " . sprintf("%.2f", $start_duration) . "s, status = $status";
234 say "[$display] Running $test with logfile $logpath";
237 my $parser = TAP::Parser->new({
238 exec => [ 'sh', '-c', qq|DISPLAY=$display LOGPATH="$logpath" /usr/bin/perl -It/lib $test| ],
239 spool => IO::Scalar->new(\$output),
244 my ($stdout, $stderr) = $parser->get_select_handles;
245 for my $handle ($parser->get_select_handles) {
251 # Ignore activity on stderr (unnecessary with merge => 1,
252 # but let’s keep it in here if we want to use merge => 0
253 # for some reason in the future).
254 return if defined($stderr) and $handle == $stderr;
256 my $result = $parser->next;
257 if (defined($result)) {
258 # TODO: check if we should bail out
262 # $result is not defined, we are done parsing
263 say "[$display] $test finished";
264 close($parser->delete_spool);
265 $aggregator->add($test, $parser);
266 push @done, [ $test, $output ];
270 undef $_ for @watchers;
282 $activate_cv->send(1) if $dont_start;
290 my ($test, $output) = @$_;
291 say "output for $test:";
296 $harness->summary($aggregator);