about summary refs log tree commit
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-06-30 22:19:38 +0000
committerEric Wong <e@80x24.org>2019-06-30 22:25:23 +0000
commit3eb26fe04dae68612c841e749abd2848ce78ae59 (patch)
tree4047806eec0029de5cea7db2b18ec4408d8ec65d
parent0b84d8600368c67080e683e415f7e502a343316b (diff)
downloadpublic-inbox-3eb26fe04dae68612c841e749abd2848ce78ae59.tar.gz
IO::Socket:*->new options are verbose and we can save
a bunch of code by putting this into t/common.perl,
since the related spawn_listener stuff is already there.
-rw-r--r--t/common.perl18
-rw-r--r--t/git-http-backend.t9
-rw-r--r--t/httpd-corner.t15
-rw-r--r--t/httpd-https.t9
-rw-r--r--t/httpd.t10
-rw-r--r--t/nntpd-tls.t11
-rw-r--r--t/nntpd.t10
-rw-r--r--t/perf-nntpd.t9
-rw-r--r--t/v2mirror.t8
-rw-r--r--t/v2writable.t9
-rw-r--r--t/www_listing.t9
11 files changed, 31 insertions, 86 deletions
diff --git a/t/common.perl b/t/common.perl
index 5a898e32..3f05b68a 100644
--- a/t/common.perl
+++ b/t/common.perl
@@ -17,6 +17,24 @@ sub stream_to_string {
         $str;
 }
 
