Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 320c311

Browse files
committed
worker_spi: Switch to TAP tests
This commit moves worker_spi to use TAP tests. sql/worker_spi.sql is gone, replaced by an equivalent set of queries in a TAP script, without worker_spi loaded in shared_preload_libraries: - One query to launch a worker dynamically, relying now on "postgres" as the default database to connect to. - Two wait queries with poll_query_until(), one to wait for the worker schema to be initialized and a second to wait for a tuple processed by the worker. - Server reload to accelerate the main loop of the spawned worker. More coverage is added for workers registered when the library is loaded with shared_preload_libraries, while on it, checking that these are connecting to the database set in the GUC worker_spi.database. A local run of these test is showing that TAP is slightly faster than the original, while providing more coverage (3.7s vs 4.4s). There was also some discussions about keeping the SQL tests, but this would require initializing twice a cluster, increasing the runtime of the tests up to 5.6s here. These tests will be expanded more in an upcoming patch aimed at adding support for custom wait events for the Extension class, still under discussion, to check the new in-core APIs with and without a library set in shared_preload_libraries. Bharath has written the part where shared_preload_libraries is used, while I have migrated the existing SQL tests to TAP. Author: Bharath Rupireddy, Michael Paquier Reviewed-by: Masahiro Ikeda Discussion: https://postgr.es/m/CALj2ACWR9ncAiDF73unqdJF1dmsA2R0efGXX2624X+YVxcAVWg@mail.gmail.com
1 parent b635ac0 commit 320c311

File tree

7 files changed

+84
-102
lines changed

7 files changed

+84
-102
lines changed
-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
# Generated subdirectories
2-
/log/
3-
/results/
42
/tmp_check/

src/test/modules/worker_spi/Makefile

+1-7
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ 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
9+
TAP_TESTS = 1
1610

1711
ifdef USE_PGXS
1812
PG_CONFIG = pg_config

src/test/modules/worker_spi/dynamic.conf

-2
This file was deleted.

src/test/modules/worker_spi/expected/worker_spi.out

-50
This file was deleted.

src/test/modules/worker_spi/meson.build

+3-6
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@ tests += {
2525
'name': 'worker_spi',
2626
'sd': meson.current_source_dir(),
2727
'bd': meson.current_build_dir(),
28-
'regress': {
29-
'sql': [
30-
'worker_spi',
28+
'tap': {
29+
'tests': [
30+
't/001_worker_spi.pl',
3131
],
32-
'dbname': 'contrib_regression',
33-
'regress_args': ['--temp-config', files('dynamic.conf')],
34-
'runningcheck': false,
3532
},
3633
}

src/test/modules/worker_spi/sql/worker_spi.sql

-35
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright (c) 2023, PostgreSQL Global Development Group
2+
3+
# Test worker_spi module.
4+
5+
use strict;
6+
use warnings;
7+
use PostgreSQL::Test::Cluster;
8+
use PostgreSQL::Test::Utils;
9+
use Test::More;
10+
11+
my $node = PostgreSQL::Test::Cluster->new('mynode');
12+
$node->init;
13+
$node->start;
14+
15+
note "testing dynamic bgworkers";
16+
17+
$node->safe_psql('postgres', 'CREATE EXTENSION worker_spi;');
18+
19+
# Launch one dynamic worker, then wait for its initialization to complete.
20+
# This consists in making sure that a table name "counted" is created
21+
# on a new schema whose name includes the index defined in input argument
22+
# of worker_spi_launch().
23+
# By default, dynamic bgworkers connect to the "postgres" database.
24+
my $result =
25+
$node->safe_psql('postgres', 'SELECT worker_spi_launch(4) IS NOT NULL;');
26+
is($result, 't', "dynamic bgworker launched");
27+
$node->poll_query_until(
28+
'postgres',
29+
qq[SELECT count(*) > 0 FROM information_schema.tables
30+
WHERE table_schema = 'schema4' AND table_name = 'counted';]);
31+
$node->safe_psql('postgres',
32+
"INSERT INTO schema4.counted VALUES ('total', 0), ('delta', 1);");
33+
# Issue a SIGHUP on the node to force the worker to loop once, accelerating
34+
# this test.
35+
$node->reload;
36+
# Wait until the worker has processed the tuple that has just been inserted.
37+
$node->poll_query_until('postgres',
38+
qq[SELECT count(*) FROM schema4.counted WHERE type = 'delta';], '0');
39+
$result = $node->safe_psql('postgres', 'SELECT * FROM schema4.counted;');
40+
is($result, qq(total|1), 'dynamic bgworker correctly consumed tuple data');
41+
42+
note "testing bgworkers loaded with shared_preload_libraries";
43+
44+
# Create the database first so as the workers can connect to it when
45+
# the library is loaded.
46+
$node->safe_psql('postgres', q(CREATE DATABASE mydb;));
47+
$node->safe_psql('mydb', 'CREATE EXTENSION worker_spi;');
48+
49+
# Now load the module as a shared library.
50+
$node->append_conf(
51+
'postgresql.conf', q{
52+
shared_preload_libraries = 'worker_spi'
53+
worker_spi.database = 'mydb'
54+
worker_spi.total_workers = 3
55+
});
56+
$node->restart;
57+
58+
# Check that bgworkers have been registered and launched.
59+
ok( $node->poll_query_until(
60+
'mydb',
61+
qq[SELECT datname, count(datname) FROM pg_stat_activity
62+
WHERE backend_type = 'worker_spi' GROUP BY datname;],
63+
'mydb|3'),
64+
'bgworkers all launched'
65+
) or die "Timed out while waiting for bgworkers to be launched";
66+
67+
# Ask worker_spi to launch dynamic bgworkers with the library loaded, then
68+
# check their existence.
69+
my $worker1_pid = $node->safe_psql('mydb', 'SELECT worker_spi_launch(1);');
70+
my $worker2_pid = $node->safe_psql('mydb', 'SELECT worker_spi_launch(2);');
71+
ok( $node->poll_query_until(
72+
'mydb',
73+
qq[SELECT datname, count(datname) FROM pg_stat_activity
74+
WHERE backend_type = 'worker_spi dynamic' AND
75+
pid IN ($worker1_pid, $worker2_pid) GROUP BY datname;],
76+
'mydb|2'),
77+
'dynamic bgworkers all launched'
78+
) or die "Timed out while waiting for dynamic bgworkers to be launched";
79+
80+
done_testing();

0 commit comments

Comments
 (0)