Testsuite: move dynamic_socket to Exim::Runtest
[exim.git] / test / lib / Exim / Runtest.pm
index 2ac9a61ee0944f0c6b19489e3cb971c893dcb54e..1df2ea1a9b7ea8bafcfee15593e96bd3c1504d4a 100644 (file)
@@ -1,6 +1,7 @@
 package Exim::Runtest;
 use strict;
 use warnings;
+use IO::Socket::INET;
 use Carp;
 
 use List::Util qw'shuffle';
@@ -22,7 +23,19 @@ sub mailgroup {
     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;