Commit | Line | Data |
---|---|---|
b84cdbe2 SH |
1 | #!./perl |
2 | ||
3 | BEGIN { | |
4 | chdir 't' if -d 't'; | |
5 | @INC = '../lib'; | |
6 | require './test.pl'; | |
7 | } | |
8 | ||
9 | BEGIN { | |
10 | if ($^O eq 'riscos') { | |
11 | skip_all("kill() not implemented on this platform"); | |
12 | } | |
13 | } | |
14 | ||
15 | use strict; | |
16 | ||
8af710eb | 17 | plan tests => 6; |
b84cdbe2 SH |
18 | |
19 | ok( kill(0, $$), 'kill(0, $pid) returns true if $pid exists' ); | |
20 | ||
21 | # It's not easy to come up with an individual PID that is known not to exist, | |
22 | # so just check that at least some PIDs in a large range are reported not to | |
23 | # exist. | |
24 | my $count = 0; | |
25 | my $total = 30_000; | |
26 | for my $pid (1 .. $total) { | |
27 | ++$count if kill(0, $pid); | |
28 | } | |
29 | # It is highly unlikely that all of the above PIDs are genuinely in use, | |
30 | # so $count should be less than $total. | |
31 | ok( $count < $total, 'kill(0, $pid) returns false if $pid does not exist' ); | |
e2c0f81f DG |
32 | |
33 | # Verify that trying to kill a non-numeric PID is fatal | |
34 | my @bad_pids = ( | |
35 | [ undef , 'undef' ], | |
36 | [ '' , 'empty string' ], | |
37 | [ 'abcd', 'alphabetic' ], | |
38 | ); | |
39 | ||
40 | for my $case ( @bad_pids ) { | |
41 | my ($pid, $name) = @$case; | |
42 | eval { kill 0, $pid }; | |
43 | like( $@, qr/^Can't kill a non-numeric process ID/, "dies killing $name pid"); | |
44 | } | |
45 | ||
8af710eb TC |
46 | # Verify that killing a magic variable containing a number doesn't |
47 | # trigger the above | |
48 | { | |
49 | my $x = $$ . " "; | |
50 | $x =~ /(\d+)/; | |
51 | ok(eval { kill 0, $1 }, "can kill a number string in a magic variable"); | |
52 | } |