]> git.sur5r.net Git - i3/i3/blob - testcases/complete-run.pl
tests: Refactor the socket activation into lib/SocketActivation.pm
[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 EV;
31 use AnyEvent;
32 use AnyEvent::Handle;
33 use AnyEvent::I3 qw(:all);
34 use IO::Scalar; # not in core :\
35 use Try::Tiny; # not in core
36 use X11::XCB;
37
38 # install a dummy CHLD handler to overwrite the CHLD handler of AnyEvent / EV
39 # XXX: we could maybe also use a different loop than the default loop in EV?
40 $SIG{CHLD} = sub {
41 };
42
43 # reads in a whole file
44 sub slurp {
45     open my $fh, '<', shift;
46     local $/;
47     <$fh>;
48 }
49
50 my $coverage_testing = 0;
51 my @displays = ();
52
53 my $result = GetOptions(
54     "coverage-testing" => \$coverage_testing,
55     "display=s" => \@displays,
56 );
57
58 @displays = split(/,/, join(',', @displays));
59 @displays = map { s/ //g; $_ } @displays;
60
61 @displays = qw(:1) if @displays == 0;
62
63 # connect to all displays for two reasons:
64 # 1: check if the display actually works
65 # 2: keep the connection open so that i3 is not the only client. this prevents
66 #    the X server from exiting (Xdummy will restart it, but not quick enough
67 #    sometimes)
68 my @conns;
69 my @wdisplays;
70 for my $display (@displays) {
71     my $screen;
72     my $x = X11::XCB->new($display, $screen);
73     if ($x->has_error) {
74         say STDERR "WARNING: Not using X11 display $display, could not connect";
75     } else {
76         push @conns, $x;
77         push @wdisplays, $display;
78     }
79 }
80
81 my $config = slurp('i3-test.config');
82
83 # 1: get a list of all testcases
84 my @testfiles = @ARGV;
85
86 # if no files were passed on command line, run all tests from t/
87 @testfiles = <t/*.t> if @testfiles == 0;
88
89 # 2: create an output directory for this test-run
90 my $outdir = "testsuite-";
91 $outdir .= POSIX::strftime("%Y-%m-%d-%H-%M-%S-", localtime());
92 $outdir .= `git describe --tags`;
93 chomp($outdir);
94 mkdir($outdir) or die "Could not create $outdir";
95 unlink("latest") if -e "latest";
96 symlink("$outdir", "latest") or die "Could not symlink latest to $outdir";
97
98 # 3: run all tests
99 my @done;
100 my $num = @testfiles;
101 my $harness = TAP::Harness->new({ });
102
103 my $aggregator = TAP::Parser::Aggregator->new();
104 $aggregator->start();
105
106 my $cv = AnyEvent->condvar;
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             try {
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         my $parser = TAP::Parser->new({
191             exec => [ 'sh', '-c', qq|DISPLAY=$display LOGPATH="$logpath" /usr/bin/perl -It/lib $test| ],
192             spool => IO::Scalar->new(\$output),
193             merge => 1,
194         });
195
196         my @watchers;
197         my ($stdout, $stderr) = $parser->get_select_handles;
198         for my $handle ($parser->get_select_handles) {
199             my $w;
200             $w = AnyEvent->io(
201                 fh => $handle,
202                 poll => 'r',
203                 cb => sub {
204                     # Ignore activity on stderr (unnecessary with merge => 1,
205                     # but let’s keep it in here if we want to use merge => 0
206                     # for some reason in the future).
207                     return if defined($stderr) and $handle == $stderr;
208
209                     my $result = $parser->next;
210                     if (defined($result)) {
211                         # TODO: check if we should bail out
212                         return;
213                     }
214
215                     # $result is not defined, we are done parsing
216                     say "[$display] $test finished";
217                     close($parser->delete_spool);
218                     $aggregator->add($test, $parser);
219                     push @done, [ $test, $output ];
220
221                     $kill_i3->();
222
223                     undef $_ for @watchers;
224                     if (@done == $num) {
225                         $cv->send;
226                     } else {
227                         take_job($display);
228                     }
229                 }
230             );
231             push @watchers, $w;
232         }
233     });
234
235     $activate_cv->send(1) if $dont_start;
236 }
237
238 $cv->recv;
239
240 $aggregator->stop();
241
242 for (@done) {
243     my ($test, $output) = @$_;
244     say "output for $test:";
245     say $output;
246 }
247
248 # 4: print summary
249 $harness->summary($aggregator);