Show
Ignore:
Timestamp:
07/03/09 18:13:00 (5 months ago)
Author:
bradfitz
Message:

make Perlbal::Test be more robust and only use free ports.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/Perlbal/Test.pm

    r784 r822  
    2828use POSIX qw( :sys_wait_h ); 
    2929use IO::Socket::INET; 
     30use Socket qw(MSG_NOSIGNAL IPPROTO_TCP TCP_NODELAY SOL_SOCKET); 
    3031use HTTP::Response; 
    3132 
     
    9798=head1 I<new_port()> 
    9899 
    99 Return the next port number in the series. Port numbers are assigned 
     100Return the next free port number in the series. Port numbers are assigned 
    100101starting at 60000. 
    101102 
     
    103104 
    104105sub new_port { 
    105     return $free_port++;  # FIXME: make it somehow detect if port is in use? 
     106    test_port() ? return $free_port++ : return new_port($free_port++); 
     107} 
     108 
     109=head1 I<test_port()> 
     110 
     111Return 1 if the port is free to use for listening on $free_port else return 0. 
     112 
     113=cut 
     114 
     115sub test_port { 
     116    my $sock = IO::Socket::INET->new(LocalPort => $free_port) or return 0; 
     117    $sock->close(); 
     118    return 1; 
    106119} 
    107120