Seeking code to find a free TCP/IP port
I know some modules search for a free port during testing, but I can't think of any specific one ATM. Any suggestions?
I know some modules search for a free port during testing, but I can't think of any specific one ATM. Any suggestions?
I try to write all code in Perl, but find I end up writing in bash, CSS, HTML, JS, and SQL, and doing database design, just to get anything done...
There's an example on this very bpo scroll: https://github.com/jshy/Test-mongod/blob/master/lib/Test/mongod.pm
Tldr, Net::EmptyPort
Test::TCP does this, but would it be possible to just create a socket and let the OS assign it an ephemeral port, and use that in your application?
Thanx for the suggestions. Net::EmptyPort in Test::TCP it is.
I should also mention that I have never used the above module, I had just seen it. I have been recommended to share another technique seen here: https://github.com/kraih/mojo/blob/master/lib/Mojo/IOLoop/Server.pm#L43
Exactly, you can just omit port during listen, OS will pick random free port.
See also this discussion:
http://www.perlmonks.org/?node_id=579649
The code using this is a test, t/daemon.t, in Pod::Webserver, which I recently adopted. It (obviously) runs a daemon (which I very rarely do in my code), using:
my $port = $ENV{'PODWEBSERVERPORT'};
Due to fails via CPAN testers, I changed that to:
my $port = $ENV{'PODWEBSERVERPORT'} || empty_port();
But then the connexion was always refused. Putting that in a BEGIN block worked.
I've saved your links in my annotated list of modules. $many x $thanx;