mwrap (Perl version) user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
* [PATCH] t/mwrap-httpd: avoid busy waits for bind+listen
@ 2022-12-11  6:29 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2022-12-11  6:29 UTC (permalink / raw)
  To: mwrap-perl

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: $!";
 

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

only message in thread, other threads:[~2022-12-11  6:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-11  6:29 [PATCH] t/mwrap-httpd: avoid busy waits for bind+listen Eric Wong

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).