1
0

ProbePort.pm 881 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package Selenium::CanStartBinary::ProbePort;
  2. use strict;
  3. use warnings;
  4. # ABSTRACT: Utility functions for finding open ports to eventually bind to
  5. use IO::Socket::INET;
  6. use Selenium::Waiter qw/wait_until/;
  7. require Exporter;
  8. our @ISA = qw/Exporter/;
  9. our @EXPORT_OK = qw/find_open_port_above find_open_port probe_port/;
  10. =for Pod::Coverage *EVERYTHING*
  11. =cut
  12. sub find_open_port_above {
  13. my ($port) = @_;
  14. my $free_port = wait_until {
  15. if ( probe_port($port) ) {
  16. $port++;
  17. return 0;
  18. }
  19. else {
  20. return $port;
  21. }
  22. };
  23. return $free_port;
  24. }
  25. sub find_open_port {
  26. my ($port) = @_;
  27. probe_port($port) ? return 0 : return $port;
  28. }
  29. sub probe_port {
  30. my ($port) = @_;
  31. return IO::Socket::INET->new(
  32. PeerAddr => '127.0.0.1',
  33. PeerPort => $port,
  34. Timeout => 3
  35. );
  36. }