]> granicus.if.org Git - postgresql/commitdiff
Increase test coverage for worker_spi by ∞%
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Sun, 2 Jun 2019 04:29:49 +0000 (00:29 -0400)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Sun, 2 Jun 2019 04:29:49 +0000 (00:29 -0400)
This test module was not getting invoked, other than at compile time,
limiting its usefulness -- and keeping its coverage at 0%.  Add a
minimal regression test to ensure it runs on make check-world; this
makes it 92% covered (line-wise), which seems sufficient.

Author: Álvaro Herrera <alvherre@alvh.no-ip.org>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/20190529193256.GA17603@alvherre.pgsql

src/test/modules/worker_spi/Makefile
src/test/modules/worker_spi/dynamic.conf [new file with mode: 0644]
src/test/modules/worker_spi/expected/worker_spi.out [new file with mode: 0644]
src/test/modules/worker_spi/sql/worker_spi.sql [new file with mode: 0644]
src/test/modules/worker_spi/worker_spi.c

index 7cdb33c9dfcee3577cd9f7eb0f8578057655f401..cbf9b2e37fd6da99b9250481f903eec1940e54ae 100644 (file)
@@ -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 (file)
index 0000000..bfe015f
--- /dev/null
@@ -0,0 +1,2 @@
+shared_preload_libraries = worker_spi
+worker_spi.database = contrib_regression
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 (file)
index 0000000..dc0a79b
--- /dev/null
@@ -0,0 +1,50 @@
+CREATE EXTENSION worker_spi;
+SELECT worker_spi_launch(4) IS NOT NULL;
+ ?column? 
+----------
+ t
+(1 row)
+
+-- wait until the worker completes its initialization
+DO $$
+DECLARE
+       visible bool;
+       loops int := 0;
+BEGIN
+       LOOP
+               visible := table_name IS NOT NULL
+                       FROM information_schema.tables
+                       WHERE table_schema = 'schema4' AND table_name = 'counted';
+               IF visible OR loops > 120 * 10 THEN EXIT; END IF;
+               PERFORM pg_sleep(0.1);
+               loops := loops + 1;
+       END LOOP;
+END
+$$;
+INSERT INTO schema4.counted VALUES ('total', 0), ('delta', 1);
+SELECT pg_reload_conf();
+ pg_reload_conf 
+----------------
+ t
+(1 row)
+
+-- wait until the worker has processed the tuple we just inserted
+DO $$
+DECLARE
+       count int;
+       loops int := 0;
+BEGIN
+       LOOP
+               count := count(*) FROM schema4.counted WHERE type = 'delta';
+               IF count = 0 OR loops > 120 * 10 THEN EXIT; END IF;
+               PERFORM pg_sleep(0.1);
+               loops := loops + 1;
+       END LOOP;
+END
+$$;
+SELECT * FROM schema4.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 (file)
index 0000000..4683523
--- /dev/null
@@ -0,0 +1,35 @@
+CREATE EXTENSION worker_spi;
+SELECT worker_spi_launch(4) IS NOT NULL;
+-- wait until the worker completes its initialization
+DO $$
+DECLARE
+       visible bool;
+       loops int := 0;
+BEGIN
+       LOOP
+               visible := table_name IS NOT NULL
+                       FROM information_schema.tables
+                       WHERE table_schema = 'schema4' AND table_name = 'counted';
+               IF visible OR loops > 120 * 10 THEN EXIT; END IF;
+               PERFORM pg_sleep(0.1);
+               loops := loops + 1;
+       END LOOP;
+END
+$$;
+INSERT INTO schema4.counted VALUES ('total', 0), ('delta', 1);
+SELECT pg_reload_conf();
+-- wait until the worker has processed the tuple we just inserted
+DO $$
+DECLARE
+       count int;
+       loops int := 0;
+BEGIN
+       LOOP
+               count := count(*) FROM schema4.counted WHERE type = 'delta';
+               IF count = 0 OR loops > 120 * 10 THEN EXIT; END IF;
+               PERFORM pg_sleep(0.1);
+               loops := loops + 1;
+       END LOOP;
+END
+$$;
+SELECT * FROM schema4.counted;
index c1878dd6946a93e45bc8e52b13d12e426ef0fd84..bc9ef64a508ed5ea652cdfe1300ab158638fdf79 100644 (file)
@@ -55,6 +55,7 @@ static volatile sig_atomic_t got_sigterm = false;
 /* GUC variables */
 static int     worker_spi_naptime = 10;
 static int     worker_spi_total_workers = 2;
+static char *worker_spi_database = NULL;
 
 
 typedef struct worktable
@@ -179,7 +180,7 @@ worker_spi_main(Datum main_arg)
        BackgroundWorkerUnblockSignals();
 
        /* Connect to our database */
-       BackgroundWorkerInitializeConnection("postgres", NULL, 0);
+       BackgroundWorkerInitializeConnection(worker_spi_database, NULL, 0);
 
        elog(LOG, "%s initialized with %s.%s",
                 MyBgworkerEntry->bgw_name, table->schema, table->name);
@@ -339,6 +340,15 @@ _PG_init(void)
                                                        NULL,
                                                        NULL);
 
+       DefineCustomStringVariable("worker_spi.database",
+                                                          "Database to connect to.",
+                                                          NULL,
+                                                          &worker_spi_database,
+                                                          "postgres",
+                                                          PGC_POSTMASTER,
+                                                          0,
+                                                          NULL, NULL, NULL);
+
        /* set up common data for all our workers */
        memset(&worker, 0, sizeof(worker));
        worker.bgw_flags = BGWORKER_SHMEM_ACCESS |