On 2019-May-29, Tom Lane wrote:

> Alvaro Herrera <alvhe...@2ndquadrant.com> writes:
> > Tom pointed out that coverage for worker_spi is 0%.  For a module that
> > only exists to provide coverage, that's pretty stupid.  This patch
> > increases coverage to 90.9% line-wise and 100% function-wise, which
> > seems like a sufficient starting point.
> 
> > How would people feel about me getting this in master at this point in
> > the cycle, it being just some test code?  We can easily revert if
> > it seems too unstable.
> 
> I'm not opposed to adding a new test case at this point in the cycle,
> but as written this one seems more or less guaranteed to fail under
> load.

True.  Here's a version that should be more resilient.

One thing I noticed while writing it, though, is that worker_spi uses
the postgres database, instead of the contrib_regression database that
was created for it.  And we create a schema and a table there.  This is
going to get some eyebrows raised, I think, so I'll look into fixing
that as a bugfix before getting this commit in.

-- 
Álvaro Herrera                https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
diff --git a/src/test/modules/worker_spi/Makefile b/src/test/modules/worker_spi/Makefile
index 7cdb33c9df..cbf9b2e37f 100644
--- a/src/test/modules/worker_spi/Makefile
+++ b/src/test/modules/worker_spi/Makefile
@@ -6,6 +6,14 @@ EXTENSION = worker_spi
 DATA = worker_spi--1.0.sql
 PGFILEDESC = "worker_spi - background worker example"
 
+REGRESS = worker_spi
+
+# enable our module in shared_preload_libraries for dynamic bgworkers
+REGRESS_OPTS = --temp-config $(top_srcdir)/src/test/modules/worker_spi/dynamic.conf
+
+# Disable installcheck to ensure we cover dynamic bgworkers.
+NO_INSTALLCHECK = 1
+
 ifdef USE_PGXS
 PG_CONFIG = pg_config
 PGXS := $(shell $(PG_CONFIG) --pgxs)
diff --git a/src/test/modules/worker_spi/dynamic.conf b/src/test/modules/worker_spi/dynamic.conf
new file mode 100644
index 0000000000..646885a9c7
--- /dev/null
+++ b/src/test/modules/worker_spi/dynamic.conf
@@ -0,0 +1,2 @@
+worker_spi.naptime = 1
+shared_preload_libraries = worker_spi
diff --git a/src/test/modules/worker_spi/expected/worker_spi.out b/src/test/modules/worker_spi/expected/worker_spi.out
new file mode 100644
index 0000000000..cf1f252e29
--- /dev/null
+++ b/src/test/modules/worker_spi/expected/worker_spi.out
@@ -0,0 +1,37 @@
+\c postgres
+CREATE EXTENSION worker_spi;
+SELECT worker_spi_launch(1) IS NOT NULL;
+ ?column? 
+----------
+ t
+(1 row)
+
+INSERT INTO schema1.counted VALUES ('total', 0), ('delta', 1);
+SELECT pg_reload_conf();
+ pg_reload_conf 
+----------------
+ t
+(1 row)
+
+DO $$
+	DECLARE
+		count int;
+		loops int := 0;
+	BEGIN
+		LOOP
+			PERFORM pg_sleep(CASE WHEN count(*) = 0 THEN 0 ELSE 0.1 END)
+			FROM schema1.counted WHERE type = 'delta';
+			GET DIAGNOSTICS count = ROW_COUNT;
+			loops := loops + 1;
+			IF count < 1 OR loops > 180 * 10 THEN
+				RETURN;
+			END IF;
+	END LOOP;
+END
+$$;
+SELECT * FROM schema1.counted;
+ type  | value 
+-------+-------
+ total |     1
+(1 row)
+
diff --git a/src/test/modules/worker_spi/sql/worker_spi.sql b/src/test/modules/worker_spi/sql/worker_spi.sql
new file mode 100644
index 0000000000..bae2680dfb
--- /dev/null
+++ b/src/test/modules/worker_spi/sql/worker_spi.sql
@@ -0,0 +1,22 @@
+\c postgres
+CREATE EXTENSION worker_spi;
+SELECT worker_spi_launch(1) IS NOT NULL;
+INSERT INTO schema1.counted VALUES ('total', 0), ('delta', 1);
+SELECT pg_reload_conf();
+DO $$
+	DECLARE
+		count int;
+		loops int := 0;
+	BEGIN
+		LOOP
+			PERFORM pg_sleep(CASE WHEN count(*) = 0 THEN 0 ELSE 0.1 END)
+			FROM schema1.counted WHERE type = 'delta';
+			GET DIAGNOSTICS count = ROW_COUNT;
+			loops := loops + 1;
+			IF count < 1 OR loops > 180 * 10 THEN
+				RETURN;
+			END IF;
+	END LOOP;
+END
+$$;
+SELECT * FROM schema1.counted;
-- 
2.17.1

Reply via email to