File Coverage

blib/lib/Selenium/CanStartBinary/ProbePort.pm
Criterion Covered Total %
statement 12 21 57.1
branch 0 2 0.0
condition n/a
subroutine 4 7 57.1
pod 0 3 0.0
total 16 33 48.4


line stmt bran cond sub pod time code
1             package Selenium::CanStartBinary::ProbePort;
2             $Selenium::CanStartBinary::ProbePort::VERSION = '1.47';
3 1     1   6 use strict;
  1         2  
  1         24  
4 1     1   4 use warnings;
  1         1  
  1         23  
5              
6             # ABSTRACT: Utility functions for finding open ports to eventually bind to
7              
8 1     1   4 use IO::Socket::INET;
  1         3  
  1         11  
9 1     1   897 use Selenium::Waiter qw/wait_until/;
  1         3  
  1         191  
10              
11             require Exporter;
12             our @ISA = qw/Exporter/;
13             our @EXPORT_OK = qw/find_open_port_above find_open_port probe_port/;
14              
15              
16             sub find_open_port_above {
17 0     0 0   socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname("tcp"));
18 0           bind(SOCK, sockaddr_in(0, INADDR_ANY));
19 0           my $port = (sockaddr_in(getsockname(SOCK)))[0];
20 0           close(SOCK);
21 0           return $port;
22             }
23              
24             sub find_open_port {
25 0     0 0   my ($port) = @_;
26              
27 0 0         probe_port($port) ? return 0 : return $port;
28             }
29              
30             sub probe_port {
31 0     0 0   my ($port) = @_;
32              
33 0           return IO::Socket::INET->new(
34             PeerAddr => '127.0.0.1',
35             PeerPort => $port,
36             Timeout => 3
37             );
38             }
39              
40             __END__