X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Flib%2FExternalCatty.pm;h=27bce2e80b5a2a94a94fb12971bf470812f18d83;hb=71896b6e2ad0faf686bfab695a70b89ef15aac4f;hp=e16cf6053f8be67e715f58caf946bb25c6ea4729;hpb=08ff083c1d5839a2c805ef20a347d61b8e9e026c;p=catagits%2FTest-WWW-Mechanize-Catalyst.git diff --git a/t/lib/ExternalCatty.pm b/t/lib/ExternalCatty.pm index e16cf60..27bce2e 100644 --- a/t/lib/ExternalCatty.pm +++ b/t/lib/ExternalCatty.pm @@ -2,26 +2,44 @@ package ExternalCatty; use strict; use warnings; use Catalyst; +use Catalyst::ScriptRunner; +use IO::Socket::INET; __PACKAGE__->config( name => 'ExternalCatty' ); __PACKAGE__->setup; -__PACKAGE__->setup_engine('HTTP'); + +sub MAX_PORT_TRIES() { 5 } # The Cat HTTP server background option is useless here :-( # Thus we have to provide our own background method. sub background { my $self = shift; my $port = shift; + $port = $self->assert_or_find_available_port($port); my $child = fork; die "Can't fork Cat HTTP server: $!" unless defined $child; - return $child if $child; + return($child, $port) if $child; if ( $^O !~ /MSWin32/ ) { require POSIX; POSIX::setsid() or die "Can't start a new session: $!"; } + local @ARGV = ('-p', $port); + Catalyst::ScriptRunner->run(__PACKAGE__, 'Server'); +} - $self->run($port); +sub assert_or_find_available_port { + my($self, $port) = @_; + for my $i (1..MAX_PORT_TRIES) { + IO::Socket::INET->new( + LocalAddr => 'localhost', + LocalPort => $port, + Proto => 'tcp' + ) and return $port; + $port += int(rand 100) + 1; + } + die q{Can't find an open port to run external server on after } + . MAX_PORT_TRIES . q{tries}; } 1;