File: 190_alarm.t

package info (click to toggle)
perl 5.8.8-7etch6
  • links: PTS
  • area: main
  • in suites: etch
  • size: 60,396 kB
  • ctags: 33,629
  • sloc: perl: 199,713; ansic: 160,511; sh: 33,095; pascal: 8,270; lisp: 6,121; makefile: 2,373; cpp: 2,035; yacc: 1,047; java: 23
file content (54 lines) | stat: -rwxr-xr-x 1,185 bytes parent folder | download | duplicates (3)
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
# Test to make sure alarm / SIGALM does not interfere
# with Net::Ping.  (This test was derived to ensure
# compatibility with the "spamassassin" utility.)
# Based on code written by radu@netsoft.ro (Radu Greab).

BEGIN {
  unless (eval "require Socket") {
    print "1..0 \# Skip: no Socket\n";
    exit;
  }
  unless (eval {alarm 0; 1;}) {
    print "1..0 \# Skip: alarm borks on $^O $^X $] ?\n";
    exit;
  }
  unless (getservbyname('echo', 'tcp')) {
    print "1..0 \# Skip: no echo port\n";
    exit;
  }
}

use strict;
use Test;
use Net::Ping;

plan tests => 6;

# Everything compiled
ok 1;

eval {
  my $timeout = 11;

  ok 1; # In eval
  local $SIG{ALRM} = sub { die "alarm works" };
  ok 1; # SIGALRM can be set on this platform
  alarm $timeout;
  ok 1; # alarm() can be set on this platform

  my $start = time;
  while (1) {
    my $ping = Net::Ping->new("tcp", 2);
    # It does not matter if alive or not
    $ping->ping("127.0.0.1");
    $ping->ping("172.29.249.249");
    die "alarm failed" if time > $start + $timeout + 1;
  }
};
# Got out of "infinite loop" okay
ok 1;

# Make sure it died for a good excuse
ok $@ =~ /alarm works/ or die $@;

alarm 0; # Reset alarm