From d040e90b1ed206a14b031ac92f494b87b1d32ae8 Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Sat, 27 Mar 2021 16:10:08 -0300
Subject: [PATCH v30 7/7] Use libpq_pipeline to test PQtrace

---
 .../libpq_pipeline/t/001_libpq_pipeline.pl    | 24 ++++++++++-
 .../traces/multi_pipelines.trace              | 26 ++++++++++++
 .../libpq_pipeline/traces/prepared.trace      | 21 ++++++++++
 .../traces/simple_pipeline.trace              | 15 +++++++
 .../libpq_pipeline/traces/singlerow.trace     | 42 +++++++++++++++++++
 5 files changed, 126 insertions(+), 2 deletions(-)
 create mode 100644 src/test/modules/libpq_pipeline/traces/multi_pipelines.trace
 create mode 100644 src/test/modules/libpq_pipeline/traces/prepared.trace
 create mode 100644 src/test/modules/libpq_pipeline/traces/simple_pipeline.trace
 create mode 100644 src/test/modules/libpq_pipeline/traces/singlerow.trace

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 0213f21ee8..a0ee8640a7 100644
--- a/src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl
+++ b/src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl
@@ -4,7 +4,7 @@ use warnings;
 use Config;
 use PostgresNode;
 use TestLib;
-use Test::More tests => 8;
+use Test::More tests => 12;
 use Cwd;
 
 my $node = get_new_node('main');
@@ -20,9 +20,29 @@ my @tests = split(/\s+/, $out);
 
 for my $testname (@tests)
 {
+	my @extraargs = ();
+	my $cmptrace = grep(/^$testname$/,
+		qw(simple_pipeline multi_pipelines prepared singlerow)) > 0;
+	my $traceout = "$TestLib::log_path/$testname.trace";
+
+	# For a bunch of tests, generate a libpq trace file too.
+	if ($cmptrace)
+	{
+		push @extraargs, "-t", $traceout;
+	}
+
+
 	$node->command_ok(
-		[ 'libpq_pipeline', $testname, $node->connstr('postgres'), $numrows ],
+		[ 'libpq_pipeline', @extraargs, $testname, $node->connstr('postgres'), $numrows ],
 		"libpq_pipeline $testname");
+
+	if ($cmptrace)
+	{
+		my $expected = slurp_file("traces/$testname.trace");
+		my $result = slurp_file($traceout);
+
+		is($expected, $result, "$testname trace match");
+	}
 }
 
 $node->stop('fast');
