>From 2e63d0fece895eb7a662788fc3d4b3839cf29321 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Wed, 24 Jul 2013 19:34:29 +0200
Subject: [PATCH 2/2] Add regression tests for background workers via
 worker_spi.

---
 contrib/worker_spi/Makefile                |   6 +
 contrib/worker_spi/expected/worker_spi.out | 216 +++++++++++++++++++++++++++++
 contrib/worker_spi/sql/worker_spi.sql      |  44 ++++++
 3 files changed, 266 insertions(+)
 create mode 100644 contrib/worker_spi/expected/worker_spi.out
 create mode 100644 contrib/worker_spi/sql/worker_spi.sql

diff --git a/contrib/worker_spi/Makefile b/contrib/worker_spi/Makefile
index fbb29b4..fcacf30 100644
--- a/contrib/worker_spi/Makefile
+++ b/contrib/worker_spi/Makefile
@@ -5,6 +5,8 @@ MODULES = worker_spi
 EXTENSION = worker_spi
 DATA = worker_spi--1.0.sql
 
+REGRESS = worker_spi
+
 ifdef USE_PGXS
 PG_CONFIG = pg_config
 PGXS := $(shell $(PG_CONFIG) --pgxs)
@@ -15,3 +17,7 @@ top_builddir = ../..
 include $(top_builddir)/src/Makefile.global
 include $(top_srcdir)/contrib/contrib-global.mk
 endif