+sub tcp_server () {
+        IO::Socket::INET->new(
+                LocalAddr => '127.0.0.1',
+                ReuseAddr => 1,
+                Proto => 'tcp',
+                Type => Socket::SOCK_STREAM(),
+                Listen => 1024,
+        )
+}
+
+sub unix_server ($) {
+        IO::Socket::UNIX->new(
+                Listen => 1024,
+                Type => Socket::SOCK_STREAM(),
+                Local => $_[0],
+        )
+}
+
 sub spawn_listener {
         my ($env, $cmd, $socks) = @_;
         my $pid = fork;
diff --git a/t/git-http-backend.t b/t/git-http-backend.t
index fc2d5462..946cd86a 100644
--- a/t/git-http-backend.t
+++ b/t/git-http-backend.t
@@ -24,14 +24,7 @@ my $tmpdir = tempdir('pi-git-http-backend-XXXXXX', TMPDIR => 1, CLEANUP => 1);
 my $err = "$tmpdir/stderr.log";
 my $out = "$tmpdir/stdout.log";
 my $httpd = 'blib/script/public-inbox-httpd';
-my %opts = (
-        LocalAddr => '127.0.0.1',
-        ReuseAddr => 1,
-        Proto => 'tcp',
-        Type => SOCK_STREAM,
-        Listen => 1024,
-);
-my $sock = IO::Socket::INET->new(%opts);
+my $sock = tcp_server();
 my $host = $sock->sockhost;
 my $port = $sock->sockport;
 my $pid;
diff --git a/t/httpd-corner.t b/t/httpd-corner.t
index 1cfc2565..5efb9d14 100644
--- a/t/httpd-corner.t
+++ b/t/httpd-corner.t
@@ -28,14 +28,7 @@ my $err = "$tmpdir/stderr.log";
 my $out = "$tmpdir/stdout.log";
 my $httpd = 'blib/script/public-inbox-httpd';
 my $psgi = "./t/httpd-corner.psgi";
-my %opts = (
-        LocalAddr => '127.0.0.1',
-        ReuseAddr => 1,
-        Proto => 'tcp',
-        Type => SOCK_STREAM,
-        Listen => 1024,
-);
-my $sock = IO::Socket::INET->new(%opts);
+my $sock = tcp_server();
 
 # Make sure we don't clobber socket options set by systemd or similar
 # using socket activation:
@@ -56,11 +49,7 @@ if ($^O eq 'linux') {
 }
 
 my $upath = "$tmpdir/s";
-my $unix = IO::Socket::UNIX->new(
-        Listen => 1024,
-        Type => SOCK_STREAM,
-        Local => $upath
-);
+my $unix = unix_server($upath);
 ok($unix, 'UNIX socket created');
 my $pid;
 END { kill 'TERM', $pid if defined $pid };
diff --git a/t/httpd-https.t b/t/httpd-https.t
index f6b9806a..93966949 100644
--- a/t/httpd-https.t
+++ b/t/httpd-https.t
@@ -24,14 +24,7 @@ my $tmpdir = tempdir('pi-httpd-https-XXXXXX', TMPDIR => 1, CLEANUP => 1);
 my $err = "$tmpdir/stderr.log";
 my $out = "$tmpdir/stdout.log";
 my $httpd = 'blib/script/public-inbox-httpd';
-my %opts = (
-        LocalAddr => '127.0.0.1',
-        ReuseAddr => 1,
-        Proto => 'tcp',
-        Type => SOCK_STREAM,
-        Listen => 1024,
-);
-my $https = IO::Socket::INET->new(%opts);
+my $https = tcp_server();
 my ($pid, $tail_pid);
 END {
         foreach ($pid, $tail_pid) {
diff --git a/t/httpd.t b/t/httpd.t
index e085c4b9..e0a2bf44 100644
--- a/t/httpd.t
+++ b/t/httpd.t
@@ -24,15 +24,7 @@ my $addr = $group . '@example.com';
 my $cfgpfx = "publicinbox.$group";
 my $httpd = 'blib/script/public-inbox-httpd';
 my $init = 'blib/script/public-inbox-init';
-
-my %opts = (
-        LocalAddr => '127.0.0.1',
-        ReuseAddr => 1,
-        Proto => 'tcp',
-        Type => SOCK_STREAM,
-        Listen => 1024,
-);
-my $sock = IO::Socket::INET->new(%opts);
+my $sock = tcp_server();
 my $pid;
 use_ok 'PublicInbox::Git';
 use_ok 'PublicInbox::Import';
diff --git a/t/nntpd-tls.t b/t/nntpd-tls.t
index e3ecdd4f..82b63f3e 100644
--- a/t/nntpd-tls.t
+++ b/t/nntpd-tls.t
@@ -37,15 +37,8 @@ my $pi_config = "$tmpdir/pi_config";
 my $group = 'test-nntpd-tls';
 my $addr = $group . '@example.com';
 my $nntpd = 'blib/script/public-inbox-nntpd';
-my %opts = (
-        LocalAddr => '127.0.0.1',
-        ReuseAddr => 1,
-        Proto => 'tcp',
-        Type => SOCK_STREAM,
-        Listen => 1024,
-);
-my $starttls = IO::Socket::INET->new(%opts);
-my $nntps = IO::Socket::INET->new(%opts);
+my $starttls = tcp_server();
+my $nntps = tcp_server();
 my ($pid, $tail_pid);
 END {
         foreach ($pid, $tail_pid) {
diff --git a/t/nntpd.t b/t/nntpd.t
index bf5bb883..0e59de07 100644
--- a/t/nntpd.t
+++ b/t/nntpd.t
@@ -36,14 +36,8 @@ SKIP: {
         use_ok 'PublicInbox::V2Writable';
 }
 
-my %opts = (
-        LocalAddr => '127.0.0.1',
-        ReuseAddr => 1,
-        Proto => 'tcp',
-        Type => SOCK_STREAM,
-        Listen => 1024,
-);
-my $sock = IO::Socket::INET->new(%opts);
+my %opts;
+my $sock = tcp_server();
 my $pid;
 my $len;
 END { kill 'TERM', $pid if defined $pid };
diff --git a/t/perf-nntpd.t b/t/perf-nntpd.t
index ce3cc409..29d6aaed 100644
--- a/t/perf-nntpd.t
+++ b/t/perf-nntpd.t
@@ -44,14 +44,7 @@ if (($ENV{NNTP_TEST_URL} || '') =~ m!\Anntp://([^/]+)/([^/]+)\z!) {
                 close $fh or die "close($pi_config): $!";
         }
 
-        %opts = (
-                LocalAddr => '127.0.0.1',
-                ReuseAddr => 1,
-                Proto => 'tcp',
-                Listen => 1024,
-        );
-        my $sock = IO::Socket::INET->new(%opts);
-
+        my $sock = tcp_server();
         ok($sock, 'sock created');
         my $cmd = [ $nntpd, '-W0' ];
         $pid = spawn_listener({ PI_CONFIG => $pi_config }, $cmd, [$sock]);
diff --git a/t/v2mirror.t b/t/v2mirror.t
index c31dcd5b..9bc32f30 100644
--- a/t/v2mirror.t
+++ b/t/v2mirror.t
@@ -60,17 +60,11 @@ my $epoch_max = $v2w->{epoch_max};
 ok($epoch_max > 0, "multiple epochs");
 $v2w->done;
 
-my %opts = (
-        LocalAddr => '127.0.0.1',
-        ReuseAddr => 1,
-        Proto => 'tcp',
-        Listen => 1024,
-);
 my ($sock, $pid);
 END { kill 'TERM', $pid if defined $pid };
 
 $! = 0;
-$sock = IO::Socket::INET->new(%opts);
+$sock = tcp_server();
 ok($sock, 'sock created');
 my $cmd = [ "$script-httpd", "--stdout=$tmpdir/out", "--stderr=$tmpdir/err" ];
 ok(defined($pid = spawn_listener(undef, $cmd, [ $sock ])),
diff --git a/t/v2writable.t b/t/v2writable.t
index 8f32fbe5..5406fd1b 100644
--- a/t/v2writable.t
+++ b/t/v2writable.t
@@ -134,13 +134,6 @@ if ('ensure git configs are correct') {
         use IO::Socket::INET;
         my $err = "$mainrepo/stderr.log";
         my $out = "$mainrepo/stdout.log";
-        my %opts = (
-                LocalAddr => '127.0.0.1',
-                ReuseAddr => 1,
-                Proto => 'tcp',
-                Type => SOCK_STREAM,
-                Listen => 1024,
-        );
         my $group = 'inbox.comp.test.v2writable';
         my $pi_config = "$mainrepo/pi_config";
         open my $fh, '>', $pi_config or die "open: $!\n";
@@ -153,7 +146,7 @@ if ('ensure git configs are correct') {
 EOF
         ;
         close $fh or die "close: $!\n";
-        my $sock = IO::Socket::INET->new(%opts);
+        my $sock = tcp_server();
         ok($sock, 'sock created');
         my $pid;
         my $len;
diff --git a/t/www_listing.t b/t/www_listing.t
index e5b797db..a5d81f38 100644
--- a/t/www_listing.t
+++ b/t/www_listing.t
@@ -73,14 +73,7 @@ SKIP: {
         my $v2 = "$tmpdir/v2";
         my $httpd = 'blib/script/public-inbox-httpd';
         use IO::Socket::INET;
-        my %opts = (
-                LocalAddr => '127.0.0.1',
-                ReuseAddr => 1,
-                Proto => 'tcp',
-                Type => SOCK_STREAM,
-                Listen => 1024,
-        );
-        my $sock = IO::Socket::INET->new(%opts);
+        my $sock = tcp_server();
         ok($sock, 'sock created');
         my ($host, $port) = ($sock->sockhost, $sock->sockport);
         my @clone = qw(git clone -q -s --bare);