mwrap (Perl version) user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <mwrap-perl@80x24.org>
To: mwrap-perl@80x24.org
Subject: [PATCH] t/mwrap-httpd: avoid busy waits for bind+listen
Date: Sun, 11 Dec 2022 06:29:47 +0000	[thread overview]
Message-ID: <20221211062947.92658-1-mwrap-perl@80x24.org> (raw)

Using a FIFO in Perl means our mwrap_ctor constructor has
already run and bound the listen socket.  So using it as a
synchronization barrier means we can rely on it to ensure
the socket is connect-able.
---
 t/mwrap-httpd.t | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/t/mwrap-httpd.t b/t/mwrap-httpd.t
index b7076de..73b704e 100644
--- a/t/mwrap-httpd.t
+++ b/t/mwrap-httpd.t
@@ -7,9 +7,12 @@ use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
 use POSIX qw(dup2 _exit mkfifo);
 BEGIN { require './t/test_common.perl' };
 my $env = { MWRAP => "socket_dir:$mwrap_tmp" };
-my $fifo = "$mwrap_tmp/fifo";
-mkfifo($fifo, 0600) // plan(skip_all => "mkfifo: $!");
-my $pid = mwrap_run('httpd test', $env, '-e', "open my \$fh, '<', '$fifo'");
+my $f1 = "$mwrap_tmp/f1";
+my $f2 = "$mwrap_tmp/f2";
+mkfifo($f1, 0600) // plan(skip_all => "mkfifo: $!");
+mkfifo($f2, 0600) // plan(skip_all => "mkfifo: $!");
+my $pid = mwrap_run('httpd test', $env, '-e',
+	"open my \$f1, '>', '$f1'; close \$f1; open my \$f2, '<', '$f2'");
 my $spid;
 my $mw_exit;
 my $cleanup = sub {
@@ -25,7 +28,7 @@ my $cleanup = sub {
 	use autodie;
 	if (defined $pid) {
 		my $exit = $?;
-		open my $fh, '>', $fifo;
+		open my $fh, '>', $f2;
 		close $fh;
 		waitpid($pid, 0);
 		$mw_exit = $?;
@@ -39,12 +42,12 @@ END { $cleanup->() }
 my $sock = "$mwrap_tmp/$pid.sock";
 my %o = (Peer => $sock , Type => SOCK_STREAM);
 local $SIG{PIPE} = 'IGNORE';
-my $c;
-for (1..10000) {
-	last if -S $sock && ($c = IO::Socket::UNIX->new(%o));
-	select undef, undef, undef, 0.011;
-}
+
+open my $fh, '<', $f1;
+is(my $nil = <$fh>, undef, 'FIFO open');
+close $fh;
 ok(-S $sock, 'socket created');
+my $c = IO::Socket::UNIX->new(%o);
 ok($c, 'socket connected');
 is(send($c, 'GET', MSG_NOSIGNAL), 3, 'trickled 3 bytes') or diag "send: $!";
 

                 reply	other threads:[~2022-12-11  6:29 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

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

  git send-email \
    --in-reply-to=20221211062947.92658-1-mwrap-perl@80x24.org \
    --to=mwrap-perl@80x24.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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mwrap-perl.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).