From 78fa33f244c8e168d4e30c98430e5396b28e4246 Mon Sep 17 00:00:00 2001
From: "okbob@github.com" <pavel.stehule@gmail.com>
Date: Mon, 28 Aug 2023 14:57:07 +0200
Subject: [PATCH 2/4] PQlinkParameterStatus, PQunlinkParameterStatus test based
 on libpq_pipeline test

---
 .../modules/libpq_pipeline/libpq_pipeline.c   | 69 +++++++++++++++++++
 .../libpq_pipeline/t/001_libpq_pipeline.pl    |  2 +-
 .../libpq_pipeline/traces/reportguc.trace     | 30 ++++++++
 3 files changed, 100 insertions(+), 1 deletion(-)
 create mode 100644 src/test/modules/libpq_pipeline/traces/reportguc.trace

diff --git a/src/test/modules/libpq_pipeline/libpq_pipeline.c b/src/test/modules/libpq_pipeline/libpq_pipeline.c
index 9907bc8600..d192e75cbb 100644
--- a/src/test/modules/libpq_pipeline/libpq_pipeline.c
+++ b/src/test/modules/libpq_pipeline/libpq_pipeline.c
@@ -1677,6 +1677,72 @@ test_uniqviol(PGconn *conn)
 	fprintf(stderr, "ok\n");
 }
 
