From 0e3fb9cac2a131d49dcfb8d744a546772b374856 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Tue, 9 May 2017 23:35:31 -0400
Subject: [PATCH 1/3] Add isolation tests for sequences

---
 src/test/isolation/expected/sequence-ddl.out     | 37 ++++++++++++++++++++++++
 src/test/isolation/expected/sequence-nextval.out | 35 ++++++++++++++++++++++
 src/test/isolation/isolation_schedule            |  2 ++
 src/test/isolation/specs/sequence-ddl.spec       | 29 +++++++++++++++++++
 src/test/isolation/specs/sequence-nextval.spec   | 27 +++++++++++++++++
 5 files changed, 130 insertions(+)
 create mode 100644 src/test/isolation/expected/sequence-ddl.out
 create mode 100644 src/test/isolation/expected/sequence-nextval.out
 create mode 100644 src/test/isolation/specs/sequence-ddl.spec
 create mode 100644 src/test/isolation/specs/sequence-nextval.spec

diff --git a/src/test/isolation/expected/sequence-ddl.out b/src/test/isolation/expected/sequence-ddl.out
new file mode 100644
index 0000000000..ecd1f7204d
--- /dev/null
+++ b/src/test/isolation/expected/sequence-ddl.out
@@ -0,0 +1,37 @@
+Parsed test spec with 2 sessions
+
+starting permutation: s1alter s1commit s2nv
+step s1alter: ALTER SEQUENCE seq1 MAXVALUE 10;
+step s1commit: COMMIT;
+step s2nv: SELECT nextval('seq1') FROM generate_series(1, 15);
+ERROR:  nextval: reached maximum value of sequence "seq1" (10)
+
+starting permutation: s1alter s2nv s1commit
+step s1alter: ALTER SEQUENCE seq1 MAXVALUE 10;
+step s2nv: SELECT nextval('seq1') FROM generate_series(1, 15);
+ERROR:  nextval: reached maximum value of sequence "seq1" (10)
+step s1commit: COMMIT;
+
+starting permutation: s2begin s2nv s1alter2 s2commit s1commit
+step s2begin: BEGIN;
+step s2nv: SELECT nextval('seq1') FROM generate_series(1, 15);
+nextval        
+
+1              
+2              
+3              
+4              
+5              
+6              
+7              
+8              
+9              
+10             
+11             
+12             
+13             
+14             
+15             
+step s1alter2: ALTER SEQUENCE seq1 MAXVALUE 20;
+step s2commit: COMMIT;
+step s1commit: COMMIT;
diff --git a/src/test/isolation/expected/sequence-nextval.out b/src/test/isolation/expected/sequence-nextval.out
new file mode 100644
index 0000000000..861f0b0283
--- /dev/null
+++ b/src/test/isolation/expected/sequence-nextval.out
@@ -0,0 +1,35 @@
+Parsed test spec with 2 sessions
+
+starting permutation: s1a s1b s2a s2b s1c s1d s2c s2d
+step s1a: SELECT nextval('seq1');
+nextval        
+
+1              
+step s1b: SELECT nextval('seq1');
+nextval        
+
+2              
+step s2a: SELECT nextval('seq1');
+nextval        
+
+3              
+step s2b: SELECT nextval('seq1');
+nextval        
+
+4              
+step s1c: SELECT nextval('seq2');
+nextval        
+
+1              
+step s1d: SELECT nextval('seq2');
+nextval        
+
+2              
+step s2c: SELECT nextval('seq2');
+nextval        
+
+11             
+step s2d: SELECT nextval('seq2');
+nextval        
+
+12             
diff --git a/src/test/isolation/isolation_schedule b/src/test/isolation/isolation_schedule
index fc1918dedc..7dbbabd907 100644
--- a/src/test/isolation/isolation_schedule
+++ b/src/test/isolation/isolation_schedule
@@ -58,6 +58,8 @@ test: alter-table-1
 test: alter-table-2
 test: alter-table-3
 test: create-trigger
+test: sequence-nextval
+test: sequence-ddl
 test: async-notify
 test: vacuum-reltuples
 test: timeouts
diff --git a/src/test/isolation/specs/sequence-ddl.spec b/src/test/isolation/specs/sequence-ddl.spec
new file mode 100644
index 0000000000..9d1d438ead
--- /dev/null
+++ b/src/test/isolation/specs/sequence-ddl.spec
@@ -0,0 +1,29 @@
+# Test sequence usage and concurrent sequence DDL
+
+setup
+{
+    CREATE SEQUENCE seq1;
+}
+
+teardown
+{
+    DROP SEQUENCE seq1;
+}
+
+session "s1"
+setup           { BEGIN; }
+step "s1alter"  { ALTER SEQUENCE seq1 MAXVALUE 10; }
+step "s1alter2" { ALTER SEQUENCE seq1 MAXVALUE 20; }
+step "s1commit" { COMMIT; }
+
+session "s2"
+step "s2begin"  { BEGIN; }
+step "s2nv"     { SELECT nextval('seq1') FROM generate_series(1, 15); }
+step "s2commit" { COMMIT; }
+
+permutation "s1alter" "s1commit" "s2nv"
+
+# s2nv sees the uncommitted s1alter change.
+permutation "s1alter" "s2nv" "s1commit"
+
+permutation "s2begin" "s2nv" "s1alter2" "s2commit" "s1commit"
diff --git a/src/test/isolation/specs/sequence-nextval.spec b/src/test/isolation/specs/sequence-nextval.spec
new file mode 100644
index 0000000000..ad51d17d6b
--- /dev/null
+++ b/src/test/isolation/specs/sequence-nextval.spec
@@ -0,0 +1,27 @@
+# Test basic sequence behavior
+
+setup
+{
+    CREATE SEQUENCE seq1;
+    CREATE SEQUENCE seq2 CACHE 10;
+}
+
+teardown
+{
+    DROP SEQUENCE seq1;
+    DROP SEQUENCE seq2;
+}
+
+session "s1"
+step "s1a"      { SELECT nextval('seq1'); }
+step "s1b"      { SELECT nextval('seq1'); }
+step "s1c"      { SELECT nextval('seq2'); }
+step "s1d"      { SELECT nextval('seq2'); }
+
+session "s2"
+step "s2a"      { SELECT nextval('seq1'); }
+step "s2b"      { SELECT nextval('seq1'); }
+step "s2c"      { SELECT nextval('seq2'); }
+step "s2d"      { SELECT nextval('seq2'); }
+
+permutation "s1a" "s1b" "s2a" "s2b" "s1c" "s1d" "s2c" "s2d"
-- 
2.12.2

