From 5572c8468059a10dac58e74f7961528ab68f1013 Mon Sep 17 00:00:00 2001
From: "kuroda.hayato%40jp.fujitsu.com" <kuroda.hayato@jp.fujitsu.com>
Date: Wed, 21 Sep 2022 06:52:23 +0000
Subject: [PATCH v15 4/4] add test

---
 .../postgres_fdw/expected/postgres_fdw.out    | 36 +++++++++++++++++++
 contrib/postgres_fdw/sql/postgres_fdw.sql     | 26 ++++++++++++++
 2 files changed, 62 insertions(+)

diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out
index 2e4e82a94f..476e4966cc 100644
--- a/contrib/postgres_fdw/expected/postgres_fdw.out
+++ b/contrib/postgres_fdw/expected/postgres_fdw.out
@@ -11463,3 +11463,39 @@ SELECT * FROM prem2;
 
 ALTER SERVER loopback OPTIONS (DROP parallel_commit);
 ALTER SERVER loopback2 OPTIONS (DROP parallel_commit);
+-- ===================================================================
+-- test for health-check feature
+-- ===================================================================
+-- Disable debug_discard_caches in order to manage remote connections
+SET debug_discard_caches TO '0';
+-- Disconnect once and set application_name to an arbitrary value
+SELECT 1 FROM postgres_fdw_disconnect_all();
+ ?column? 
+----------
+        1
+(1 row)
+
+ALTER SERVER loopback OPTIONS (SET application_name 'healthcheck');
+-- Set GUC for checking the health of remote servers
+SET postgres_fdw.health_check_interval TO '1s';
+BEGIN;
+SELECT 1 FROM ft1 LIMIT 1;
+ ?column? 
+----------
+        1
+(1 row)
+
+-- Terminate the remote backend process
+SELECT pg_terminate_backend(pid, 180000) FROM pg_stat_activity
+       WHERE application_name = 'healthcheck';
+ pg_terminate_backend 
+----------------------
+ t
+(1 row)
+
+-- While sleeping the process down will be detected.
+SELECT pg_sleep(3);
+ERROR:  Foreign Server loopback might be down.
+COMMIT;
+-- Clean up
+RESET debug_discard_caches;
diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql
index e48ccd286b..1e1f30fc6c 100644
--- a/contrib/postgres_fdw/sql/postgres_fdw.sql
+++ b/contrib/postgres_fdw/sql/postgres_fdw.sql
@@ -3682,3 +3682,29 @@ SELECT * FROM prem2;
 
 ALTER SERVER loopback OPTIONS (DROP parallel_commit);
 ALTER SERVER loopback2 OPTIONS (DROP parallel_commit);
+
+-- ===================================================================
+-- test for health-check feature
+-- ===================================================================
+
+-- Disable debug_discard_caches in order to manage remote connections
+SET debug_discard_caches TO '0';
+
+-- Disconnect once and set application_name to an arbitrary value
+SELECT 1 FROM postgres_fdw_disconnect_all();
+ALTER SERVER loopback OPTIONS (SET application_name 'healthcheck');
+
+-- Set GUC for checking the health of remote servers
+SET postgres_fdw.health_check_interval TO '1s';
+
+BEGIN;
+SELECT 1 FROM ft1 LIMIT 1;
+-- Terminate the remote backend process
+SELECT pg_terminate_backend(pid, 180000) FROM pg_stat_activity
+       WHERE application_name = 'healthcheck';
+-- While sleeping the process down will be detected.
+SELECT pg_sleep(3);
+COMMIT;
+
+-- Clean up
+RESET debug_discard_caches;
-- 
2.27.0

