]> git.sur5r.net Git - i3/i3/blob - testcases/complete-run.pl
Don’t call ev_destroy_loop with ev < 4 in atexit (Thanks xeen)
[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 Cwd qw(abs_path);
17 use File::Basename qw(basename);
18 use File::Temp qw(tempfile tempdir);
19 use Getopt::Long;
20 use IO::Socket::UNIX;
21 use POSIX;
22 use Time::HiRes qw(sleep gettimeofday tv_interval);
23 use TAP::Harness;
24 use TAP::Parser;
25 use TAP::Parser::Aggregator;
26 # these are shipped with the testsuite
27 use lib qw(lib);
28 use SocketActivation;
29 # the following modules are not shipped with Perl
30 use AnyEvent;
31 use AnyEvent::Handle;
32 use AnyEvent::I3 qw(:all);
33 use X11::XCB;
34
35 # We actually use AnyEvent to make sure it loads an event loop implementation.
36 # Afterwards, we overwrite SIGCHLD:
37 my $cv = AnyEvent->condvar;
38
39 # Install a dummy CHLD handler to overwrite the CHLD handler of AnyEvent.
40 # AnyEvent’s handler wait()s for every child which conflicts with TAP (TAP
41 # needs to get the exit status to determine if a test is successful).
42 $SIG{CHLD} = sub {
43 };
44
45 # reads in a whole file
46 sub slurp {
47     open my $fh, '<', shift;
48     local $/;
49     <$fh>;
50 }
51
52 my $coverage_testing = 0;
53 my @displays = ();
54
55 my $result = GetOptions(
56     "coverage-testing" => \$coverage_testing,
57     "display=s" => \@displays,
58 );
59
60 @displays = split(/,/, join(',', @displays));
61 @displays = map { s/ //g; $_ } @displays;
62
63 @displays = qw(:1) if @displays == 0;
64
65 # connect to all displays for two reasons:
66 # 1: check if the display actually works
67 # 2: keep the connection open so that i3 is not the only client. this prevents
68 #    the X server from exiting (Xdummy will restart it, but not quick enough
69 #    sometimes)
70 my @conns;
71 my @wdisplays;
72 for my $display (@displays) {
73     my $screen;
74     my $x = X11::XCB->new($display, $screen);
75     if ($x->has_error) {
76         say STDERR "WARNING: Not using X11 display $display, could not connect";
77     } else {
78         push @conns, $x;
79         push @wdisplays, $display;
80     }
81 }
82
83 my $config = slurp('i3-test.config');
84
85 # 1: get a list of all testcases
86 my @testfiles = @ARGV;
87
88 # if no files were passed on command line, run all tests from t/
89 @testfiles = <t/*.t> if @testfiles == 0;
90
91 # 2: create an output directory for this test-run
92 my $outdir = "testsuite-";
93 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
94 $outdir .= `git describe --tags`;
95 chomp($outdir);
96 mkdir($outdir) or die "Could not create $outdir";
97 unlink("latest") if -e "latest";
98 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
99
100 # 3: run all tests
101 my @done;
102 my $num = @testfiles;
103 my $harness = TAP::Harness->new({ });
104
105 my $aggregator = TAP::Parser::Aggregator->new();
106 $aggregator->start();
107
108 # We start tests concurrently: For each display, one test gets started. Every
109 # test starts another test after completing.
110 take_job($_) for @wdisplays;
111
112 #
113 # Takes a test from the beginning of @testfiles and runs it.
114 #
115 # The TAP::Parser (which reads the test output) will get called as soon as
116 # there is some activity on the stdout file descriptor of the test process
117 # (using an AnyEvent->io watcher).
118 #
119 # When a test completes and @done contains $num entries, the $cv condvar gets
120 # triggered to finish testing.
121 #
122 sub take_job {
123     my ($display) = @_;
124
125     my $test = shift @testfiles;
126     return unless $test;
127     my $dont_start = (slurp($test) =~ /# !NO_I3_INSTANCE!/);
128     my $logpath = "$outdir/i3-log-for-" . basename($test);
129
130     my ($fh, $tmpfile) = tempfile('i3-run-cfg.XXXXXX', UNLINK => 1);
131     say $fh $config;
132     say $fh "ipc-socket /tmp/nested-$display";
133     close($fh);
134
135     my $activate_cv = AnyEvent->condvar;
136     my $time_before_start = [gettimeofday];
137
138     my $pid;
139     if (!$dont_start) {
140         $pid = activate_i3(
141             unix_socket_path => "/tmp/nested-$display-activation",
142             display => $display,
143             configfile => $tmpfile,
144             logpath => $logpath,
145             cv => $activate_cv
146         );
147
148         my $child_watcher;
149         $child_watcher = AnyEvent->child(pid => $pid, cb => sub {
150             say "child died. pid = $pid";
151             undef $child_watcher;
152         });
153     }
154
155     my $kill_i3 = sub {
156         # Don’t bother killing i3 when we haven’t started it
157         return if $dont_start;
158
159         # When measuring code coverage, try to exit i3 cleanly (otherwise, .gcda
160         # files are not written) and fallback to killing it
161         if ($coverage_testing) {
162             my $exited = 0;
163             eval {
164                 say "Exiting i3 cleanly...";
165                 i3("/tmp/nested-$display")->command('exit')->recv;
166                 $exited = 1;
167             };
168             return if $exited;
169         }
170
171         say "[$display] killing i3";
172         kill(9, $pid) or die "could not kill i3";
173     };
174
175     # This will be called as soon as i3 is running and answered to our
176     # IPC request
177     $activate_cv->cb(sub {
178         my $time_activating = [gettimeofday];
179         my $start_duration = tv_interval($time_before_start, $time_activating);
180         my ($status) = $activate_cv->recv;
181         if ($dont_start) {
182             say "[$display] Not starting i3, testcase does that";
183         } else {
184             say "[$display] i3 startup: took " . sprintf("%.2f", $start_duration) . "s, status = $status";
185         }
186
187         say "[$display] Running $test with logfile $logpath";
188
189         my $output;
190         open(my $spool, '>', \$output);
191         my $parser = TAP::Parser->new({
192             exec => [ 'sh', '-c', qq|DISPLAY=$display LOGPATH="$logpath" /usr/bin/perl -Ilib $test| ],
193             spool => $spool,
194             merge => 1,
195         });
196
197         my @watchers;
198         my ($stdout, $stderr) = $parser->get_select_handles;
199         for my $handle ($parser->get_select_handles) {
200             my $w;
201             $w = AnyEvent->io(
202                 fh => $handle,
203                 poll => 'r',
204                 cb => sub {
205                     # Ignore activity on stderr (unnecessary with merge => 1,
206                     # but let’s keep it in here if we want to use merge => 0
207                     # for some reason in the future).
208                     return if defined($stderr) and $handle == $stderr;
209
210                     my $result = $parser->next;
211                     if (defined($result)) {
212                         # TODO: check if we should bail out
213                         return;
214                     }
215
216                     # $result is not defined, we are done parsing
217                     say "[$display] $test finished";
218                     close($parser->delete_spool);
219                     $aggregator->add($test, $parser);
220                     push @done, [ $test, $output ];
221
222                     $kill_i3->();
223
224                     undef $_ for @watchers;
225                     if (@done == $num) {
226                         $cv->send;
227                     } else {
228                         take_job($display);
229                     }
230                 }
231             );
232             push @watchers, $w;
233         }
234     });
235
236     $activate_cv->send(1) if $dont_start;
237 }
238
239 $cv->recv;
240
241 $aggregator->stop();
242
243 for (@done) {
244     my ($test, $output) = @$_;
245     say "output for $test:";
246     say $output;
247 }
248
249 # 4: print summary
250 $harness->summary($aggregator);