Skip to content

Commit

Permalink
- Acted upon the issue RT #102095.
Browse files Browse the repository at this point in the history
- Refactored the method connect_ok() to be more explicit.
- Exported method connect_ok() by default.
- Changed the default timeout to 2 seconds.
- Added unit test for the method connect_ok().
  • Loading branch information
manwar committed Feb 13, 2015
1 parent 169a971 commit 2c85a79
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 deletions.
7 changes: 7 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
Revision history for Perl extension Test::Internet.

0.04 Fri Feb 13 15:30:00 2015
- Acted upon the issue RT #102095.
- Refactored the method connect_ok() to be more explicit.
- Exported method connect_ok() by default.
- Changed the default timeout to 2 seconds.
- Added unit test for the method connect_ok().

0.03 Thu Feb 12 22:30:00 2015
- Added description to the method connect_ok().
- Tidied up the example code in the pod document and added link
Expand Down
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ t/pod.t
t/manifest.t
t/meta-json.t
t/meta-yml.t
t/connect_ok.t
2 changes: 1 addition & 1 deletion Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ WriteMakefile(
clean => { FILES => 'Test-Internet-*' },
(eval { ExtUtils::MakeMaker->VERSION(6.46) } ? (META_MERGE => {
'meta-spec' => { version => 2 },
provides => { 'Test::Internet' => { file => 'lib/Test/Internet.pm', version => '0.03' } },
provides => { 'Test::Internet' => { file => 'lib/Test/Internet.pm', version => '0.04' } },
resources => {
repository => {
type => 'git',
Expand Down
48 changes: 42 additions & 6 deletions lib/Test/Internet.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package Test::Internet;

$Test::Internet::VERSION = '0.03';
$Test::Internet::VERSION = '0.04';
$Test::Internet::AUTHORITY = 'cpan:MANWAR';

=head1 NAME
Expand All @@ -9,13 +9,14 @@ Test::Internet - Interface to test internet connection.
=head1 VERSION
Version 0.03
Version 0.04
=cut

use strict; use warnings;

use 5.006;
use Socket;
use Net::DNS;
use Data::Dumper;
use Test::Builder ();
Expand All @@ -24,7 +25,7 @@ require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(connect_ok);

our $DEFAULT_TIMEOUT = 5;
our $DEFAULT_TIMEOUT = 2;

=head1 DESCRIPTION
Expand All @@ -37,11 +38,12 @@ code can be found L<here|https://raw.githubusercontent.com/Manwar/WWW-Google-Pla
=head2 connect_ok($timeout)
Return true/false depending on whether there is an active internet connection.The
default timeout is 5 seconds unless the user pass the timeout period.
default timeout is 2 seconds unless the user pass the timeout period. It gets
exported by default.
use strict; use warnings;
use Test::More;
use Test::Internet qw(connect_ok);
use Test::Internet;
plan skip_all => "No internet connection." unless connect_ok();
ok(connect_ok());
Expand All @@ -52,14 +54,40 @@ default timeout is 5 seconds unless the user pass the timeout period.
sub connect_ok {
my ($timeout) = @_;

my @nameservers = grep { inet_aton("$_.root-servers.net") } ('a'..'j');
return 0 unless (scalar(@nameservers));

$timeout = $DEFAULT_TIMEOUT unless defined $timeout;
my $resolver = Net::DNS::Resolver->new;
$resolver->tcp_timeout($timeout);
$resolver->udp_timeout($timeout);
$resolver->nameservers(map { "$_.root-servers.net" } @nameservers);

my $response = $resolver->query("root-servers.net", "NS");
if (defined $response) {
return 1 if (grep { $_->type eq 'NS' } $response->answer);
}

return defined $resolver->query("root-servers.net", "NS");
return 0;
}

=head1 Why L<Test::Internet>?
Karen Etheridge raised this question as in RT# 102095 and introduced me to a very
similar module L<Test::RequiresInternet> on CPAN.What a shame that it didn't turn
up in a search on CPAN, while I was looking for any module with the word Internet.
I am not an expert on how the CPAN search engine works, though. Had I known about
it, I wouldn't have bothered creating L<Test::Internet> to be honest.
The nice thing about the L<Test::RequiresInternet> is that it does not need any
external module and just uses what is available in core perl i.e. Socket. However
it relies on a webservice to exist and respond, so if that webservice is down the
module will give a false negative.
So if the requirement is to check if there is an active internet connection only
then I would recommend L<Test::Internet>. In case you want to check if you can
reach a particular given host as well then go for L<Test::RequiresInternet>.
=head1 AUTHOR
Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>
Expand All @@ -68,6 +96,14 @@ Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>
L<https://github.com/Manwar/Test-Internet>
=head1 ACKNOWLEDGEMENT
David Kitcher-Jones (m4ddav3) for his immensely valuable inputs.
=head1 SEE ALSO
L<Test::RequiresInternet>
=head1 BUGS
Please report any bugs or feature requests to C<bug-test-internet at rt.cpan.org>
Expand Down
12 changes: 12 additions & 0 deletions t/connect_ok.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/perl

use 5.006;
use strict; use warnings;
use Test::More;
use Test::Internet;

plan skip_all => "No internet connection." unless connect_ok();

ok(connect_ok());

done_testing();

0 comments on commit 2c85a79

Please sign in to comment.