Best practice for unit tests that rely on internet access?

Peter Edwards peter at dragonstaff.co.uk
Wed May 4 12:51:01 BST 2011


On 4 May 2011 12:24, David Cantrell <david at cantrell.org.uk> wrote:

> On Tue, May 03, 2011 at 10:39:17PM +0100, Alexander Clouter wrote:
> > David Cantrell <david at cantrell.org.uk> wrote:
> > > You could always check whether outbound HTTP is allowed by connecting
> to
> > > somewhere entirely different.  Try penthouse or thepiratebay -
>

Argh. Something tells me that will do down badly!

Often you can put the connectivity tests in the developer tests in the xt/
directory and then they don't need to be run at install time.

Another approach is to wrap the tests that might hang in a block with a
timeout

    my @tests = glob "*.t";

    my $harness = TAP::Harness->new(
{color=>$nocolor?0:1,verbosity=>$verbose} );

    timeout( $TIMEOUT, sub {$harness->runtests(@tests)} );

sub timeout {
    my ($secs, $code) = @_;

    $SIG{ALRM} = sub { die "timeout after $secs sec(s)" };

    eval {

        alarm $secs;
        eval {
            &{$code};
        };
        alarm 0;

        warn $@ if $@;
    };

    alarm 0;

    delete $SIG{ALRM};
}



Regards, Peter
http://perl.dragonstaff.co.uk


More information about the london.pm mailing list