+
+# Disabled, so we don't steal worker slots from a running instance.
+# Also, the tests depend on some default configuration variables.
+installcheck:;
diff --git a/contrib/worker_spi/expected/worker_spi.out b/contrib/worker_spi/expected/worker_spi.out
new file mode 100644
index 0000000..e11f453
--- /dev/null
+++ b/contrib/worker_spi/expected/worker_spi.out
@@ -0,0 +1,216 @@
+-- hardcoded in worker_spi atm
+\c postgres
+CREATE EXTENSION worker_spi;
+-- default's still the same
+SHOW max_worker_processes;
+ max_worker_processes 
+----------------------
+ 8
+(1 row)
+
+SELECT worker_spi_launch(1);
+ worker_spi_launch 
+-------------------
+ t
+(1 row)
+
+SELECT worker_spi_launch(2);
+ worker_spi_launch 
+-------------------
+ t
+(1 row)
+
+SELECT worker_spi_launch(3);
+ worker_spi_launch 
+-------------------
+ t
+(1 row)
+
+SELECT worker_spi_launch(4);
+ worker_spi_launch 
+-------------------
+ t
+(1 row)
+
+SELECT worker_spi_launch(5);
+ worker_spi_launch 
+-------------------
+ t
+(1 row)
+
+SELECT worker_spi_launch(6);
+ worker_spi_launch 
+-------------------
+ t
+(1 row)
+
+SELECT worker_spi_launch(7);
+ worker_spi_launch 
+-------------------
+ t
+(1 row)
+
+SELECT worker_spi_launch(8);
+ worker_spi_launch 
+-------------------
+ t
+(1 row)
+
+-- should fail
+SELECT worker_spi_launch(9);
+ worker_spi_launch 
+-------------------
+ f
+(1 row)
+
+-- wait till it started
+SELECT pg_sleep(3);
+ pg_sleep 
+----------
+ 
+(1 row)
+
+SELECT count(*) FROM pg_stat_activity WHERE application_name NOT IN ('psql', 'pg_regress');
+ count 
+-------
+     8
+(1 row)
+
+\dn
+ List of schemas
+  Name   | Owner  
+---------+--------
+ public  | andres
+ schema1 | andres
+ schema2 | andres
+ schema3 | andres
+ schema4 | andres
+ schema5 | andres
+ schema6 | andres
+ schema7 | andres
+ schema8 | andres
+(9 rows)
+
+\dt schema*.*
+         List of relations
+ Schema  |  Name   | Type  | Owner  
+---------+---------+-------+--------
+ schema1 | counted | table | andres
+ schema2 | counted | table | andres
+ schema3 | counted | table | andres
+ schema4 | counted | table | andres
+ schema5 | counted | table | andres
+ schema6 | counted | table | andres
+ schema7 | counted | table | andres
+ schema8 | counted | table | andres
+(8 rows)
+
+SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE application_name NOT IN ('psql', 'pg_regress');
+ pg_terminate_backend 
+----------------------
+ t
+ t
+ t
+ t
+ t
+ t
+ t
+ t
+(8 rows)
+
+SELECT pg_sleep(1);
+ pg_sleep 
+----------
+ 
+(1 row)
+
+SELECT count(*) FROM pg_stat_activity WHERE application_name NOT IN ('psql', 'pg_regress');
+ count 
+-------
+     0
+(1 row)
+
+-- can start after crash
+SELECT worker_spi_launch(10);
+ worker_spi_launch 
+-------------------
+ t
+(1 row)
+
+SELECT worker_spi_launch(11);
+ worker_spi_launch 
+-------------------
+ t
+(1 row)
+
+-- wait till started
+SELECT pg_sleep(3);
+ pg_sleep 
+----------
+ 
+(1 row)
+
+SELECT count(*) FROM pg_stat_activity WHERE application_name NOT IN ('psql', 'pg_regress');
+ count 
+-------
+     2
+(1 row)
+
+SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE application_name NOT IN ('psql', 'pg_regress');
+ pg_terminate_backend 
+----------------------
+ t
+ t
+(2 rows)
+
+SELECT pg_sleep(3);
+ pg_sleep 
+----------
+ 
+(1 row)
+
+SELECT count(*) FROM pg_stat_activity WHERE application_name NOT IN ('psql', 'pg_regress');
+ count 
+-------
+     0
+(1 row)
+
+SELECT pg_sleep(3);
+ pg_sleep 
+----------
+ 
+(1 row)
+
+\dn
+  List of schemas
+   Name   | Owner  
+----------+--------
+ public   | andres
+ schema1  | andres
+ schema10 | andres
+ schema11 | andres
+ schema2  | andres
+ schema3  | andres
+ schema4  | andres
+ schema5  | andres
+ schema6  | andres
+ schema7  | andres
+ schema8  | andres
+(11 rows)
+
+\dt schema*.*
+          List of relations
+  Schema  |  Name   | Type  | Owner  
+----------+---------+-------+--------
+ schema1  | counted | table | andres
+ schema10 | counted | table | andres
+ schema11 | counted | table | andres
+ schema2  | counted | table | andres
+ schema3  | counted | table | andres
+ schema4  | counted | table | andres
+ schema5  | counted | table | andres
+ schema6  | counted | table | andres
+ schema7  | counted | table | andres
+ schema8  | counted | table | andres
+(10 rows)
+
diff --git a/contrib/worker_spi/sql/worker_spi.sql b/contrib/worker_spi/sql/worker_spi.sql
new file mode 100644
index 0000000..41664a2
--- /dev/null
+++ b/contrib/worker_spi/sql/worker_spi.sql
@@ -0,0 +1,44 @@
+-- hardcoded in worker_spi atm
+\c postgres
+
+CREATE EXTENSION worker_spi;
+
+-- default's still the same
+SHOW max_worker_processes;
+
+SELECT worker_spi_launch(1);
+SELECT worker_spi_launch(2);
+SELECT worker_spi_launch(3);
+SELECT worker_spi_launch(4);
+SELECT worker_spi_launch(5);
+SELECT worker_spi_launch(6);
+SELECT worker_spi_launch(7);
+SELECT worker_spi_launch(8);
+-- should fail
+SELECT worker_spi_launch(9);
+
+-- wait till it started
+SELECT pg_sleep(3);
+SELECT count(*) FROM pg_stat_activity WHERE application_name NOT IN ('psql', 'pg_regress');
+\dn
+\dt schema*.*
+
+SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE application_name NOT IN ('psql', 'pg_regress');
+SELECT pg_sleep(1);
+SELECT count(*) FROM pg_stat_activity WHERE application_name NOT IN ('psql', 'pg_regress');
+
+-- can start after crash
+SELECT worker_spi_launch(10);
+SELECT worker_spi_launch(11);
+
+-- wait till started
+SELECT pg_sleep(3);
+SELECT count(*) FROM pg_stat_activity WHERE application_name NOT IN ('psql', 'pg_regress');
+
+SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE application_name NOT IN ('psql', 'pg_regress');
+SELECT pg_sleep(3);
+SELECT count(*) FROM pg_stat_activity WHERE application_name NOT IN ('psql', 'pg_regress');
+
+SELECT pg_sleep(3);
+\dn
+\dt schema*.*
-- 
1.8.3.251.g1462b67

