]> git.sur5r.net Git - i3/i3/blob - testcases/complete-run.pl
complete-run: Bugfix: return condvar when $dont_start is true
[i3/i3] / testcases / complete-run.pl
1 #!/usr/bin/env perl
2 # vim:ts=4:sw=4:expandtab
3 #
4 # © 2010-2011 Michael Stapelberg and contributors
5 #
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
9 # headless X11 servers
10 #
11
12 use strict;
13 use warnings;
14 use v5.10;
15 # the following are modules which ship with Perl (>= 5.10):
16 use Pod::Usage;
17 use Carp::Always;
18 use Cwd qw(abs_path);
19 use File::Basename qw(basename);
20 use File::Temp qw(tempfile tempdir);
21 use Getopt::Long;
22 use IO::Socket::UNIX;
23 use POSIX;
24 use Time::HiRes qw(sleep gettimeofday tv_interval);
25 use TAP::Harness;
26 use TAP::Parser;
27 use TAP::Parser::Aggregator;
28 # these are shipped with the testsuite
29 use lib qw(lib);
30 use SocketActivation;
31 # the following modules are not shipped with Perl
32 use AnyEvent;
33 use AnyEvent::Handle;
34 use AnyEvent::I3 qw(:all);
35 use X11::XCB;
36
37 # We actually use AnyEvent to make sure it loads an event loop implementation.
38 # Afterwards, we overwrite SIGCHLD:
39 my $cv = AnyEvent->condvar;
40
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).
44 $SIG{CHLD} = sub {
45 };
46
47 # reads in a whole file
48 sub slurp {
49     open my $fh, '<', shift;
50     local $/;
51     <$fh>;
52 }
53
54 my $coverage_testing = 0;
55 my $valgrind = 0;
56 my $help = 0;
57 my @displays = ();
58
59 my $result = GetOptions(
60     "coverage-testing" => \$coverage_testing,
61     "valgrind" => \$valgrind,
62     "display=s" => \@displays,
63     "help|?" => \$help,
64 );
65
66 pod2usage(0) if $help;
67
68 @displays = split(/,/, join(',', @displays));
69 @displays = map { s/ //g; $_ } @displays;
70
71 @displays = qw(:1) if @displays == 0;
72
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
77 #    sometimes)
78 my @conns;
79 my @wdisplays;
80 for my $display (@displays) {
81     my $screen;
82     my $x = X11::XCB->new($display, $screen);
83     if ($x->has_error) {
84         say STDERR "WARNING: Not using X11 display $display, could not connect";
85     } else {
86         push @conns, $x;
87         push @wdisplays, $display;
88     }
89 }
90
91 my $config = slurp('i3-test.config');
92
93 # 1: get a list of all testcases
94 my @testfiles = @ARGV;
95
96 # if no files were passed on command line, run all tests from t/
97 @testfiles = <t/*.t> if @testfiles == 0;
98
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`;
103 chomp($outdir);
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";
107
108 # 3: run all tests
109 my @done;
110 my $num = @testfiles;
111 my $harness = TAP::Harness->new({ });
112
113 my $aggregator = TAP::Parser::Aggregator->new();
114 $aggregator->start();
115
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;
119
120 #
121 # Takes a test from the beginning of @testfiles and runs it.
122 #
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).
126 #
127 # When a test completes and @done contains $num entries, the $cv condvar gets
128 # triggered to finish testing.
129 #
130 sub take_job {
131     my ($display) = @_;
132
133     my $test = shift @testfiles;
134     return unless $test;
135     my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
136     my $logpath = "$outdir/i3-log-for-" . basename($test);
137
138     my ($fh, $tmpfile) = tempfile('i3-run-cfg.XXXXXX', UNLINK => 1);
139     say $fh $config;
140     say $fh "ipc-socket /tmp/nested-$display";
141     close($fh);
142
143     my $activate_cv = AnyEvent->condvar;
144     my $time_before_start = [gettimeofday];
145
146     my $pid;
147     if ($dont_start) {
148         $activate_cv->send(1);
149     } else {
150         $pid = activate_i3(
151             unix_socket_path => "/tmp/nested-$display-activation",
152             display => $display,
153             configfile => $tmpfile,
154             outdir => $outdir,
155             logpath => $logpath,
156             valgrind => $valgrind,
157             cv => $activate_cv
158         );
159
160         my $child_watcher;
161         $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
162             say "child died. pid = $pid";
163             undef $child_watcher;
164         });
165     }
166
167     my $kill_i3 = sub {
168         my $kill_cv = AnyEvent->condvar;
169
170         # Don’t bother killing i3 when we haven’t started it
171         if ($dont_start) {
172             $kill_cv->send();
173             return $kill_cv;
174         }
175
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) {
179             my $exited = 0;
180             say "[$display] Exiting i3 cleanly...";
181             my $i3 = i3("/tmp/nested-$display");
182             $i3->connect->cb(sub {
183                 if (!$_[0]->recv) {
184                     # Could not connect to i3, just kill -9 it
185                     kill(9, $pid) or die "Could not kill i3 using kill($pid)";
186                     $kill_cv->send();
187                 } else {
188                     # Connected. Now send exit and continue once that’s acked.
189                     $i3->command('exit')->cb(sub {
190                         $kill_cv->send();
191                     });
192                 }
193             });
194         } else {
195             # No coverage testing or valgrind? Just kill -9 i3.
196             kill(9, $pid) or die "Could not kill i3 using kill($pid)";
197             $kill_cv->send();
198         }
199
200         return $kill_cv;
201     };
202
203     # This will be called as soon as i3 is running and answered to our
204     # IPC request
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;
209         if ($dont_start) {
210             say "[$display] Not starting i3, testcase does that";
211         } else {
212             say "[$display] i3 startup: took " . sprintf("%.2f", $start_duration) . "s, status = $status";
213         }
214
215         say "[$display] Running $test with logfile $logpath";
216
217         my $output;
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| ],
221             spool => $spool,
222             merge => 1,
223         });
224
225         my @watchers;
226         my ($stdout, $stderr) = $parser->get_select_handles;
227         for my $handle ($parser->get_select_handles) {
228             my $w;
229             $w = AnyEvent->io(
230                 fh => $handle,
231                 poll => 'r',
232                 cb => sub {
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;
237
238                     my $result = $parser->next;
239                     if (defined($result)) {
240                         # TODO: check if we should bail out
241                         return;
242                     }
243
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 ];
249
250                     my $exitcv = $kill_i3->();
251                     $exitcv->cb(sub {
252
253                         undef $_ for @watchers;
254                         if (@done == $num) {
255                             $cv->send;
256                         } else {
257                             take_job($display);
258                         }
259                     });
260                 }
261             );
262             push @watchers, $w;
263         }
264     });
265 }
266
267 $cv->recv;
268
269 $aggregator->stop();
270
271 for (@done) {
272     my ($test, $output) = @$_;
273     say "output for $test:";
274     say $output;
275 }
276
277 # 4: print summary
278 $harness->summary($aggregator);
279
280 __END__
281
282 =head1 NAME
283
284 complete-run.pl - Run the i3 testsuite
285
286 =head1 SYNOPSIS
287
288 complete-run.pl [files...]
289
290 =head1 OPTIONS
291
292 =over 8
293
294 =item B<--display>
295
296 Specifies which X11 display should be used. Can be specified multiple times and
297 will parallelize the tests:
298
299   # Run tests on the second X server
300   ./complete-run.pl -d :1
301
302   # Run four tests in parallel on some Xdummy servers
303   ./complete-run.pl -d :1,:2,:3,:4
304
305 =item B<--valgrind>
306
307 Runs i3 under valgrind to find memory problems. The output will be available in
308 C<latest/valgrind.log>.
309
310 =item B<--coverage-testing>
311
312 Exits i3 cleanly (instead of kill -9) to make coverage testing work properly.