Skip to content

Commit 4b3f1dd

Browse files
committed
Increase test coverage for worker_spi by ∞%
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
1 parent 0b8e053 commit 4b3f1dd

File tree

5 files changed

+106
-1
lines changed

5 files changed

+106
-1
lines changed

src/test/modules/worker_spi/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ EXTENSION = worker_spi
66
DATA = worker_spi--1.0.sql
77
PGFILEDESC = "worker_spi - background worker example"
88

9+
REGRESS = worker_spi
10+
11+
# enable our module in shared_preload_libraries for dynamic bgworkers
12+
REGRESS_OPTS = --temp-config $(top_srcdir)/src/test/modules/worker_spi/dynamic.conf
13+
14+
# Disable installcheck to ensure we cover dynamic bgworkers.
15+
NO_INSTALLCHECK = 1
16+
917
ifdef USE_PGXS
1018
PG_CONFIG = pg_config
1119
PGXS := $(shell $(PG_CONFIG) --pgxs)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
shared_preload_libraries = worker_spi
2+
worker_spi.database = contrib_regression
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
CREATE EXTENSION worker_spi;
2+
SELECT worker_spi_launch(4) IS NOT NULL;
3+
?column?
4+
----------
5+
t
6+
(1 row)
7+
8+
-- wait until the worker completes its initialization
9+
DO $$
10+
DECLARE
11+
visible bool;
12+
loops int := 0;
13+
BEGIN
14+
LOOP
15+
visible := table_name IS NOT NULL
16+
FROM information_schema.tables
17+
WHERE table_schema = 'schema4' AND table_name = 'counted';
18+
IF visible OR loops > 120 * 10 THEN EXIT; END IF;
19+
PERFORM pg_sleep(0.1);
20+
loops := loops + 1;
21+
END LOOP;
22+
END
23+
$$;
24+
INSERT INTO schema4.counted VALUES ('total', 0), ('delta', 1);
25+
SELECT pg_reload_conf();
26+
pg_reload_conf
27+
----------------
28+
t
29+
(1 row)
30+
31+
-- wait until the worker has processed the tuple we just inserted
32+
DO $$
33+
DECLARE
34+
count int;
35+
loops int := 0;
36+
BEGIN
37+
LOOP
38+
count := count(*) FROM schema4.counted WHERE type = 'delta';
39+
IF count = 0 OR loops > 120 * 10 THEN EXIT; END IF;
40+
PERFORM pg_sleep(0.1);
41+
loops := loops + 1;
42+
END LOOP;
43+
END
44+
$$;
45+
SELECT * FROM schema4.counted;
46+
type | value
47+
-------+-------
48+
total | 1
49+
(1 row)
50+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
CREATE EXTENSION worker_spi;
2+
SELECT worker_spi_launch(4) IS NOT NULL;
3+
-- wait until the worker completes its initialization
4+
DO $$
5+
DECLARE
6+
visible bool;
7+
loops int := 0;
8+
BEGIN
9+
LOOP
10+
visible := table_name IS NOT NULL
11+
FROM information_schema.tables
12+
WHERE table_schema = 'schema4' AND table_name = 'counted';
13+
IF visible OR loops > 120 * 10 THEN EXIT; END IF;
14+
PERFORM pg_sleep(0.1);
15+
loops := loops + 1;
16+
END LOOP;
17+
END
18+
$$;
19+
INSERT INTO schema4.counted VALUES ('total', 0), ('delta', 1);
20+
SELECT pg_reload_conf();
21+
-- wait until the worker has processed the tuple we just inserted
22+
DO $$
23+
DECLARE
24+
count int;
25+
loops int := 0;
26+
BEGIN
27+
LOOP
28+
count := count(*) FROM schema4.counted WHERE type = 'delta';
29+
IF count = 0 OR loops > 120 * 10 THEN EXIT; END IF;
30+
PERFORM pg_sleep(0.1);
31+
loops := loops + 1;
32+
END LOOP;
33+
END
34+
$$;
35+
SELECT * FROM schema4.counted;

src/test/modules/worker_spi/worker_spi.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ static volatile sig_atomic_t got_sigterm = false;
5555
/* GUC variables */
5656
static int worker_spi_naptime = 10;
5757
static int worker_spi_total_workers = 2;
58+
static char *worker_spi_database = NULL;
5859

5960

6061
typedef struct worktable
@@ -179,7 +180,7 @@ worker_spi_main(Datum main_arg)
179180
BackgroundWorkerUnblockSignals();
180181

181182
/* Connect to our database */
182-
BackgroundWorkerInitializeConnection("postgres", NULL, 0);
183+
BackgroundWorkerInitializeConnection(worker_spi_database, NULL, 0);
183184

184185
elog(LOG, "%s initialized with %s.%s",
185186
MyBgworkerEntry->bgw_name, table->schema, table->name);
@@ -339,6 +340,15 @@ _PG_init(void)
339340
NULL,
340341
NULL);
341342

343+
DefineCustomStringVariable("worker_spi.database",
344+
"Database to connect to.",
345+
NULL,
346+
&worker_spi_database,
347+
"postgres",
348+
PGC_POSTMASTER,
349+
0,
350+
NULL, NULL, NULL);
351+
342352
/* set up common data for all our workers */
343353
memset(&worker, 0, sizeof(worker));
344354
worker.bgw_flags = BGWORKER_SHMEM_ACCESS |

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy