unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] t/lei-refresh-mail-sync: avoid kill+sleep loop
Date: Sun, 12 Feb 2023 03:12:03 +0000	[thread overview]
Message-ID: <20230212031203.3020863-1-e@80x24.org> (raw)

While we can't waitpid() on daemonized process, we can abuse the
lack of FD_CLOEXEC to detect a process death.  This saves
roughly 400ms for this slow test.
---
 lib/PublicInbox/TestCommon.pm |  3 +++
 t/lei-refresh-mail-sync.t     | 20 ++++++++++++--------
 2 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/lib/PublicInbox/TestCommon.pm b/lib/PublicInbox/TestCommon.pm
index 1fe7931e..8a34e45a 100644
--- a/lib/PublicInbox/TestCommon.pm
+++ b/lib/PublicInbox/TestCommon.pm
@@ -480,6 +480,9 @@ sub start_script {
 	my $pid = fork // die "fork: $!\n";
 	if ($pid == 0) {
 		eval { PublicInbox::DS->Reset };
+		for (@{delete($opt->{-CLOFORK}) // []}) {
+			close($_) or die "close $!";
+		}
 		# pretend to be systemd (cf. sd_listen_fds(3))
 		# 3 == SD_LISTEN_FDS_START
 		my $fd;
diff --git a/t/lei-refresh-mail-sync.t b/t/lei-refresh-mail-sync.t
index ea83a513..0498a0c4 100644
--- a/t/lei-refresh-mail-sync.t
+++ b/t/lei-refresh-mail-sync.t
@@ -5,17 +5,20 @@ use strict; use v5.10.1; use PublicInbox::TestCommon;
 require_mods(qw(lei));
 use File::Path qw(remove_tree);
 require Socket;
+use Fcntl qw(F_SETFD);
+
+pipe(my ($stop_r, $stop_w)) or xbail "pipe: $!";
+fcntl($stop_w, F_SETFD, 0) or xbail "F_SETFD: $!";
 
 my $stop_daemon = sub { # needed since we don't have inotify
+	close $stop_w or xbail "close \$stop_w: $!";
 	lei_ok qw(daemon-pid);
 	chomp(my $pid = $lei_out);
 	$pid > 0 or xbail "bad pid: $pid";
 	kill('TERM', $pid) or xbail "kill: $!";
-	for (0..10) {
-		tick;
-		kill(0, $pid) or last;
-	}
-	kill(0, $pid) and xbail "daemon still running (PID:$pid)";
+	is(sysread($stop_r, my $buf, 1), 0, 'daemon stop pipe read EOF');
+	pipe($stop_r, $stop_w) or xbail "pipe: $!";
+	fcntl($stop_w, F_SETFD, 0) or xbail "F_SETFD: $!";
 };
 
 test_lei({ daemon_only => 1 }, sub {
@@ -88,7 +91,8 @@ 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");
+		my $opt = { 3 => $s, -CLOFORK => [ $stop_w ] };
+		my $td = start_script($cmd, $env, $opt) or xbail("-$x");
 		my $addr = tcp_host_port($s);
 		$srv->{$x} = { addr => $addr, td => $td, cmd => $cmd, s => $s };
 	}
@@ -139,8 +143,8 @@ SKIP: {
 	my $cmd = $srv->{imapd}->{cmd};
 	my $s = $srv->{imapd}->{s};
 	$s->blocking(0);
-	$srv->{imapd}->{td} = start_script($cmd, $env, { 3 => $s }) or
-		xbail "@$cmd";
+	my $opt = { 3 => $s, -CLOFORK => [ $stop_w ] };
+	$srv->{imapd}->{td} = start_script($cmd, $env, $opt) or xbail "@$cmd";
 	lei_ok 'refresh-mail-sync', '--all';
 	lei_ok 'inspect', "blob:$oid";
 	is($lei_out, $before, 'no changes when server was down');

                 reply	other threads:[~2023-02-12  3:13 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20230212031203.3020863-1-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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).