From b7c21e95c9a48208f1aec377366b26e40cf9ecb4 Mon Sep 17 00:00:00 2001
From: Mark Dilger <mark.dilger@enterprisedb.com>
Date: Tue, 14 Sep 2021 15:18:52 -0700
Subject: [PATCH v34 09/10] Make isolationtester handle closed sessions.

The recent implementation of ALTER SYSTEM READ ONLY should be
testable from src/test/isolation.  For that, the isolation tester
needs to reconnect after getting kicked out rather than aborting.
---
 src/test/isolation/isolationtester.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/test/isolation/isolationtester.c b/src/test/isolation/isolationtester.c
index 88594a3cb5..87cf88cbec 100644
--- a/src/test/isolation/isolationtester.c
+++ b/src/test/isolation/isolationtester.c
@@ -85,6 +85,24 @@ disconnect_atexit(void)
 			PQfinish(conns[i].conn);
 }
 
+static void
+restore_connection(PGconn *conn)
+{
+	if (PQstatus(conn) != CONNECTION_BAD)
+		return;
+
+	fprintf(stderr,
+			_("The connection to the server was lost. Attempting reset: "));
+	PQreset(conn);
+	if (PQstatus(conn) == CONNECTION_BAD)
+	{
+		fprintf(stderr, _("Failed.\n"));
+		exit(1);
+	}
+	else
+		fprintf(stderr, _("Succeeded.\n"));
+}
+
 int
 main(int argc, char **argv)
 {
@@ -890,7 +908,7 @@ try_complete_step(TestSpec *testspec, PermutationStep *pstep, int flags)
 					{
 						fprintf(stderr, "PQconsumeInput failed: %s\n",
 								PQerrorMessage(conn));
-						exit(1);
+						restore_connection(conn);
 					}
 					if (!PQisBusy(conn))
 						break;
@@ -964,7 +982,7 @@ try_complete_step(TestSpec *testspec, PermutationStep *pstep, int flags)
 		{
 			fprintf(stderr, "PQconsumeInput failed: %s\n",
 					PQerrorMessage(conn));
-			exit(1);
+			restore_connection(conn);
 		}
 	}
 
@@ -1013,6 +1031,8 @@ try_complete_step(TestSpec *testspec, PermutationStep *pstep, int flags)
 						printf("%s:  %s\n", sev, msg);
 					else
 						printf("%s\n", PQresultErrorMessage(res));
+
+					restore_connection(conn);
 				}
 				break;
 			default:
-- 
2.21.1 (Apple Git-122.3)

