From 615c1bb798445886c54ece062db397146d7038fe Mon Sep 17 00:00:00 2001
From: "Andrey M. Borodin" <x4mmm@night.local>
Date: Wed, 21 Feb 2024 19:34:26 +0300
Subject: [PATCH 4/4] Add timeouts TAP tests

These tests verify that transaction_timeout, idle_session_timeout
and idle_in_transaction_session_timeout work as expected.
---
 src/backend/tcop/postgres.c                  |  10 ++
 src/test/modules/test_misc/Makefile          |   4 +
 src/test/modules/test_misc/meson.build       |   4 +
 src/test/modules/test_misc/t/005_timeouts.pl | 104 +++++++++++++++++++
 4 files changed, 122 insertions(+)
 create mode 100644 src/test/modules/test_misc/t/005_timeouts.pl

diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 2c63b7875a..7a9f650397 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -72,6 +72,7 @@
 #include "tcop/tcopprot.h"
 #include "tcop/utility.h"
 #include "utils/guc_hooks.h"
+#include "utils/injection_point.h"
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
 #include "utils/ps_status.h"
@@ -3411,9 +3412,12 @@ ProcessInterrupts(void)
 		 * interrupt.
 		 */
 		if (IdleInTransactionSessionTimeout > 0)
+		{
+			INJECTION_POINT("IdleInTransactionSessionTimeout");
 			ereport(FATAL,
 					(errcode(ERRCODE_IDLE_IN_TRANSACTION_SESSION_TIMEOUT),
 					 errmsg("terminating connection due to idle-in-transaction timeout")));
+		}
 		else
 			IdleInTransactionSessionTimeoutPending = false;
 	}
@@ -3422,9 +3426,12 @@ ProcessInterrupts(void)
 	{
 		/* As above, ignore the signal if the GUC has been reset to zero. */
 		if (TransactionTimeout > 0)
+		{
+			INJECTION_POINT("TransactionTimeout");
 			ereport(FATAL,
 					(errcode(ERRCODE_TRANSACTION_TIMEOUT),
 					 errmsg("terminating connection due to transaction timeout")));
+		}
 		else
 			TransactionTimeoutPending = false;
 	}
@@ -3433,9 +3440,12 @@ ProcessInterrupts(void)
 	{
 		/* As above, ignore the signal if the GUC has been reset to zero. */
 		if (IdleSessionTimeout > 0)
+		{
+			INJECTION_POINT("IdleSessionTimeout");
 			ereport(FATAL,
 					(errcode(ERRCODE_IDLE_SESSION_TIMEOUT),
 					 errmsg("terminating connection due to idle-session timeout")));
+		}
 		else
 			IdleSessionTimeoutPending = false;
 	}
diff --git a/src/test/modules/test_misc/Makefile b/src/test/modules/test_misc/Makefile
index 39c6c2014a..a958d156f4 100644
--- a/src/test/modules/test_misc/Makefile
+++ b/src/test/modules/test_misc/Makefile
@@ -2,6 +2,10 @@
 
 TAP_TESTS = 1
 
+EXTRA_INSTALL=src/test/modules/injection_points
+
+export enable_injection_points enable_injection_points
+
 ifdef USE_PGXS
 PG_CONFIG = pg_config
 PGXS := $(shell $(PG_CONFIG) --pgxs)
diff --git a/src/test/modules/test_misc/meson.build b/src/test/modules/test_misc/meson.build
index 964d95db26..df2913e893 100644
--- a/src/test/modules/test_misc/meson.build
+++ b/src/test/modules/test_misc/meson.build
@@ -5,11 +5,15 @@ tests += {
   'sd': meson.current_source_dir(),
   'bd': meson.current_build_dir(),
   'tap': {
+    'env': {
+       'enable_injection_points': get_option('injection_points') ? 'yes' : 'no',
+    },
     'tests': [
       't/001_constraint_validation.pl',
       't/002_tablespace.pl',
       't/003_check_guc.pl',
       't/004_io_direct.pl',
+      't/005_timeouts.pl'
     ],
   },
 }