+/*
+ * Test of ReportGUC message
+ */
+static void
+test_reportguc(PGconn *conn)
+{
+	PGresult   *res;
+	const char *param;
+	const char *errmsg;
+
+	fprintf(stderr, "reportguc ...");
+
+	res = PQexec(conn, "SELECT pg_catalog.set_config('test.test', 'Hello', false)");
+	if (PQresultStatus(res) != PGRES_TUPLES_OK)
+		pg_fatal("failed to create custom config variable: %s", PQerrorMessage(conn));
+	PQclear(res);
+
+	/* we should to see in protocol so variable is reported */
+	res = PQlinkParameterStatus(conn, "test.test");
+	if (PQresultStatus(res) != PGRES_COMMAND_OK)
+		pg_fatal("failed to link custom variable: %s", PQerrorMessage(conn));
+	PQclear(res);
+
+	param = PQparameterStatus(conn, "test.test");
+	if (!param || strcmp(param, "Hello") != 0)
+		pg_fatal("the parameter has not expected value");
+
+	/* we should to see in protocol so variable is reported */
+	res = PQexec(conn, "SELECT pg_catalog.set_config('test.test', 'Bonjour', false)");
+	if (PQresultStatus(res) != PGRES_TUPLES_OK)
+		pg_fatal("failed to create custom config variable: %s", PQerrorMessage(conn));
+	PQclear(res);
+
+	param = PQparameterStatus(conn, "test.test");
+	if (!param || strcmp(param, "Bonjour") != 0)
+		pg_fatal("the parameter has not expected value");
+
+	res = PQunlinkParameterStatus(conn, "test.test");
+	if (PQresultStatus(res) != PGRES_COMMAND_OK)
+		pg_fatal("failed to unlink custom variable: %s", PQerrorMessage(conn));
+	PQclear(res);
+
+	/* we should to get error when reported variable doesn't exists */
+	res = PQlinkParameterStatus(conn, "not_exists_variable");
+	if (PQresultStatus(res) != PGRES_FATAL_ERROR)
+		pg_fatal("unexpected status: %s", PQresStatus(PQresultStatus(res)));
+
+	/* the error message should not be empty */
+	errmsg = PQerrorMessage(conn);
+	if (!errmsg || *errmsg == '\0')
+		pg_fatal("missing error message");
+	PQclear(res);
+
+	/* now, this change should not be reported */
+	res = PQexec(conn, "SELECT pg_catalog.set_config('test.test', 'Ahoj', false)");
+	if (PQresultStatus(res) != PGRES_TUPLES_OK)
+		pg_fatal("failed to create custom config variable: %s", PQerrorMessage(conn));
+	PQclear(res);
+
+	param = PQparameterStatus(conn, "test.test");
+	if (!param || strcmp(param, "Bonjour") != 0)
+		pg_fatal("the parameter has not expected value");
+
+	fprintf(stderr, "ok\n");
+}
+
 /*
  * Subroutine for test_uniqviol; given a PGresult, print it out and consume
  * the expected NULL that should follow it.
@@ -1757,6 +1823,7 @@ print_test_list(void)
 	printf("singlerow\n");
 	printf("transaction\n");
 	printf("uniqviol\n");
+	printf("reportguc\n");
 }
 
 int
@@ -1869,6 +1936,8 @@ main(int argc, char **argv)
 		test_transaction(conn);
 	else if (strcmp(testname, "uniqviol") == 0)
 		test_uniqviol(conn);
+	else if (strcmp(testname, "reportguc") == 0)
+		test_reportguc(conn);
 	else
 	{
 		fprintf(stderr, "\"%s\" is not a recognized test name\n", testname);
diff --git a/src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl b/src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl
index 056fa5c6d2..08f6163b64 100644
--- a/src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl
+++ b/src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl
@@ -38,7 +38,7 @@ for my $testname (@tests)
 	my @extraargs = ('-r', $numrows);
 	my $cmptrace = grep(/^$testname$/,
 		qw(simple_pipeline nosync multi_pipelines prepared singlerow
-		  pipeline_abort pipeline_idle transaction
+		  pipeline_abort pipeline_idle transaction reportguc
 		  disallowed_in_pipeline)) > 0;
 
 	# For a bunch of tests, generate a libpq trace file too.
diff --git a/src/test/modules/libpq_pipeline/traces/reportguc.trace b/src/test/modules/libpq_pipeline/traces/reportguc.trace
new file mode 100644
index 0000000000..1076f5389c
--- /dev/null
+++ b/src/test/modules/libpq_pipeline/traces/reportguc.trace
@@ -0,0 +1,30 @@
+F	62	Query	 "SELECT pg_catalog.set_config('test.test', 'Hello', false)"
+B	35	RowDescription	 1 "set_config" NNNN 0 NNNN 65535 -1 0
+B	15	DataRow	 1 5 'Hello'
+B	13	CommandComplete	 "SELECT 1"
+B	5	ReadyForQuery	 I
+F	15	ReportGUC	 t "test.test"
+F	4	Sync
+B	20	ParameterStatus	 "test.test" "Hello"
+B	8	CommandComplete	 "SET"
+B	5	ReadyForQuery	 I
+F	64	Query	 "SELECT pg_catalog.set_config('test.test', 'Bonjour', false)"
+B	35	RowDescription	 1 "set_config" NNNN 0 NNNN 65535 -1 0
+B	17	DataRow	 1 7 'Bonjour'
+B	13	CommandComplete	 "SELECT 1"
+B	22	ParameterStatus	 "test.test" "Bonjour"
+B	5	ReadyForQuery	 I
+F	15	ReportGUC	 f "test.test"
+F	4	Sync
+B	10	CommandComplete	 "UNSET"
+B	5	ReadyForQuery	 I
+F	25	ReportGUC	 t "not_exists_variable"
+F	4	Sync
+B	NN	ErrorResponse	 S "ERROR" V "ERROR" C "42704" M "unrecognized configuration parameter "not_exists_variable"" F "SSSS" L "SSSS" R "SSSS" \x00
+B	5	ReadyForQuery	 I
+F	61	Query	 "SELECT pg_catalog.set_config('test.test', 'Ahoj', false)"
+B	35	RowDescription	 1 "set_config" NNNN 0 NNNN 65535 -1 0
+B	14	DataRow	 1 4 'Ahoj'
+B	13	CommandComplete	 "SELECT 1"
+B	5	ReadyForQuery	 I
+F	4	Terminate
-- 
2.41.0

