From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.1 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id CCA461F61A for ; Sun, 11 Dec 2022 06:29:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1670740146; bh=DZz4kRtocxI5akMM/8GJJbweQEilUV8swND2C2PYDc4=; h=From:To:Subject:Date:From; b=TUOsZpzZ5hZqRq/L+aLrHKbg7NIrKgt+z7c8SjMAJDvDEwi4QZOoWwwN8JBJTvCU3 L0v4Ddw2iUuwTlxapR+qTwHUb7l+EqIxVC176eS9UXs9WwCzAX1lM16FWG9M2aIFTN dohHtyoKjJauY9q/lDMnfI2vArExG4HyUhle6Aok= From: Eric Wong 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 Message-Id: <20221211062947.92658-1-mwrap-perl@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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: $!";