From f2af0cf2b60f3fd930718f5f52e216da14942f31 Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Sat, 15 Aug 2015 00:10:48 -0400
Subject: [PATCH 3/3] Add simple isolation tests for deadlock detection.

---
 src/test/isolation/expected/deadlock-2way.out | 11 +++++
 src/test/isolation/expected/deadlock-8way.out | 35 ++++++++++++++
 src/test/isolation/isolation_schedule         |  2 +
 src/test/isolation/specs/deadlock-2way.spec   | 24 ++++++++++
 src/test/isolation/specs/deadlock-8way.spec   | 66 +++++++++++++++++++++++++++
 5 files changed, 138 insertions(+)
 create mode 100644 src/test/isolation/expected/deadlock-2way.out
 create mode 100644 src/test/isolation/expected/deadlock-8way.out
 create mode 100644 src/test/isolation/specs/deadlock-2way.spec
 create mode 100644 src/test/isolation/specs/deadlock-8way.spec

diff --git a/src/test/isolation/expected/deadlock-2way.out b/src/test/isolation/expected/deadlock-2way.out
new file mode 100644
index 0000000..a429bdb
--- /dev/null
+++ b/src/test/isolation/expected/deadlock-2way.out
@@ -0,0 +1,11 @@
+Parsed test spec with 2 sessions
+
+starting permutation: s1a1 s2a2 s1a2 s2a1 s1c s2c
+step s1a1: LOCK TABLE a1;
+step s2a2: LOCK TABLE a2;
+step s1a2: LOCK TABLE a2; <waiting ...>
+step s2a1: LOCK TABLE a1;
+step s1a2: <... completed>
+error in steps s2a1 s1a2: ERROR:  deadlock detected
+step s1c: COMMIT;
+step s2c: COMMIT;
diff --git a/src/test/isolation/expected/deadlock-8way.out b/src/test/isolation/expected/deadlock-8way.out
new file mode 100644
index 0000000..cb25f75
--- /dev/null
+++ b/src/test/isolation/expected/deadlock-8way.out
@@ -0,0 +1,35 @@
+Parsed test spec with 8 sessions
+
+starting permutation: s1a1 s2a2 s3a3 s4a4 s5a5 s6a6 s7a7 s8a8 s1a2 s2a3 s3a4 s4a5 s5a6 s6a7 s7a8 s8a1 s8c s7c s6c s5c s4c s3c s2c s1c
+step s1a1: LOCK TABLE a1;
+step s2a2: LOCK TABLE a2;
+step s3a3: LOCK TABLE a3;
+step s4a4: LOCK TABLE a4;
+step s5a5: LOCK TABLE a5;
+step s6a6: LOCK TABLE a6;
+step s7a7: LOCK TABLE a7;
+step s8a8: LOCK TABLE a8;
+step s1a2: LOCK TABLE a2; <waiting ...>
+step s2a3: LOCK TABLE a3; <waiting ...>
+step s3a4: LOCK TABLE a4; <waiting ...>
+step s4a5: LOCK TABLE a5; <waiting ...>
+step s5a6: LOCK TABLE a6; <waiting ...>
+step s6a7: LOCK TABLE a7; <waiting ...>
+step s7a8: LOCK TABLE a8; <waiting ...>
+step s8a1: LOCK TABLE a1;
+step s7a8: <... completed>
+error in steps s8a1 s7a8: ERROR:  deadlock detected
+step s8c: COMMIT;
+step s7c: COMMIT;
+step s6a7: <... completed>
+step s6c: COMMIT;
+step s5a6: <... completed>
+step s5c: COMMIT;
+step s4a5: <... completed>
+step s4c: COMMIT;
+step s3a4: <... completed>
+step s3c: COMMIT;
+step s2a3: <... completed>
+step s2c: COMMIT;
+step s1a2: <... completed>
+step s1c: COMMIT;
diff --git a/src/test/isolation/isolation_schedule b/src/test/isolation/isolation_schedule
index c0ed637..e4c5a71 100644
--- a/src/test/isolation/isolation_schedule
+++ b/src/test/isolation/isolation_schedule
@@ -10,6 +10,8 @@ test: partial-index
 test: two-ids
 test: multiple-row-versions
 test: index-only-scan
+test: deadlock-2way
+test: deadlock-8way
 test: fk-contention
 test: fk-deadlock
 test: fk-deadlock2
