From 0810c36f444b517c40d338a42ab3946590a040cd Mon Sep 17 00:00:00 2001
From: vignesh <vignesh@localhost.localdomain>
Date: Fri, 22 Nov 2019 14:42:13 +0530
Subject: [PATCH 2/2] Made pump_until usable as a common subroutine.

Patch includes movement of pump_until subroutine from 013_crash_restart to
TestLib so that it can be used as a common sub from 013_crash_restart and
060_dropdb_force.
---
 src/test/perl/TestLib.pm                 | 37 ++++++++++++++++++
 src/test/recovery/t/013_crash_restart.pl | 66 ++++++++++----------------------
 2 files changed, 57 insertions(+), 46 deletions(-)

diff --git a/src/test/perl/TestLib.pm b/src/test/perl/TestLib.pm
index 905d0d1..ed16c6f 100644
--- a/src/test/perl/TestLib.pm
+++ b/src/test/perl/TestLib.pm
@@ -841,6 +841,43 @@ sub command_checks_all
 
 =pod
 
+=item pump_until(proc, psql_timeout, stream, untl)
+
+# Pump until string is matched, or timeout occurs
+
+=cut
+
+sub pump_until
+{
+        my ($proc, $psql_timeout, $stream, $untl) = @_;
+        $proc->pump_nb();
+        while (1)
+        {
+                last if $$stream =~ /$untl/;
+                if ($psql_timeout->is_expired)
+                {
+                        diag("aborting wait: program timed out");
+                        diag("stream contents: >>", $$stream, "<<");
+                        diag("pattern searched for: ", $untl);
+
+                        return 0;
+                }
+                if (not $proc->pumpable())
+                {
+                        diag("aborting wait: program died");
+                        diag("stream contents: >>", $$stream, "<<");
+                        diag("pattern searched for: ", $untl);
+
+                        return 0;
+                }
+                $proc->pump();
+        }
+        return 1;
+
+}
+
+=pod
+
 =back
 
 =cut
diff --git a/src/test/recovery/t/013_crash_restart.pl b/src/test/recovery/t/013_crash_restart.pl
index 2c47797..c93d465 100644
--- a/src/test/recovery/t/013_crash_restart.pl
+++ b/src/test/recovery/t/013_crash_restart.pl
@@ -72,8 +72,8 @@ CREATE TABLE alive(status text);
 INSERT INTO alive VALUES($$committed-before-sigquit$$);
 SELECT pg_backend_pid();
 ];
-ok(pump_until($killme, \$killme_stdout, qr/[[:digit:]]+[\r\n]$/m),
-	'acquired pid for SIGQUIT');
+ok(TestLib::pump_until($killme, $psql_timeout, \$killme_stdout,
+	qr/[[:digit:]]+[\r\n]$/m), 'acquired pid for SIGQUIT');
 my $pid = $killme_stdout;
 chomp($pid);
 $killme_stdout = '';
@@ -84,8 +84,8 @@ $killme_stdin .= q[
 BEGIN;
 INSERT INTO alive VALUES($$in-progress-before-sigquit$$) RETURNING status;
 ];
-ok(pump_until($killme, \$killme_stdout, qr/in-progress-before-sigquit/m),
-	'inserted in-progress-before-sigquit');
+ok(TestLib::pump_until($killme, $psql_timeout, \$killme_stdout,
+	qr/in-progress-before-sigquit/m), 'inserted in-progress-before-sigquit');
 $killme_stdout = '';
 $killme_stderr = '';
 
@@ -97,8 +97,8 @@ $monitor_stdin .= q[
 SELECT $$psql-connected$$;
 SELECT pg_sleep(3600);
 ];
-ok(pump_until($monitor, \$monitor_stdout, qr/psql-connected/m),
-	'monitor connected');
+ok(TestLib::pump_until($monitor, $psql_timeout, \$monitor_stdout,
+	qr/psql-connected/m), 'monitor connected');
 $monitor_stdout = '';
 $monitor_stderr = '';
 
@@ -112,8 +112,9 @@ is($ret, 0, "killed process with SIGQUIT");
 $killme_stdin .= q[
 SELECT 1;
 ];
