1# Test to make sure alarm / SIGALM does not interfere
2# with Net::Ping.  (This test was derived to ensure
3# compatibility with the "spamassassin" utility.)
4# Based on code written by radu@netsoft.ro (Radu Greab).
5
6BEGIN {
7  unless (eval "require Socket") {
8    print "1..0 \# Skip: no Socket\n";
9    exit;
10  }
11  unless (eval {alarm 0; 1;}) {
12    print "1..0 \# Skip: alarm borks on $^O $^X $] ?\n";
13    exit;
14  }
15  unless (getservbyname('echo', 'tcp')) {
16    print "1..0 \# Skip: no echo port\n";
17    exit;
18  }
19}
20
21use strict;
22use Test;
23use Net::Ping;
24
25plan tests => 6;
26
27# Everything compiled
28ok 1;
29
30eval {
31  my $timeout = 11;
32
33  ok 1; # In eval
34  local $SIG{ALRM} = sub { die "alarm works" };
35  ok 1; # SIGALRM can be set on this platform
36  alarm $timeout;
37  ok 1; # alarm() can be set on this platform
38
39  my $start = time;
40  while (1) {
41    my $ping = Net::Ping->new("tcp", 2);
42    # It does not matter if alive or not
43    $ping->ping("127.0.0.1");
44    $ping->ping("172.29.249.249");
45    die "alarm failed" if time > $start + $timeout + 1;
46  }
47};
48# Got out of "infinite loop" okay
49ok 1;
50
51# Make sure it died for a good excuse
52ok $@ =~ /alarm works/ or die $@;
53
54alarm 0; # Reset alarm
55