Skip to content

Commit 44a6efb

Browse files
authored
tests: run under Xvfb by default (if available) (i3#2951)
This shaves off two seconds of wall-clock time (10s → 8s).
1 parent 28ca1e8 commit 44a6efb

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

docs/testsuite

+2-4
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,8 @@ containing the appropriate i3 logfile for each testcase. The latest folder can
113113
always be found under the symlink +latest/+. Unless told differently, it will
114114
run the tests on a separate X server instance (using Xephyr).
115115

116-
Xephyr will open a window where you can inspect the running test. You can run
117-
the tests without an X session with Xvfb, such as with +xvfb-run
118-
./complete-run+. This will also speed up the tests significantly especially on
119-
machines without a powerful video card.
116+
Xephyr will open a window where you can inspect the running test. By default,
117+
tests are run under Xvfb.
120118

121119
.Example invocation of +complete-run.pl+
122120
---------------------------------------

testcases/complete-run.pl.in

+43-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ binmode STDERR, ':utf8';
3838
# subshell or situations like that.
3939
AnyEvent::Util::close_all_fds_except(0, 1, 2);
4040

41+
our @CLEANUP;
42+
4143
# convenience wrapper to write to the log file
4244
my $log;
4345
sub Log { say $log "@_" }
@@ -55,6 +57,7 @@ my %options = (
5557
xtrace => 0,
5658
coverage => 0,
5759
restart => 0,
60+
xvfb => 1,
5861
);
5962
my $keep_xserver_output = 0;
6063

@@ -64,6 +67,7 @@ my $result = GetOptions(
6467
"valgrind" => \$options{valgrind},
6568
"strace" => \$options{strace},
6669
"xtrace" => \$options{xtrace},
70+
"xvfb" => \$options{xvfb},
6771
"display=s" => \@displays,
6872
"parallel=i" => \$parallel,
6973
"help|?" => \$help,
@@ -112,6 +116,44 @@ $ENV{PATH} = join(':',
112116
qx(Xephyr -help 2>&1);
113117
die "Xephyr was not found in your path. Please install Xephyr (xserver-xephyr on Debian)." if $?;
114118

119+
qx(xvfb-run --help 2>&1);
120+
if ($? && $options{xvfb}) {
121+
say "xvfb-run not found, not running tests under xvfb. Install the xvfb package to speed up tests";
122+
$options{xvfb} = 0;
123+
}
124+
125+
if ($options{xvfb}) {
126+
for (my $n = 99; $n < 120; $n++) {
127+
my $path = File::Temp::tmpnam($ENV{TMPDIR} // "/tmp", "i3-testsXXXXXX");
128+
if (!defined(POSIX::mkfifo($path, 0600))) {
129+
die "mkfifo: $!";
130+
}
131+
my $pid = fork // die "fork: $!";
132+
if ($pid == 0) {
133+
# Child
134+
135+
# Xvfb checks whether the parent ignores USR1 and sends USR1 to the
136+
# parent when ready, so that the wait call will be interrupted. We
137+
# can’t implement this in Perl, as Perl’s waitpid transparently
138+
# handles -EINTR.
139+
exec('/bin/sh', '-c', qq|trap "exit" INT; trap : USR1; (trap '' USR1; exec Xvfb :$n -screen 0 640x480x8 -nolisten tcp) & PID=\$!; wait; if ! kill -0 \$PID 2>/dev/null; then echo 1:\$PID > $path; else echo 0:\$PID > $path; wait \$PID; fi|);
140+
die "exec: $!";
141+
}
142+
chomp(my $kill = slurp($path));
143+
unlink($path);
144+
my ($code, $xvfbpid) = ($kill =~ m,^([0-1]):(.*)$,);
145+
next unless $code eq '0';
146+
147+
$ENV{DISPLAY} = ":$n";
148+
say "Running tests under Xvfb display $ENV{DISPLAY}";
149+
150+
push(@CLEANUP, sub {
151+
kill(15, $xvfbpid);
152+
});
153+
last;
154+
}
155+
}
156+
115157
@displays = split(/,/, join(',', @displays));
116158
@displays = map { s/ //g; $_ } @displays;
117159

@@ -379,7 +421,7 @@ sub take_job {
379421

380422
sub cleanup {
381423
my $exitcode = $?;
382-
$_->() for our @CLEANUP;
424+
$_->() for @CLEANUP;
383425
exit $exitcode;
384426
}
385427

travis/run-tests.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fi
2626

2727
# Try running the tests in parallel so that the common case (tests pass) is
2828
# quick, but fall back to running them in sequence to make debugging easier.
29-
if ! xvfb-run make check
29+
if ! make check
3030
then
31-
xvfb-run ./testcases/complete-run.pl --parallel=1 || (cat latest/complete-run.log; false)
31+
./testcases/complete-run.pl --parallel=1 || (cat latest/complete-run.log; false)
3232
fi

0 commit comments

Comments
 (0)