diff --git a/src/test/modules/test_misc/t/005_timeouts.pl b/src/test/modules/test_misc/t/005_timeouts.pl
new file mode 100644
index 0000000000..5d940e4441
--- /dev/null
+++ b/src/test/modules/test_misc/t/005_timeouts.pl
@@ -0,0 +1,104 @@
+
+# Copyright (c) 2024, PostgreSQL Global Development Group
+
+use strict;
+use warnings;
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+# Test timeouts that will FATAL-out.
+# This test relies on an injection points to await timeout ocurance.
+# Relying on sleep prooved to be unstable on buildfarm.
+# It's difficult to rely on NOTICE injection point, because FATALed
+# backend can look differently under different circumstances.
+
+if ($ENV{enable_injection_points} ne 'yes')
+{
+	plan skip_all => 'Injection points not supported by this build';
+}
+
+# Initialize primary node
+my $node = PostgreSQL::Test::Cluster->new('master');
+$node->init();
+$node->start;
+
+$node->safe_psql('postgres', 'CREATE EXTENSION injection_points;');
+
+$node->safe_psql('postgres',
+	"SELECT injection_points_attach('TransactionTimeout', 'wait');");
+
+my $psql_session =
+  $node->background_psql('postgres', on_error_stop => 0);
+$psql_session->query_until(
+	qr/starting_bg_psql/, q(
+   \echo starting_bg_psql
+   SET transaction_timeout to '1ms';
+   BEGIN;
+   $$
+));
+
+# Wait until the backend is in the timeout.
+ok( $node->poll_query_until(
+		'postgres',
+		qq[SELECT count(*) FROM pg_stat_activity
+           WHERE wait_event = 'TransactionTimeout' ;],
+		'1'),
+	'backend is waiting in transaction timeout'
+) or die "Timed out while waiting for transaction timeout";
+
+$node->safe_psql('postgres',
+	"SELECT injection_points_wakeup('TransactionTimeout');");
+$psql_session->quit;
+
+$node->safe_psql('postgres',
+	"SELECT injection_points_attach('IdleInTransactionSessionTimeout', 'wait');");
+
+$psql_session =
+  $node->background_psql('postgres', on_error_stop => 0);
+$psql_session->query_until(
+	qr/starting_bg_psql/, q(
+   \echo starting_bg_psql
+   SET idle_in_transaction_session_timeout to '10ms';
+   BEGIN;
+));
+
+# Wait until the backend is in the timeout.
+ok( $node->poll_query_until(
+		'postgres',
+		qq[SELECT count(*) FROM pg_stat_activity
+           WHERE wait_event = 'IdleInTransactionSessionTimeout' ;],
+		'1'),
+	'backend is waiting in idle in transaction session timeout'
+) or die "Timed out while waiting for idleness timeout";
+
+$node->safe_psql('postgres',
+	"SELECT injection_points_wakeup('IdleInTransactionSessionTimeout');");
+$psql_session->quit;
+
+$node->safe_psql('postgres',
+	"SELECT injection_points_attach('IdleSessionTimeout', 'wait');");
+
+$psql_session =
+  $node->background_psql('postgres', on_error_stop => 0);
+$psql_session->query_until(
+	qr/starting_bg_psql/, q(
+   \echo starting_bg_psql
+   SET idle_session_timeout to '10ms';
+));
+
+# Wait until the backend is in the timeout.
+ok( $node->poll_query_until(
+		'postgres',
+		qq[SELECT count(*) FROM pg_stat_activity
+           WHERE wait_event = 'IdleSessionTimeout' ;],
+		'1'),
+	'backend is waiting in idle session timeout'
+) or die "Timed out while waiting for idleness timeout";
+
+$node->safe_psql('postgres',
+	"SELECT injection_points_wakeup('IdleSessionTimeout');");
+$psql_session->quit;
+
+
+done_testing();
-- 
2.37.1 (Apple Git-137.1)

