| 1 |
use Test2::V0 -target => 'Test2::Harness::Stall::Capture'; |
| 2 |
# HARNESS-DURATION-SHORT |
| 3 |
|
| 4 |
use ok $CLASS; |
| 5 |
|
| 6 |
use File::Temp qw/tempdir/; |
| 7 |
|
| 8 |
sub capture { |
| 9 |
return $CLASS->new(workdir => tempdir(CLEANUP => 1)); |
| 10 |
} |
| 11 |
|
| 12 |
subtest catches_usr1 => sub { |
| 13 |
my $one = capture(); |
| 14 |
|
| 15 |
# Pin the signal number: it is 10 here but 30 on the BSDs and Darwin and |
| 16 |
# 16 on Solaris, and this subtest is about parsing the mask, not about |
| 17 |
# what the local number happens to be. |
| 18 |
my $mock = mock $CLASS => (override => [usr1_number => sub { 10 }]); |
| 19 |
|
| 20 |
# SigCgt is a hex mask of caught signals; with SIGUSR1 at 10 that is bit 9. |
| 21 |
ok($one->catches_usr1({sigcgt => '0000000000000200'}), "usr1 caught"); |
| 22 |
ok($one->catches_usr1({sigcgt => '0000000180014a03'}), "usr1 among others"); |
| 23 |
ok(!$one->catches_usr1({sigcgt => '0000000000000100'}), "a neighbouring signal is not usr1"); |
| 24 |
ok(!$one->catches_usr1({sigcgt => '0000000000000000'}), "nothing caught"); |
| 25 |
ok(!$one->catches_usr1({}), "no field at all"); |
| 26 |
ok(!$one->catches_usr1({sigcgt => 'not-hex'}), "unparseable"); |
| 27 |
}; |
| 28 |
|
| 29 |
# SIGUSR1's default action is to terminate. A test job sheds the handler when |
| 30 |
# the runner restores the original %SIG, so signalling one kills a running |
| 31 |
# test; and a pid recorded earlier may have exited and been recycled. |
| 32 |
subtest signal_pids_refuses_anything_that_did_not_install_the_handler => sub { |
| 33 |
my $one = capture(); |
| 34 |
|
| 35 |
my @killed; |
| 36 |
|
| 37 |
# have_proc must be forced: without it this subtest signals everything on |
| 38 |
# any platform that has no /proc, and this file ships to CPAN smokers. |
| 39 |
my $mock = mock $CLASS => ( |
| 40 |
override => [ |
| 41 |
have_proc => sub { 1 }, |
| 42 |
usr1_number => sub { 10 }, |
| 43 |
kill_pid => sub { push @killed => $_[1]; 1 }, |
| 44 |
], |
| 45 |
); |
| 46 |
|
| 47 |
my $procs = { |
| 48 |
100 => {pid => 100, sigcgt => '0000000000000200'}, # ours |
| 49 |
200 => {pid => 200, sigcgt => '0000000000000000'}, # shed it |
| 50 |
300 => {pid => 300, gone => 1}, # exited |
| 51 |
400 => {pid => 400, sigcgt => '0000000000000200', gone => 1}, # exited |
| 52 |
}; |
| 53 |
|
| 54 |
my $sent = $one->signal_pids([100, 200, 300, 400, 500], $procs); |
| 55 |
|
| 56 |
is($sent, [100], "only the process catching SIGUSR1"); |
| 57 |
is(\@killed, [100], "and only that one was signalled"); |
| 58 |
}; |
| 59 |
|
| 60 |
subtest signal_pids_never_signals_a_process_group => sub { |
| 61 |
my $one = capture(); |
| 62 |
|
| 63 |
my @killed; |
| 64 |
my $mock = mock $CLASS => ( |
| 65 |
override => [ |
| 66 |
have_proc => sub { 1 }, |
| 67 |
kill_pid => sub { push @killed => $_[1]; 1 }, |
| 68 |
], |
| 69 |
); |
| 70 |
|
| 71 |
# A negative pid means a process group to kill(), which must never happen. |
| 72 |
my $sent = $one->signal_pids([-1, -100, 0], {-100 => {sigcgt => '0000000000000200'}}); |
| 73 |
|
| 74 |
is($sent, [], "nothing signalled"); |
| 75 |
is(\@killed, [], "kill was never called"); |
| 76 |
}; |
| 77 |
|
| 78 |
subtest process_tree_parses_a_comm_containing_spaces_and_parens => sub { |
| 79 |
my $one = capture(); |
| 80 |
|
| 81 |
# /proc/PID/stat puts comm in parens and it may contain anything, so ppid |
| 82 |
# has to be read after the last ')'. |
| 83 |
my $mock = mock $CLASS => ( |
| 84 |
override => [ |
| 85 |
have_proc => sub { 1 }, |
| 86 |
all_pids => sub { [10, 11, 12] }, |
| 87 |
read_proc => sub { |
| 88 |
my ($self, $pid, $what) = @_; |
| 89 |
return unless $what eq 'stat'; |
| 90 |
return "10 (yath) S 1 10 10 0 -1 0" if $pid == 10; |
| 91 |
return "11 (weird (name) here) S 10 1 1" if $pid == 11; |
| 92 |
return "12 (other) S 99 1 1" if $pid == 12; |
| 93 |
return; |
| 94 |
}, |
| 95 |
], |
| 96 |
); |
| 97 |
|
| 98 |
is($one->process_tree(10), [10, 11], "found the child despite the parens in its name"); |
| 99 |
}; |
| 100 |
|
| 101 |
done_testing; |