diff --git a/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace b/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace
new file mode 100644
index 0000000000..f7dfbef11f
--- /dev/null
+++ b/src/test/modules/libpq_pipeline/traces/multi_pipelines.trace
@@ -0,0 +1,26 @@
+F	27	Query	 "SET lc_messages TO "C""
+B	8	CommandComplete	 "SET"
+B	5	ReadyForQuery	 I
+F	21	Parse	 "" "SELECT $1" 1 23
+F	19	Bind	 "" "" 0 1 1 '1' 1 0
+F	6	Describe	 P ""
+F	9	Execute	 "" 0
+F	4	Sync
+F	21	Parse	 "" "SELECT $1" 1 23
+F	19	Bind	 "" "" 0 1 1 '1' 1 0
+F	6	Describe	 P ""
+F	9	Execute	 "" 0
+F	4	Sync
+B	4	ParseComplete
+B	4	BindComplete
+B	33	RowDescription	 1 "?column?" 0 0 23 4 -1 0
+B	11	DataRow	 1 1 '1'
+B	13	CommandComplete	 "SELECT 1"
+B	5	ReadyForQuery	 I
+B	4	ParseComplete
+B	4	BindComplete
+B	33	RowDescription	 1 "?column?" 0 0 23 4 -1 0
+B	11	DataRow	 1 1 '1'
+B	13	CommandComplete	 "SELECT 1"
+B	5	ReadyForQuery	 I
+F	4	Terminate
diff --git a/src/test/modules/libpq_pipeline/traces/prepared.trace b/src/test/modules/libpq_pipeline/traces/prepared.trace
new file mode 100644
index 0000000000..62c5c38b6a
--- /dev/null
+++ b/src/test/modules/libpq_pipeline/traces/prepared.trace
@@ -0,0 +1,21 @@
+F	27	Query	 "SET lc_messages TO "C""
+B	8	CommandComplete	 "SET"
+B	5	ReadyForQuery	 I
+F	68	Parse	 "select_one" "SELECT $1, '42', $1::numeric, interval '1 sec'" 1 23
+F	16	Describe	 S "select_one"
+F	4	Sync
+B	4	ParseComplete
+B	10	ParameterDescription	 1 23
+B	113	RowDescription	 4 "?column?" 0 0 23 4 -1 0 "?column?" 0 0 25 65535 -1 0 "numeric" 0 0 1700 65535 -1 0 "interval" 0 0 1186 16 -1 0
+B	5	ReadyForQuery	 I
+F	10	Query	 "BEGIN"
+B	10	CommandComplete	 "BEGIN"
+B	5	ReadyForQuery	 T
+F	43	Query	 "DECLARE cursor_one CURSOR FOR SELECT 1"
+B	19	CommandComplete	 "DECLARE CURSOR"
+B	5	ReadyForQuery	 T
+F	16	Describe	 P "cursor_one"
+F	4	Sync
+B	33	RowDescription	 1 "?column?" 0 0 23 4 -1 0
+B	5	ReadyForQuery	 T
+F	4	Terminate
diff --git a/src/test/modules/libpq_pipeline/traces/simple_pipeline.trace b/src/test/modules/libpq_pipeline/traces/simple_pipeline.trace
new file mode 100644
index 0000000000..82a80c9191
--- /dev/null
+++ b/src/test/modules/libpq_pipeline/traces/simple_pipeline.trace
@@ -0,0 +1,15 @@
+F	27	Query	 "SET lc_messages TO "C""
+B	8	CommandComplete	 "SET"
+B	5	ReadyForQuery	 I
+F	21	Parse	 "" "SELECT $1" 1 23
+F	19	Bind	 "" "" 0 1 1 '1' 1 0
+F	6	Describe	 P ""
+F	9	Execute	 "" 0
+F	4	Sync
+B	4	ParseComplete
+B	4	BindComplete
+B	33	RowDescription	 1 "?column?" 0 0 23 4 -1 0
+B	11	DataRow	 1 1 '1'
+B	13	CommandComplete	 "SELECT 1"
+B	5	ReadyForQuery	 I
+F	4	Terminate
diff --git a/src/test/modules/libpq_pipeline/traces/singlerow.trace b/src/test/modules/libpq_pipeline/traces/singlerow.trace
new file mode 100644
index 0000000000..7708569f39
--- /dev/null
+++ b/src/test/modules/libpq_pipeline/traces/singlerow.trace
@@ -0,0 +1,42 @@
+F	27	Query	 "SET lc_messages TO "C""
+B	8	CommandComplete	 "SET"
+B	5	ReadyForQuery	 I
+F	38	Parse	 "" "SELECT generate_series(42, $1)" 0
+F	20	Bind	 "" "" 0 1 2 '44' 1 0
+F	6	Describe	 P ""
+F	9	Execute	 "" 0
+F	38	Parse	 "" "SELECT generate_series(42, $1)" 0
+F	20	Bind	 "" "" 0 1 2 '45' 1 0
+F	6	Describe	 P ""
+F	9	Execute	 "" 0
+F	38	Parse	 "" "SELECT generate_series(42, $1)" 0
+F	20	Bind	 "" "" 0 1 2 '46' 1 0
+F	6	Describe	 P ""
+F	9	Execute	 "" 0
+F	4	Sync
+B	4	ParseComplete
+B	4	BindComplete
+B	40	RowDescription	 1 "generate_series" 0 0 23 4 -1 0
+B	12	DataRow	 1 2 '42'
+B	12	DataRow	 1 2 '43'
+B	12	DataRow	 1 2 '44'
+B	13	CommandComplete	 "SELECT 3"
+B	4	ParseComplete
+B	4	BindComplete
+B	40	RowDescription	 1 "generate_series" 0 0 23 4 -1 0
+B	12	DataRow	 1 2 '42'
+B	12	DataRow	 1 2 '43'
+B	12	DataRow	 1 2 '44'
+B	12	DataRow	 1 2 '45'
+B	13	CommandComplete	 "SELECT 4"
+B	4	ParseComplete
+B	4	BindComplete
+B	40	RowDescription	 1 "generate_series" 0 0 23 4 -1 0
+B	12	DataRow	 1 2 '42'
+B	12	DataRow	 1 2 '43'
+B	12	DataRow	 1 2 '44'
+B	12	DataRow	 1 2 '45'
+B	12	DataRow	 1 2 '46'
+B	13	CommandComplete	 "SELECT 5"
+B	5	ReadyForQuery	 I
+F	4	Terminate
-- 
2.20.1

