From c47a3b348c5818e341668b074582eef6b23376ef Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Thu, 20 Jul 2017 15:02:12 +0900
Subject: [PATCH 2/2] Add test for reconnection feature

This patch adds the regression test for the session reconnection
feature of postgres_fdw. make prove-check runs the test.
---
 contrib/postgres_fdw/Makefile              |  3 ++
 contrib/postgres_fdw/t/001_reconnection.pl | 65 ++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+)
 create mode 100644 contrib/postgres_fdw/t/001_reconnection.pl

diff --git a/contrib/postgres_fdw/Makefile b/contrib/postgres_fdw/Makefile
index 3543312..1be5c9f 100644
--- a/contrib/postgres_fdw/Makefile
+++ b/contrib/postgres_fdw/Makefile
@@ -23,3 +23,6 @@ top_builddir = ../..
 include $(top_builddir)/src/Makefile.global
 include $(top_srcdir)/contrib/contrib-global.mk
 endif
+
+prove-check:
+	$(prove_check)
diff --git a/contrib/postgres_fdw/t/001_reconnection.pl b/contrib/postgres_fdw/t/001_reconnection.pl
new file mode 100644
index 0000000..b93725d
--- /dev/null
+++ b/contrib/postgres_fdw/t/001_reconnection.pl
@@ -0,0 +1,65 @@
+# Minimal test testing reconnection
+use strict;
+use warnings;
+use PostgresNode;
+use PsqlSession;
+use TestLib;
+use Test::More tests => 6;
+
+# start a server
+my $server = get_new_node('server');
+$server->init();
+$server->start;
+my $session1 = get_new_session('session1', $server);
+my $session2 = get_new_session('session2', $server);
+$session1->open;
+$session2->open;
+
+my $ret = $session1->execsql("SELECT current_database()");
+ok($ret =~ /^.*\n-.*-\n *(\S+) *\n/,
+   'retrieving connection setting');
+my $dbname = $1;
+my $port = $server->port;
+
+$session1->execsql_multi(
+	("CREATE EXTENSION postgres_fdw;",
+	 "CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw OPTIONS (dbname \'$dbname\', port \'$port\');",
+	"CREATE USER MAPPING FOR CURRENT_USER SERVER loopback;",
+	"CREATE TABLE lt1 (c1 int);",
+	"INSERT INTO lt1 VALUES (1);",
+	"CREATE FOREIGN TABLE ft1 (c1 int) SERVER loopback OPTIONS (table_name 'lt1');",
+	 "SET client_min_messages to DEBUG3;"));
+ok($session1->errstate == 0, 'setting up');
+
+$session1->execsql("BEGIN;");
+
+my ($stout, $sterr) = $session1->execsql("SELECT c1 FROM ft1 LIMIT 1;");
+ok($sterr =~ /DEBUG: *new postgres_fdw connection 0x[[:xdigit:]]+/,
+   "creating new connection");
+
+$session2->execsql_multi(
+	"ALTER SERVER loopback OPTIONS (ADD host 'hoge')",
+	"ALTER SERVER loopback OPTIONS (DROP host)");
+
+($stout, $sterr) =  $session1->execsql("SELECT c1 FROM ft1 LIMIT 1;");
+ok($sterr !~ /DEBUG: *closing connection 0x[[:xdigit:]]+ for option changes to take effect/,
+   'no disconnection within a transaction');
+
+$session1->execsql("COMMIT;");
+
+$session2->execsql_multi(
+	"ALTER USER MAPPING ft1 OPTIONS (host 'hoge')",
+	"ALTER SERVER loopback OPTIONS (DROP host)");
+
+
+($stout, $sterr) = $session1->execsql("SELECT c1 FROM ft1 LIMIT 1;");
+ok($sterr =~ /DEBUG: *closing connection 0x[[:xdigit:]]+ for option changes to take effect\nDEBUG: *new postgres_fdw connection 0x[[:xdigit:]]+/,
+   'reconnection by option change outside a transactin');
+
+($stout, $sterr) =  $session1->execsql("SELECT c1 FROM ft1 LIMIT 1;");
+ok($sterr !~ /DEBUG: *closing connection 0x[[:xdigit:]]+ for option changes to take effect/,
+   'no disconnection without option change');
+
+$session1->close;
+$session2->close;
+$server->stop;
-- 
2.9.2

