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);
25 use TAP::Parser::Aggregator;
26 use File::Basename qw(basename);
27 use AnyEvent::I3 qw(:all);
30 use Time::HiRes qw(sleep gettimeofday tv_interval);
32 use IO::Socket::UNIX; # core
36 # open a file so that we get file descriptor 3. we will later close it in the
37 # child and dup() the listening socket file descriptor to 3 to pass it to i3
38 open(my $reserved, '<', '/dev/null');
39 if (fileno($reserved) != 3) {
40 warn "Socket file descriptor is not 3.";
41 warn "Please don't start this script within a subshell of vim or something.";
45 # install a dummy CHLD handler to overwrite the CHLD handler of AnyEvent / EV
46 # XXX: we could maybe also use a different loop than the default loop in EV?
50 # reads in a whole file
52 open my $fh, '<', shift;
57 my $coverage_testing = 0;
60 my $result = GetOptions(
61 "coverage-testing" => \$coverage_testing,
62 "display=s" => \@displays,
65 @displays = split(/,/, join(',', @displays));
66 @displays = map { s/ //g; $_ } @displays;
68 @displays = qw(:1) if @displays == 0;
70 # connect to all displays for two reasons:
71 # 1: check if the display actually works
72 # 2: keep the connection open so that i3 is not the only client. this prevents
73 # the X server from exiting (Xdummy will restart it, but not quick enough
77 for my $display (@displays) {
79 my $x = X11::XCB->new($display, $screen);
81 say STDERR "WARNING: Not using X11 display $display, could not connect";
84 push @wdisplays, $display;
88 my $config = slurp('i3-test.config');
90 # 1: get a list of all testcases
91 my @testfiles = @ARGV;
93 # if no files were passed on command line, run all tests from t/
94 @testfiles = <t/*.t> if @testfiles == 0;
96 # 2: create an output directory for this test-run
97 my $outdir = "testsuite-";
98 $outdir .= DateTime->now->strftime("%Y-%m-%d-%H-%M-%S-");
99 $outdir .= `git describe --tags`;
101 mkdir($outdir) or die "Could not create $outdir";
102 unlink("latest") if -e "latest";
103 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
107 my $num = @testfiles;
108 my $harness = TAP::Harness->new({ });
110 my $aggregator = TAP::Parser::Aggregator->new();
111 $aggregator->start();
113 my $cv = AnyEvent->condvar;
115 # We start tests concurrently: For each display, one test gets started. Every
116 # test starts another test after completing.
117 take_job($_) for @wdisplays;
120 # Takes a test from the beginning of @testfiles and runs it.
122 # The TAP::Parser (which reads the test output) will get called as soon as
123 # there is some activity on the stdout file descriptor of the test process
124 # (using an AnyEvent->io watcher).
126 # When a test completes and @done contains $num entries, the $cv condvar gets
127 # triggered to finish testing.
132 my $test = shift @testfiles;
134 my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
135 my $logpath = "$outdir/i3-log-for-" . basename($test);
137 my ($fh, $tmpfile) = tempfile('i3-run-cfg.XXXXXX', UNLINK => 1);
139 say $fh "ipc-socket /tmp/nested-$display";
142 my $activate_cv = AnyEvent->condvar;
143 my $time_before_start = [gettimeofday];
145 # remove the old unix socket
146 unlink("/tmp/nested-$display-activation");
148 # pass all file descriptors up to three to the children.
149 # we need to set this flag before opening the socket.
150 open(my $fdtest, '<', '/dev/null');
151 $^F = fileno($fdtest);
153 my $socket = IO::Socket::UNIX->new(
155 Local => "/tmp/nested-$display-activation",
159 if (!defined($pid)) {
160 die "could not fork()";
163 $ENV{LISTEN_PID} = $$;
164 $ENV{LISTEN_FDS} = 1;
165 $ENV{DISPLAY} = $display;
169 POSIX::dup2(fileno($socket), 3);
172 my $i3cmd = abs_path("../i3") . " -V -d all --disable-signalhandler";
173 my $cmd = "exec $i3cmd -c $tmpfile >$logpath 2>&1";
174 exec "/bin/sh", '-c', $cmd;
176 # if we are still here, i3 could not be found or exec failed. bail out.
181 $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
182 say "child died. pid = $pid";
183 undef $child_watcher;
186 # close the socket, the child process should be the only one which keeps a file
187 # descriptor on the listening socket.
190 # We now connect (will succeed immediately) and send a request afterwards.
191 # As soon as the reply is there, i3 is considered ready.
192 my $cl = IO::Socket::UNIX->new(Peer => "/tmp/nested-$display-activation");
194 $hdl = AnyEvent::Handle->new(fh => $cl, on_error => sub { $activate_cv->send(0) });
196 # send a get_tree message without payload
197 $hdl->push_write('i3-ipc' . pack("LL", 0, 4));
200 $hdl->push_read(chunk => 1, => sub {
202 $activate_cv->send(1);
210 $pid = $start_i3->() unless $dont_start;
213 # Don’t bother killing i3 when we haven’t started it
214 return if $dont_start;
216 # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
217 # files are not written) and fallback to killing it
218 if ($coverage_testing) {
221 say "Exiting i3 cleanly...";
222 i3("/tmp/nested-$display")->command('exit')->recv;
228 say "[$display] killing i3";
229 kill(9, $pid) or die "could not kill i3";
232 # This will be called as soon as i3 is running and answered to our
234 $activate_cv->cb(sub {
235 my $time_activating = [gettimeofday];
236 my $start_duration = tv_interval($time_before_start, $time_activating);
237 my ($status) = $activate_cv->recv;
239 say "[$display] Not starting i3, testcase does that";
241 say "[$display] i3 startup: took " . sprintf("%.2f", $start_duration) . "s, status = $status";
244 say "[$display] Running $test with logfile $logpath";
247 my $parser = TAP::Parser->new({
248 exec => [ 'sh', '-c', qq|DISPLAY=$display LOGPATH="$logpath" /usr/bin/perl -It/lib $test| ],
249 spool => IO::Scalar->new(\$output),
254 my ($stdout, $stderr) = $parser->get_select_handles;
255 for my $handle ($parser->get_select_handles) {
261 # Ignore activity on stderr (unnecessary with merge => 1,
262 # but let’s keep it in here if we want to use merge => 0
263 # for some reason in the future).
264 return if defined($stderr) and $handle == $stderr;
266 my $result = $parser->next;
267 if (defined($result)) {
268 # TODO: check if we should bail out
272 # $result is not defined, we are done parsing
273 say "[$display] $test finished";
274 close($parser->delete_spool);
275 $aggregator->add($test, $parser);
276 push @done, [ $test, $output ];
280 undef $_ for @watchers;
292 $activate_cv->send(1) if $dont_start;
300 my ($test, $output) = @$_;
301 say "output for $test:";
306 $harness->summary($aggregator);