mirror of
https://git.proxmox.com/git/pve-common
synced 2025-04-28 12:10:17 +00:00
test: have lock file test use run_fork() helper
There are differences for error handling in the old and new helper, in particular when the child is signaled. But the test here does not explicitly check for specific error conditions or messages and is therefore still correct with the new helper. Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
This commit is contained in:
parent
fb96ee89f2
commit
903d285751
@ -11,50 +11,6 @@ use PVE::Tools 'lock_file_full';
|
||||
|
||||
my $name = "test.lockfile.$$-";
|
||||
|
||||
# Utilities:
|
||||
|
||||
sub forked($$) {
|
||||
my ($code1, $code2) = @_;
|
||||
|
||||
pipe(my $except_r, my $except_w) or die "pipe: $!\n";
|
||||
|
||||
my $pid = fork();
|
||||
die "fork failed: $!\n" if !defined($pid);
|
||||
|
||||
if ($pid == 0) {
|
||||
close($except_r);
|
||||
eval { $code1->() };
|
||||
if ($@) {
|
||||
print {$except_w} $@;
|
||||
$except_w->flush();
|
||||
POSIX::_exit(1);
|
||||
}
|
||||
POSIX::_exit(0);
|
||||
}
|
||||
close($except_w);
|
||||
|
||||
eval { $code2->() };
|
||||
my $err = $@;
|
||||
if ($err) {
|
||||
kill(15, $pid);
|
||||
} else {
|
||||
my $err = do { local $/ = undef; <$except_r> };
|
||||
}
|
||||
die "interrupted\n" if waitpid($pid, 0) != $pid;
|
||||
die $err if $err;
|
||||
|
||||
# Check exit code:
|
||||
my $status = POSIX::WEXITSTATUS($?);
|
||||
if ($? == -1) {
|
||||
die "failed to execute\n";
|
||||
} elsif (POSIX::WIFSIGNALED($?)) {
|
||||
my $sig = POSIX::WTERMSIG($?);
|
||||
die "got signal $sig\n";
|
||||
} elsif ($status != 0) {
|
||||
die "exit code $status\n";
|
||||
}
|
||||
}
|
||||
|
||||
# Book-keeping:
|
||||
|
||||
my %_ran;
|
||||
@ -82,7 +38,7 @@ sub forktest1($) {
|
||||
# socket pair for synchronization
|
||||
socketpair(my $fmain, my $fother, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
|
||||
or die "socketpair(): $!\n";
|
||||
forked sub {
|
||||
my $other = sub {
|
||||
# other side
|
||||
close($fmain);
|
||||
my $line;
|
||||
@ -97,7 +53,9 @@ sub forktest1($) {
|
||||
die $@ if $@;
|
||||
die "parent failed\n" if !$line || $line ne "2\n";
|
||||
assert('other side');
|
||||
}, sub {
|
||||
return;
|
||||
};
|
||||
my $main = sub {
|
||||
# main process
|
||||
# Wait for our child to lock:
|
||||
close($fother);
|
||||
@ -114,6 +72,8 @@ sub forktest1($) {
|
||||
print {$fmain} "2\n";
|
||||
$fmain->flush();
|
||||
};
|
||||
|
||||
PVE::Tools::run_fork($other, { afterfork => $main });
|
||||
close($fmain);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user