5 use Catalyst::ScriptRunner;
8 __PACKAGE__->config( name => 'ExternalCatty' );
11 sub MAX_PORT_TRIES() { 5 }
13 # The Cat HTTP server background option is useless here :-(
14 # Thus we have to provide our own background method.
18 $port = $self->assert_or_find_available_port($port);
20 die "Can't fork Cat HTTP server: $!" unless defined $child;
21 return($child, $port) if $child;
23 if ( $^O !~ /MSWin32/ ) {
25 POSIX::setsid() or die "Can't start a new session: $!";
27 local @ARGV = ('-p', $port);
28 Catalyst::ScriptRunner->run(__PACKAGE__, 'Server');
31 sub assert_or_find_available_port {
32 my($self, $port) = @_;
33 for my $i (1..MAX_PORT_TRIES) {
34 IO::Socket::INET->new(
35 LocalAddr => 'localhost',
39 $port += int(rand 100) + 1;
41 die q{Can't find an open port to run external server on after }
42 . MAX_PORT_TRIES . q{tries};