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);
23 use TAP::Parser::Aggregator;
24 use File::Basename qw(basename);
25 use AnyEvent::I3 qw(:all);
28 use Time::HiRes qw(sleep gettimeofday tv_interval);
30 use IO::Socket::UNIX; # core
34 # install a dummy CHLD handler to overwrite the CHLD handler of AnyEvent / EV
35 # XXX: we could maybe also use a different loop than the default loop in EV?
39 # reads in a whole file
41 open my $fh, '<', shift;
46 my $coverage_testing = 0;
49 my $result = GetOptions(
50 "coverage-testing" => \$coverage_testing,
51 "display=s" => \@displays,
54 @displays = split(/,/, join(',', @displays));
55 @displays = map { s/ //g; $_ } @displays;
57 @displays = qw(:1) if @displays == 0;
59 # connect to all displays for two reasons:
60 # 1: check if the display actually works
61 # 2: keep the connection open so that i3 is not the only client. this prevents
62 # the X server from exiting (Xdummy will restart it, but not quick enough
66 for my $display (@displays) {
68 my $x = X11::XCB->new($display, $screen);
70 say STDERR "WARNING: Not using X11 display $display, could not connect";
73 push @wdisplays, $display;
77 my $config = slurp('i3-test.config');
79 # 1: get a list of all testcases
80 my @testfiles = @ARGV;
82 # if no files were passed on command line, run all tests from t/
83 @testfiles = <t/*.t> if @testfiles == 0;
85 # 2: create an output directory for this test-run
86 my $outdir = "testsuite-";
87 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
88 $outdir .= `git describe --tags`;
90 mkdir($outdir) or die "Could not create $outdir";
91 unlink("latest") if -e "latest";
92 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
97 my $harness = TAP::Harness->new({ });
99 my $aggregator = TAP::Parser::Aggregator->new();
100 $aggregator->start();
102 my $cv = AnyEvent->condvar;
104 # We start tests concurrently: For each display, one test gets started. Every
105 # test starts another test after completing.
106 take_job($_) for @wdisplays;
109 # Takes a test from the beginning of @testfiles and runs it.
111 # The TAP::Parser (which reads the test output) will get called as soon as
112 # there is some activity on the stdout file descriptor of the test process
113 # (using an AnyEvent->io watcher).
115 # When a test completes and @done contains $num entries, the $cv condvar gets
116 # triggered to finish testing.
121 my $test = shift @testfiles;
123 my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
124 my $logpath = "$outdir/i3-log-for-" . basename($test);
126 my ($fh, $tmpfile) = tempfile('i3-run-cfg.XXXXXX', UNLINK => 1);
128 say $fh "ipc-socket /tmp/nested-$display";
131 my $activate_cv = AnyEvent->condvar;
132 my $time_before_start = [gettimeofday];
134 # remove the old unix socket
135 unlink("/tmp/nested-$display-activation");
137 # pass all file descriptors up to three to the children.
138 # we need to set this flag before opening the socket.
139 open(my $fdtest, '<', '/dev/null');
140 $^F = fileno($fdtest);
142 my $socket = IO::Socket::UNIX->new(
144 Local => "/tmp/nested-$display-activation",
148 if (!defined($pid)) {
149 die "could not fork()";
152 $ENV{LISTEN_PID} = $$;
153 $ENV{LISTEN_FDS} = 1;
154 $ENV{DISPLAY} = $display;
158 POSIX::dup2(fileno($socket), 3);
161 my $i3cmd = abs_path("../i3") . " -V -d all --disable-signalhandler";
162 my $cmd = "exec $i3cmd -c $tmpfile >$logpath 2>&1";
163 exec "/bin/sh", '-c', $cmd;
165 # if we are still here, i3 could not be found or exec failed. bail out.
170 $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
171 say "child died. pid = $pid";
172 undef $child_watcher;
175 # close the socket, the child process should be the only one which keeps a file
176 # descriptor on the listening socket.
179 # We now connect (will succeed immediately) and send a request afterwards.
180 # As soon as the reply is there, i3 is considered ready.
181 my $cl = IO::Socket::UNIX->new(Peer => "/tmp/nested-$display-activation");
183 $hdl = AnyEvent::Handle->new(fh => $cl, on_error => sub { $activate_cv->send(0) });
185 # send a get_tree message without payload
186 $hdl->push_write('i3-ipc' . pack("LL", 0, 4));
189 $hdl->push_read(chunk => 1, => sub {
191 $activate_cv->send(1);
199 $pid = $start_i3->() unless $dont_start;
202 # Don’t bother killing i3 when we haven’t started it
203 return if $dont_start;
205 # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
206 # files are not written) and fallback to killing it
207 if ($coverage_testing) {
210 say "Exiting i3 cleanly...";
211 i3("/tmp/nested-$display")->command('exit')->recv;
217 say "[$display] killing i3";
218 kill(9, $pid) or die "could not kill i3";
221 # This will be called as soon as i3 is running and answered to our
223 $activate_cv->cb(sub {
224 my $time_activating = [gettimeofday];
225 my $start_duration = tv_interval($time_before_start, $time_activating);
226 my ($status) = $activate_cv->recv;
228 say "[$display] Not starting i3, testcase does that";
230 say "[$display] i3 startup: took " . sprintf("%.2f", $start_duration) . "s, status = $status";
233 say "[$display] Running $test with logfile $logpath";
236 my $parser = TAP::Parser->new({
237 exec => [ 'sh', '-c', qq|DISPLAY=$display LOGPATH="$logpath" /usr/bin/perl -It/lib $test| ],
238 spool => IO::Scalar->new(\$output),
243 my ($stdout, $stderr) = $parser->get_select_handles;
244 for my $handle ($parser->get_select_handles) {
250 # Ignore activity on stderr (unnecessary with merge => 1,
251 # but let’s keep it in here if we want to use merge => 0
252 # for some reason in the future).
253 return if defined($stderr) and $handle == $stderr;
255 my $result = $parser->next;
256 if (defined($result)) {
257 # TODO: check if we should bail out
261 # $result is not defined, we are done parsing
262 say "[$display] $test finished";
263 close($parser->delete_spool);
264 $aggregator->add($test, $parser);
265 push @done, [ $test, $output ];
269 undef $_ for @watchers;
281 $activate_cv->send(1) if $dont_start;
289 my ($test, $output) = @$_;
290 say "output for $test:";
295 $harness->summary($aggregator);