package Exim::Runtest;
use strict;
use warnings;
+use IO::Socket::INET;
use Carp;
use List::Util qw'shuffle';
push @groups, $_ while defined($_ = getgrent);
endgrent;
return (shuffle @groups)[0];
-};
-
+}
+
+sub dynamic_socket {
+ my $socket;
+ for (my $port = 1024; $port < 65000; $port++) {
+ $socket = IO::Socket::INET->new(
+ LocalHost => '127.0.0.1',
+ LocalPort => $port,
+ Listen => 10,
+ ReuseAddr => 1,
+ ) and return $socket;
+ }
+ croak 'Can not allocate a free port.';
+}
1;
use Errno;
use FileHandle;
-use IO::Socket::INET;
use Socket;
use Time::Local;
use Cwd;
if (/^no_stdout_check/) { $stdout_skip = 1; next; }
if (/^rmfiltertest/) { $rmfiltertest = 1; next; }
if (/^sortlog/) { $sortlog = 1; next; }
- if (/\bPORT_DYNAMIC\b/) {
- for (my $port = 1024; $port < 65000; $port++) {
- $dynamic_socket = IO::Socket::INET->new(
- LocalHost => '127.0.0.1',
- LocalPort => $port,
- Listen => 10,
- ReuseAddr => 1,
- ) and last;
- }
- }
+ if (/\bPORT_DYNAMIC\b/) { $dynamic_socket = Exim::Runtest::dynamic_socket(); next; }
}
# Reset to beginning of file for per test interpreting/processing
seek(SCRIPT, 0, 0);
use lib 'lib';
use_ok 'Exim::Runtest' or BAIL_OUT 'Can not load the module';
-can_ok 'Exim::Runtest', qw(mailgroup);
+can_ok 'Exim::Runtest', qw(mailgroup dynamic_socket);
subtest 'mailgroup' => sub {
my $group = getgrgid $(;
ok getgrnam($group) => 'got an existing group';
};
+subtest 'dynamic_socket' => sub {
+ ok my $socket = Exim::Runtest::dynamic_socket() => 'got a socket';
+ diag "got socket on port @{[$socket->sockport]}";
+ isa_ok $socket => 'IO::Socket::INET';
+ $socket->close;
+};
done_testing;