82dee7de92a0bd35 failed to add ext/lib/Makefile.PL. Oops.
[p5sagit/p5-mst-13.2.git] / ext / Net-Ping / t / 250_ping_hires.t
1 # Test to make sure hires feature works.
2
3 BEGIN {
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
24 use Test qw(plan ok $TESTERR);
25 use Net::Ping;
26 plan tests => 8;
27
28 # Everything loaded fine
29 ok 1;
30
31 my $p = new Net::Ping "tcp";
32
33 # new() worked?
34 ok !!$p;
35
36 # Default is to not use Time::HiRes
37 ok !$Net::Ping::hires;
38
39 # Enable hires
40 $p -> hires();
41 ok $Net::Ping::hires;
42
43 # Make sure disable works
44 $p -> hires(0);
45 ok !$Net::Ping::hires;
46
47 # Enable again
48 $p -> hires(1);
49 ok $Net::Ping::hires;
50
51 # Test on the default port
52 my ($ret, $duration) = $p -> ping("localhost");
53
54 # localhost should always be reachable, right?
55 ok $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".
60 if (! ok($duration =~ /\.|^[01]$/)) {
61     print($TESTERR "# duration=[$duration]\n");
62 }