1b39c5158Smillert# Test to make sure hires feature works.
2b39c5158Smillert
3898184e3Ssthenuse strict;
4898184e3Ssthen
5b39c5158SmillertBEGIN {
6*f2a19305Safresh1  if ($ENV{NO_NETWORK_TESTING} ||
7*f2a19305Safresh1      ($ENV{PERL_CORE}) && !$ENV{PERL_TEST_Net_Ping}) {
8*f2a19305Safresh1    print "1..0 \# Skip: network dependent test\n";
9b39c5158Smillert    exit;
10b39c5158Smillert  }
11b39c5158Smillert  unless (eval "require Socket") {
12b39c5158Smillert    print "1..0 \# Skip: no Socket\n";
13b39c5158Smillert    exit;
14b39c5158Smillert  }
15b39c5158Smillert  unless (eval "require Time::HiRes") {
16b39c5158Smillert    print "1..0 \# Skip: no Time::HiRes\n";
17b39c5158Smillert    exit;
18b39c5158Smillert  }
19b39c5158Smillert  unless (getservbyname('echo', 'tcp')) {
20b39c5158Smillert    print "1..0 \# Skip: no echo port\n";
21b39c5158Smillert    exit;
22b39c5158Smillert  }
23de8cc8edSafresh1  unless (Socket::getaddrinfo('localhost', &Socket::AF_INET())) {
24de8cc8edSafresh1    print "1..0 \# Skip: no localhost resolver on $^O\n";
25de8cc8edSafresh1    exit;
26de8cc8edSafresh1  }
27b39c5158Smillert}
28b39c5158Smillert
29898184e3Ssthenuse Test::More tests => 8;
30898184e3SsthenBEGIN {use_ok('Net::Ping');}
31b39c5158Smillert
32b39c5158Smillertmy $p = new Net::Ping "tcp";
33b39c5158Smillert
34898184e3Ssthenisa_ok($p, 'Net::Ping', 'new() worked');
35b39c5158Smillert
3691f110e0Safresh1is($Net::Ping::hires, 1, 'Default is to use Time::HiRes');
37b39c5158Smillert
38b39c5158Smillert$p -> hires();
39898184e3Ssthenisnt($Net::Ping::hires, 0, 'Enabled hires');
40b39c5158Smillert
41b39c5158Smillert$p -> hires(0);
42898184e3Ssthenis($Net::Ping::hires, 0, 'Make sure disable works');
43b39c5158Smillert
44b39c5158Smillert$p -> hires(1);
45898184e3Ssthenisnt($Net::Ping::hires, 0, 'Enable hires again');
46b39c5158Smillert
47de8cc8edSafresh1SKIP: {
48de8cc8edSafresh1  skip "unreliable ping localhost on $^O", 2
49de8cc8edSafresh1    if $^O =~ /^(?:hpux|os390|irix|freebsd)$/;
50de8cc8edSafresh1
51b39c5158Smillert  # Test on the default port
52b39c5158Smillert  my ($ret, $duration) = $p -> ping("localhost");
53b39c5158Smillert
54898184e3Ssthen  isnt($ret, 0, 'localhost should always be reachable');
55b39c5158Smillert
56b39c5158Smillert  # It is extremely likely that the duration contains a decimal
57b39c5158Smillert  # point if Time::HiRes is functioning properly, except when it
58b39c5158Smillert  # is fast enough to be "0", or slow enough to be exactly "1".
59898184e3Ssthen  like($duration, qr/\.|^[01]$/, 'returned duration is valid');
60de8cc8edSafresh1}
61