-ok( pump_until(
+ok( TestLib::pump_until(
 		$killme,
+		$psql_timeout,
 		\$killme_stderr,
 		qr/WARNING:  terminating connection because of crash of another server process|server closed the connection unexpectedly|connection to server was lost/m
 	),
@@ -125,8 +126,9 @@ $killme->finish;
 # Wait till server restarts - we should get the WARNING here, but
 # sometimes the server is unable to send that, if interrupted while
 # sending.
-ok( pump_until(
+ok( TestLib::pump_until(
 		$monitor,
+		$psql_timeout,
 		\$monitor_stderr,
 		qr/WARNING:  terminating connection because of crash of another server process|server closed the connection unexpectedly|connection to server was lost/m
 	),
@@ -153,8 +155,8 @@ $monitor->run();
 $killme_stdin .= q[
 SELECT pg_backend_pid();
 ];
-ok(pump_until($killme, \$killme_stdout, qr/[[:digit:]]+[\r\n]$/m),
-	"acquired pid for SIGKILL");
+ok(TestLib::pump_until($killme, $psql_timeout, \$killme_stdout,
+	qr/[[:digit:]]+[\r\n]$/m), "acquired pid for SIGKILL");
 $pid = $killme_stdout;
 chomp($pid);
 $killme_stdout = '';
@@ -166,8 +168,8 @@ INSERT INTO alive VALUES($$committed-before-sigkill$$) RETURNING status;
 BEGIN;
 INSERT INTO alive VALUES($$in-progress-before-sigkill$$) RETURNING status;
 ];
-ok(pump_until($killme, \$killme_stdout, qr/in-progress-before-sigkill/m),
-	'inserted in-progress-before-sigkill');
+ok(TestLib::pump_until($killme, $psql_timeout, \$killme_stdout,
+	qr/in-progress-before-sigkill/m), 'inserted in-progress-before-sigkill');
 $killme_stdout = '';
 $killme_stderr = '';
 
@@ -178,8 +180,8 @@ $monitor_stdin .= q[
 SELECT $$psql-connected$$;
 SELECT pg_sleep(3600);
 ];
-ok(pump_until($monitor, \$monitor_stdout, qr/psql-connected/m),
-	'monitor connected');
+ok(TestLib::pump_until($monitor, $psql_timeout, \$monitor_stdout,
+	qr/psql-connected/m), 'monitor connected');
 $monitor_stdout = '';
 $monitor_stderr = '';
 
@@ -194,8 +196,9 @@ is($ret, 0, "killed process with KILL");
 $killme_stdin .= q[
 SELECT 1;
 ];
-ok( pump_until(
+ok( TestLib::pump_until(
 		$killme,
+		$psql_timeout,
 		\$killme_stderr,
 		qr/server closed the connection unexpectedly|connection to server was lost/m
 	),
@@ -205,8 +208,9 @@ $killme->finish;
 # Wait till server restarts - we should get the WARNING here, but
 # sometimes the server is unable to send that, if interrupted while
 # sending.
-ok( pump_until(
+ok( TestLib::pump_until(
 		$monitor,
+		$psql_timeout,
 		\$monitor_stderr,
 		qr/WARNING:  terminating connection because of crash of another server process|server closed the connection unexpectedly|connection to server was lost/m
 	),
@@ -244,33 +248,3 @@ is( $node->safe_psql(
 	'can still write after orderly restart');
 
 $node->stop();
-
-# Pump until string is matched, or timeout occurs
-sub pump_until
-{
-	my ($proc, $stream, $untl) = @_;
-	$proc->pump_nb();
-	while (1)
-	{
-		last if $$stream =~ /$untl/;
-		if ($psql_timeout->is_expired)
-		{
-			diag("aborting wait: program timed out");
-			diag("stream contents: >>", $$stream, "<<");
-			diag("pattern searched for: ", $untl);
-
-			return 0;
-		}
-		if (not $proc->pumpable())
-		{
-			diag("aborting wait: program died");
-			diag("stream contents: >>", $$stream, "<<");
-			diag("pattern searched for: ", $untl);
-
-			return 0;
-		}
-		$proc->pump();
-	}
-	return 1;
-
-}
-- 
1.8.3.1

