user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH] t/lei-refresh-mail-sync: speed up test on FreeBSD 12
@ 2021-11-02  9:24 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2021-11-02  9:24 UTC (permalink / raw)
  To: meta

And improve reliability while we're at it.  It seems closing a
TCP listen socket on FreeBSD 12.2 doesn't cause connect()-ing
clients to fail.  This happens regardless of whether a socket is
IPv4 or IPv6

This non-failure was causing tests to timeout slowly on the
client side instead of failing immediately.  We now fork a new
process which does nothing but accept() + shutdown() to emulate
a dead server.

Reliability improves on all OSes since there's never a point in
time when another process can bind the socket.
---
 t/lei-refresh-mail-sync.t | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)

diff --git a/t/lei-refresh-mail-sync.t b/t/lei-refresh-mail-sync.t
index 43fbc50a..ea83a513 100644
--- a/t/lei-refresh-mail-sync.t
+++ b/t/lei-refresh-mail-sync.t
@@ -88,12 +88,9 @@ SKIP: {
 		$sock_cls //= ref($s);
 		my $cmd = [ "-$x", '-W0', "--stdout=$home/$x.out",
 			"--stderr=$home/$x.err" ];
-		my $td = start_script($cmd, $env, { 3 => $s}) or xbail("-$x");
-		$srv->{$x} = {
-			addr => (my $scalar = tcp_host_port($s)),
-			td => $td,
-			cmd => $cmd,
-		};
+		my $td = start_script($cmd, $env, { 3 => $s }) or xbail("-$x");
+		my $addr = tcp_host_port($s);
+		$srv->{$x} = { addr => $addr, td => $td, cmd => $cmd, s => $s };
 	}
 	my $url = "imap://$srv->{imapd}->{addr}/t.v1.0";
 	lei_ok 'import', $url, '+L:v1';
@@ -123,20 +120,26 @@ SKIP: {
 	$before = $lei_out;
 	delete $srv->{imapd}->{td}; # kill + join daemon
 
+	my $pid = fork // xbail "fork";
+	if ($pid == 0) { # dummy server to kill new connections
+		$SIG{TERM} = sub { POSIX::_exit(0) };
+		$srv->{imapd}->{s}->blocking(1);
+		while (1) {
+			my $caddr = accept(my $c, $srv->{imapd}->{s}) // next;
+			shutdown($c, 2);
+		}
+		POSIX::_exit(0);
+	}
+	my $ar = PublicInbox::AutoReap->new($pid);
 	ok(!(lei 'refresh-mail-sync', $url), 'URL fails on dead -imapd');
 	ok(!(lei 'refresh-mail-sync', '--all'), '--all fails on dead -imapd');
+	$ar->kill for qw(avoid sig wake miss-no signalfd or EVFILT_SIG);
+	$ar->join('TERM');
 
-	# restart server (somewhat dangerous since we released the socket)
-	my $listen = $sock_cls->new(
-		ReuseAddr => 1,
-		Proto => 'tcp',
-		Type => Socket::SOCK_STREAM(),
-		Listen => 1024,
-		Blocking => 0,
-		LocalAddr => $srv->{imapd}->{addr},
-	) or xbail "$sock_cls->new: $!";
 	my $cmd = $srv->{imapd}->{cmd};
-	$srv->{imapd}->{td} = start_script($cmd, $env, { 3 => $listen }) or
+	my $s = $srv->{imapd}->{s};
+	$s->blocking(0);
+	$srv->{imapd}->{td} = start_script($cmd, $env, { 3 => $s }) or
 		xbail "@$cmd";
 	lei_ok 'refresh-mail-sync', '--all';
 	lei_ok 'inspect', "blob:$oid";

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-11-02  9:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-02  9:24 [PATCH] t/lei-refresh-mail-sync: speed up test on FreeBSD 12 Eric Wong

Code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).