Work around Msys weakness in Testlib.pm's command_like()
authorAndrew Dunstan <andrew@dunslane.net>
Wed, 26 Jul 2017 21:15:59 +0000 (17:15 -0400)
committerAndrew Dunstan <andrew@dunslane.net>
Thu, 27 Jul 2017 02:46:55 +0000 (22:46 -0400)
When output of IPC::Run::run () is redirected to scalar references, in
certain circumstances the Msys perl does not correctly detect that the
end of file has been seen, making the test hang indefinitely. One such
circumstance is when the command is 'pg_ctl start', and such a change
was made in commit f13ea95f9e. The workaround, which only applies on
MSys, is to redirect the output to temporary files and then read them in
when the process has finished.

Patch by me, reviewed and tweaked by Tom Lane.

src/bin/pg_ctl/t/001_start_stop.pl
src/test/perl/TestLib.pm

index 9c3551f1a5bf54367e8270a36429bcac434ffe06..3acc80e204e46f617adf403eccc5d597d4787e13 100644 (file)
@@ -32,9 +32,17 @@ else
    print $conf "listen_addresses = '127.0.0.1'\n";
 }
 close $conf;
-command_like([ 'pg_ctl', 'start', '-D', "$tempdir/data",
-   '-l', "$TestLib::log_path/001_start_stop_server.log" ],
-   qr/done.*server started/s, 'pg_ctl start');
+my $ctlcmd = [ 'pg_ctl', 'start', '-D', "$tempdir/data",
+              '-l', "$TestLib::log_path/001_start_stop_server.log" ];
+if ($Config{osname} ne 'msys')
+{
+   command_like($ctlcmd, qr/done.*server started/s, 'pg_ctl start');
+}
+else
+{
+   # use the version of command_like that doesn't hang on Msys here
+   command_like_safe($ctlcmd, qr/done.*server started/s, 'pg_ctl start');
+}
 
 # sleep here is because Windows builds can't check postmaster.pid exactly,
 # so they may mistake a pre-existing postmaster.pid for one created by the
index fe09689fec2005cc0b8da8cbdb66100a9ea00a23..bff6b3aed277e52f465b5f97bb99d827a4bb3931 100644 (file)
@@ -37,6 +37,7 @@ our @EXPORT = qw(
   program_version_ok
   program_options_handling_ok
   command_like
+  command_like_safe
   command_fails_like
 
   $windows_os
@@ -300,6 +301,24 @@ sub command_like
    like($stdout, $expected_stdout, "$test_name: matches");
 }
 
+sub command_like_safe
+{
+   # Doesn't rely on detecting end of file on the file descriptors,
+   # which can fail, causing the process to hang, notably on Msys
+   # when used with 'pg_ctl start'
+   my ($cmd, $expected_stdout, $test_name) = @_;
+   my ($stdout, $stderr);
+   my $stdoutfile = File::Temp->new();
+   my $stderrfile = File::Temp->new();
+   print("# Running: " . join(" ", @{$cmd}) . "\n");
+   my $result = IPC::Run::run $cmd, '>', $stdoutfile, '2>', $stderrfile;
+   $stdout = slurp_file($stdoutfile);
+   $stderr = slurp_file($stderrfile);
+   ok($result, "$test_name: exit code 0");
+   is($stderr, '', "$test_name: no stderr");
+   like($stdout, $expected_stdout, "$test_name: matches");
+}
+
 sub command_fails_like
 {
    my ($cmd, $expected_stderr, $test_name) = @_;