2011-11-29 13:48:04 +01:00
|
|
|
|
# vim:ts=4:sw=4:sts=4:expandtab
|
|
|
|
|
package TestWorker;
|
|
|
|
|
use strict; use warnings;
|
|
|
|
|
use v5.10;
|
|
|
|
|
|
|
|
|
|
use Socket qw(AF_UNIX SOCK_DGRAM PF_UNSPEC);
|
|
|
|
|
use IO::Handle; # for ->autoflush
|
|
|
|
|
|
|
|
|
|
use POSIX ();
|
|
|
|
|
|
2012-08-12 00:22:17 +02:00
|
|
|
|
use Errno qw(EAGAIN);
|
|
|
|
|
|
2011-11-29 13:48:04 +01:00
|
|
|
|
use Exporter 'import';
|
|
|
|
|
our @EXPORT = qw(worker worker_next);
|
|
|
|
|
|
|
|
|
|
use File::Basename qw(basename);
|
|
|
|
|
my @x;
|
2012-01-27 22:36:40 +01:00
|
|
|
|
my $options;
|
2011-11-29 13:48:04 +01:00
|
|
|
|
|
|
|
|
|
sub worker {
|
2012-01-27 22:36:40 +01:00
|
|
|
|
my ($display, $x, $outdir, $optref) = @_;
|
2011-11-29 13:48:04 +01:00
|
|
|
|
|
|
|
|
|
# make sure $x hangs around
|
|
|
|
|
push @x, $x;
|
|
|
|
|
|
2012-01-27 22:36:40 +01:00
|
|
|
|
# store the options hashref
|
|
|
|
|
$options = $optref;
|
|
|
|
|
|
2011-11-29 13:48:04 +01:00
|
|
|
|
socketpair(my $ipc_child, my $ipc, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)
|
|
|
|
|
or die "socketpair: $!";
|
|
|
|
|
|
|
|
|
|
$ipc->autoflush(1);
|
|
|
|
|
$ipc_child->autoflush(1);
|
|
|
|
|
|
|
|
|
|
my $worker = {
|
|
|
|
|
display => $display,
|
|
|
|
|
ipc => $ipc,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
my $pid = fork // die "could not fork: $!";
|
|
|
|
|
|
|
|
|
|
if ($pid == 0) {
|
|
|
|
|
close $ipc;
|
|
|
|
|
undef @complete_run::CLEANUP;
|
|
|
|
|
# reap dead test children
|
|
|
|
|
$SIG{CHLD} = sub { waitpid -1, POSIX::WNOHANG };
|
|
|
|
|
|
|
|
|
|
$worker->{ipc} = $ipc_child;
|
|
|
|
|
|
2017-09-14 11:30:58 +02:00
|
|
|
|
# Preload the i3test module: reduces user CPU from 25s to 18s
|
2011-11-29 13:48:04 +01:00
|
|
|
|
require i3test;
|
|
|
|
|
|
|
|
|
|
worker_wait($worker, $outdir);
|
|
|
|
|
exit 23;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close $ipc_child;
|
|
|
|
|
push @complete_run::CLEANUP, sub {
|
|
|
|
|
# signal via empty line to exit itself
|
|
|
|
|
syswrite($ipc, "\n") or kill('TERM', $pid);
|
|
|
|
|
waitpid $pid, 0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return $worker;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
our $EOF = "# end of file\n";
|
|
|
|
|
sub worker_wait {
|
|
|
|
|
my ($self, $outdir) = @_;
|
|
|
|
|
|
|
|
|
|
my $ipc = $self->{ipc};
|
|
|
|
|
my $ipc_fd = fileno($ipc);
|
|
|
|
|
|
2012-08-12 00:22:17 +02:00
|
|
|
|
while (1) {
|
|
|
|
|
my $file = $ipc->getline;
|
|
|
|
|
if (!defined($file)) {
|
|
|
|
|
next if $! == EAGAIN;
|
|
|
|
|
last;
|
|
|
|
|
}
|
2011-11-29 13:48:04 +01:00
|
|
|
|
chomp $file;
|
|
|
|
|
|
|
|
|
|
exit unless $file;
|
|
|
|
|
|
2017-09-14 11:30:58 +02:00
|
|
|
|
die "tried to launch nonexistent testfile $file: $!\n"
|
2011-11-29 13:48:04 +01:00
|
|
|
|
unless -e $file;
|
|
|
|
|
|
|
|
|
|
# start a new and self contained process:
|
2017-09-14 11:30:58 +02:00
|
|
|
|
# whatever happens in the testfile should *NOT* affect us.
|
2011-11-29 13:48:04 +01:00
|
|
|
|
|
|
|
|
|
my $pid = fork // die "could not fork: $!";
|
|
|
|
|
if ($pid == 0) {
|
|
|
|
|
undef @complete_run::CLEANUP;
|
|
|
|
|
local $SIG{CHLD};
|
|
|
|
|
|
|
|
|
|
$0 = $file;
|
|
|
|
|
|
2017-09-10 19:41:49 +02:00
|
|
|
|
# Re-seed rand() so that File::Temp’s tempnam produces different
|
|
|
|
|
# results, making a TOCTOU between e.g. t/175-startup-notification.t
|
|
|
|
|
# and t/180-fd-leaks.t less likely.
|
|
|
|
|
srand(time ^ $$);
|
|
|
|
|
|
2011-11-29 13:48:04 +01:00
|
|
|
|
POSIX::dup2($ipc_fd, 0);
|
|
|
|
|
POSIX::dup2($ipc_fd, 1);
|
|
|
|
|
POSIX::dup2(1, 2);
|
|
|
|
|
|
|
|
|
|
# get Test::Builder singleton
|
|
|
|
|
my $test = Test::Builder->new;
|
|
|
|
|
|
|
|
|
|
# Test::Builder dups stdout/stderr while loading.
|
|
|
|
|
# we need to reset them here to point to $ipc
|
|
|
|
|
$test->output(\*STDOUT);
|
|
|
|
|
$test->failure_output(\*STDERR);
|
|
|
|
|
$test->todo_output(\*STDOUT);
|
|
|
|
|
|
2016-09-24 23:48:33 +02:00
|
|
|
|
@ENV{qw(HOME DISPLAY TESTNAME OUTDIR VALGRIND STRACE XTRACE COVERAGE RESTART)}
|
|
|
|
|
= ($outdir,
|
|
|
|
|
$self->{display},
|
2012-01-27 22:36:40 +01:00
|
|
|
|
basename($file),
|
|
|
|
|
$outdir,
|
|
|
|
|
$options->{valgrind},
|
|
|
|
|
$options->{strace},
|
2012-08-04 00:51:41 +02:00
|
|
|
|
$options->{xtrace},
|
2012-01-27 22:36:40 +01:00
|
|
|
|
$options->{coverage},
|
|
|
|
|
$options->{restart});
|
2011-11-29 13:48:04 +01:00
|
|
|
|
|
|
|
|
|
package main;
|
|
|
|
|
local $@;
|
2016-10-10 21:14:59 +02:00
|
|
|
|
do $file;
|
2011-11-29 13:48:04 +01:00
|
|
|
|
$test->ok(undef, "$@") if $@;
|
|
|
|
|
|
|
|
|
|
# XXX hack, we need to trigger the read watcher once more
|
|
|
|
|
# to signal eof to TAP::Parser
|
|
|
|
|
print $EOF;
|
|
|
|
|
|
|
|
|
|
exit 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub worker_next {
|
|
|
|
|
my ($self, $file) = @_;
|
|
|
|
|
|
|
|
|
|
my $ipc = $self->{ipc};
|
|
|
|
|
syswrite $ipc, "$file\n" or die "syswrite: $!";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__PACKAGE__ __END__
|