chiark / gitweb /
http[s] tests: Fix stdout handling in ftpmaster test server
[dgit.git] / tests / ftpmasterapi-static-server
index cedc6f7cc580f93ba8197a2530bc691551f1a3de..f218cff9a0ceb33d16c360fa649b11f4c003e163 100755 (executable)
@@ -30,17 +30,24 @@ use IO::Handle;
 our ($webroot) = @ARGV;
 our $port = '';
 
+# HTTP::Server::Simple handles requests in the main process so it
+# must redirect and close STDOUT.  So transplant STDOUT to CHECK.
+open CHECK, ">& STDOUT" or die $!;
+open STDOUT, ">/dev/null" or die $!;
+
 sub stat_type_check () {
     die "[$port, $webroot] stdout not ta plain file"
        unless -f _;
 }
 
-stat STDOUT or die $!;
+stat CHECK or die $!;
 stat_type_check();
 
 sub start_polling_fstat () {
+    our $polling_pid = $$;
     $SIG{ALRM} = sub {
-       stat STDOUT or die $!;
+       return unless $$ = $polling_pid;
+       stat CHECK or die $!;
        my $nlink = (stat _)[3];
        exit 0 unless $nlink;
        stat_type_check(); # doesn't seem possible to fail but check anyway
@@ -76,8 +83,8 @@ sub port () { return 0; }
 sub after_setup_listener () {
     my $sn = getsockname HTTP::Server::Simple::HTTPDaemon or die $!;
     ($main::port,) = unpack_sockaddr_in $sn;
-    print main::STDOUT $port, "\n" or die $!;
-    flush main::STDOUT or die $!;
+    print main::CHECK $port, "\n" or die $!;
+    flush main::CHECK or die $!;
     my $c = fork // die $!;
     exit 0 if $c;
     ::main::start_polling_fstat();