1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#! /usr/bin/perl
use strict;
use warnings;
use Fcntl qw/:flock/;
use File::Temp qw/tempfile/;
use Test::More tests => 5;
use_ok('Parallel::Prefork');
my $reaped = 0;
my $pm;
eval {
$pm = Parallel::Prefork->new({
max_workers => 10,
fork_delay => 0,
on_child_reap => sub {
$reaped++;
}
});
};
ok($pm);
my ($fh, $filename) = tempfile;
syswrite $fh, '0', 1;
close $fh;
my $ppid = $$;
my $c;
until ($pm->signal_received) {
$pm->start and next;
open $fh, '+<', $filename
or die "failed to open temporary file: $filename: ";
flock $fh, LOCK_EX;
sysread $fh, $c, 10;
$c++;
seek $fh, 0, 0;
syswrite $fh, $c, length($c);
flock $fh, LOCK_UN;
local $SIG{TERM} = sub {
flock $fh, LOCK_EX;
seek $fh, 0, 0;
sysread $fh, $c, 10;
$c++;
seek $fh, 0, 0;
syswrite $fh, $c, length($c);
flock $fh, LOCK_UN;
exit 0;
};
if ($c == $pm->max_workers) {
kill 'TERM', $ppid;
}
sleep 100;
$pm->finish;
}
ok(1);
$pm->wait_all_children;
open $fh, '<', $filename
or die "failed to open temporary file: $filename: ";
sysread $fh, $c, 10;
close $fh;
is($c, $pm->max_workers * 2);
is($reaped, $pm->max_workers, "properly called on_child_reap callback");
unlink $filename;
|