diff --git a/src/test/isolation/specs/deadlock-2way.spec b/src/test/isolation/specs/deadlock-2way.spec
new file mode 100644
index 0000000..2398e59
--- /dev/null
+++ b/src/test/isolation/specs/deadlock-2way.spec
@@ -0,0 +1,24 @@
+setup
+{
+  CREATE TABLE a1 ();
+  CREATE TABLE a2 ();
+}
+
+teardown
+{
+  DROP TABLE a1, a2;
+}
+
+session "s1"
+setup		{ BEGIN; }
+step "s1a1"	{ LOCK TABLE a1; }
+step "s1a2"	{ LOCK TABLE a2; }
+step "s1c"	{ COMMIT; }
+
+session "s2"
+setup		{ BEGIN; }
+step "s2a1"	{ LOCK TABLE a1; }
+step "s2a2"	{ LOCK TABLE a2; }
+step "s2c"	{ COMMIT; }
+
+permutation "s1a1" "s2a2" "s1a2" "s2a1" "s1c" "s2c"
diff --git a/src/test/isolation/specs/deadlock-8way.spec b/src/test/isolation/specs/deadlock-8way.spec
new file mode 100644
index 0000000..7966d21
--- /dev/null
+++ b/src/test/isolation/specs/deadlock-8way.spec
@@ -0,0 +1,66 @@
+setup
+{
+  CREATE TABLE a1 ();
+  CREATE TABLE a2 ();
+  CREATE TABLE a3 ();
+  CREATE TABLE a4 ();
+  CREATE TABLE a5 ();
+  CREATE TABLE a6 ();
+  CREATE TABLE a7 ();
+  CREATE TABLE a8 ();
+}
+
+teardown
+{
+  DROP TABLE a1, a2, a3, a4, a5, a6, a7, a8;
+}
+
+session "s1"
+setup		{ BEGIN; SET deadlock_timeout = '10s'; }
+step "s1a1"	{ LOCK TABLE a1; }
+step "s1a2"	{ LOCK TABLE a2; }
+step "s1c"	{ COMMIT; }
+
+session "s2"
+setup		{ BEGIN; SET deadlock_timeout = '10s'; }
+step "s2a2"	{ LOCK TABLE a2; }
+step "s2a3"	{ LOCK TABLE a3; }
+step "s2c"	{ COMMIT; }
+
+session "s3"
+setup		{ BEGIN; SET deadlock_timeout = '10s'; }
+step "s3a3"	{ LOCK TABLE a3; }
+step "s3a4"	{ LOCK TABLE a4; }
+step "s3c"	{ COMMIT; }
+
+session "s4"
+setup		{ BEGIN; SET deadlock_timeout = '10s'; }
+step "s4a4"	{ LOCK TABLE a4; }
+step "s4a5"	{ LOCK TABLE a5; }
+step "s4c"	{ COMMIT; }
+
+session "s5"
+setup		{ BEGIN; SET deadlock_timeout = '10s'; }
+step "s5a5"	{ LOCK TABLE a5; }
+step "s5a6"	{ LOCK TABLE a6; }
+step "s5c"	{ COMMIT; }
+
+session "s6"
+setup		{ BEGIN; SET deadlock_timeout = '10s'; }
+step "s6a6"	{ LOCK TABLE a6; }
+step "s6a7"	{ LOCK TABLE a7; }
+step "s6c"	{ COMMIT; }
+
+session "s7"
+setup		{ BEGIN; SET deadlock_timeout = '10s'; }
+step "s7a7"	{ LOCK TABLE a7; }
+step "s7a8"	{ LOCK TABLE a8; }
+step "s7c"	{ COMMIT; }
+
+session "s8"
+setup		{ BEGIN; SET deadlock_timeout = '10ms'; }
+step "s8a8"	{ LOCK TABLE a8; }
+step "s8a1"	{ LOCK TABLE a1; }
+step "s8c"	{ COMMIT; }
+
+permutation "s1a1" "s2a2" "s3a3" "s4a4" "s5a5" "s6a6" "s7a7" "s8a8" "s1a2" "s2a3" "s3a4" "s4a5" "s5a6" "s6a7" "s7a8" "s8a1" "s8c" "s7c" "s6c" "s5c" "s4c" "s3c" "s2c" "s1c"
-- 
2.3.2 (Apple Git-55)

