1# Test to make sure hires feature works.
2
3BEGIN {
4  if ($ENV{PERL_CORE}) {
5    unless ($ENV{PERL_TEST_Net_Ping}) {
6      print "1..0 # Skip: network dependent test\n";
7        exit;
8    }
9  }
10  unless (eval "require Socket") {
11    print "1..0 \# Skip: no Socket\n";
12    exit;
13  }
14  unless (eval "require Time::HiRes") {
15    print "1..0 \# Skip: no Time::HiRes\n";
16    exit;
17  }
18  unless (getservbyname('echo', 'tcp')) {
19    print "1..0 \# Skip: no echo port\n";
20    exit;
21  }
22}
23
24use Test qw(plan ok $TESTERR);
25use Net::Ping;
26plan tests => 8;
27
28# Everything loaded fine
29ok 1;
30
31my $p = new Net::Ping "tcp";
32
33# new() worked?
34ok !!$p;
35
36# Default is to not use Time::HiRes
37ok !$Net::Ping::hires;
38
39# Enable hires
40$p -> hires();
41ok $Net::Ping::hires;
42
43# Make sure disable works
44$p -> hires(0);
45ok !$Net::Ping::hires;
46
47# Enable again
48$p -> hires(1);
49ok $Net::Ping::hires;
50
51# Test on the default port
52my ($ret, $duration) = $p -> ping("localhost");
53
54# localhost should always be reachable, right?
55ok $ret;
56
57# It is extremely likely that the duration contains a decimal
58# point if Time::HiRes is functioning properly, except when it
59# is fast enough to be "0", or slow enough to be exactly "1".
60if (! ok($duration =~ /\.|^[01]$/)) {
61    print($TESTERR "# duration=[$duration]\n");
62}
63