user/dev discussion of public-inbox itself
 help / color / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 1/2] tests: common tcp_server and unix_server helpers
Date: Sun, 30 Jun 2019 22:19:38 +0000
Message-ID: <20190630221939.9885-2-e@80x24.org> (raw)
In-Reply-To: <20190630221939.9885-1-e@80x24.org>

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.
---
 t/common.perl        | 18 ++++++++++++++++++
 t/git-http-backend.t |  9 +--------
 t/httpd-corner.t     | 15 ++-------------
 t/httpd-https.t      |  9 +--------
 t/httpd.t            | 10 +---------
 t/nntpd-tls.t        | 11 ++---------
 t/nntpd.t            | 10 ++--------
 t/perf-nntpd.t       |  9 +--------
 t/v2mirror.t         |  8 +-------
 t/v2writable.t       |  9 +--------
 t/www_listing.t      |  9 +--------
 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 3ca1ce33..ab11b037 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);
-- 
EW


  reply index

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-30 22:19 [PATCH 0/2] warn on inheriting blocking sockets Eric Wong
2019-06-30 22:19 ` Eric Wong [this message]
2019-06-30 22:19 ` [PATCH 2/2] daemon: warn on inheriting blocking listeners Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190630221939.9885-2-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

user/dev discussion of public-inbox itself

Archives are clonable:
	git clone --mirror https://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://ou63pmih66umazou.onion/inbox.comp.mail.public-inbox.meta
	nntp://czquwvybam4bgbro.onion/inbox.comp.mail.public-inbox.meta
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.io/gmane.mail.public-inbox.general

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git