8000 Back-Patch "Add wait_for_subscription_sync for TAP tests." · postgres/postgres@6390354 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6390354

Browse files
author
Amit Kapila
committed
Back-Patch "Add wait_for_subscription_sync for TAP tests."
This was originally done in commit 0c20dd3 for 16 only, to eliminate duplicate code and as an infrastructure that makes it easier to write future tests. However, it has been suggested that it would be good to back-patch this testing infrastructure to aid future tests in back-branches. Backpatch to all supported versions. Author: Masahiko Sawada Reviewed by: Amit Kapila, Shi yu Discussion: https://postgr.es/m/CAD21AoC-fvAkaKHa4t1urupwL8xbAcWRePeETvshvy80f6WV1A@mail.gmail.com Discussion: https://postgr.es/m/E1oJBIf-0006sw-SA@gemulon.postgresql.org
1 parent e721123 commit 6390354

File tree

9 files changed

+64
-63
lines changed

9 files changed

+64
-63
lines changed

src/test/perl/PostgresNode.pm

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,50 @@ sub wait_for_slot_catchup
20762076

20772077
=pod
20782078
2079+
=item $node->wait_for_subscription_sync(publisher, subname, dbname)
2080+
2081+
Wait for all tables in pg_subscription_rel to complete the initial
2082+
synchronization (i.e to be either in 'syncdone' or 'ready' state).
2083+
2084+
If the publisher node is given, additionally, check if the subscriber has
2085+
caught up to what has been committed on the primary. This is useful to
2086+
ensure that the initial data synchronization has been completed after
2087+
creating a new subscription.
2088+
2089+
If there is no active replication connection from this peer, wait until
2090+
poll_query_until timeout.
2091+
2092+
This is not a test. It die()s on failure.
2093+
2094+
=cut
2095+
2096+
sub wait_for_subscription_sync
2097+
{
2098+
my ($self, $publisher, $subname, $dbname) = @_;
2099+
my $name = $self->name;
2100+
2101+
$dbname = defined($dbname) ? $dbname : 'postgres';
2102+
2103+
# Wait for all tables to finish initial sync.
2104+
print "Waiting for all subscriptions in \"$name\" to synchronize data\n";
2105+
my $query =
2106+
qq[SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');];
2107+
$self->poll_query_until($dbname, $query)
2108+
or croak "timed out waiting for subscriber to synchronize data";
2109+
2110+
# Then, wait for the replication to catchup if required.
2111+
if (defined($publisher))
2112+
{
2113+
croak 'subscription name must be specified' unless defined($subname);
2114+
$publisher->wait_for_catchup($subname);
2115+
}
2116+
2117+
print "done\n";
2118+
return;
2119+
}
2120+
2121+
=pod
2122+
20792123
=item $node->wait_for_log(regexp, offset)
20802124
20812125
Waits for the contents of the server log file, starting at the given offset, to

src/test/subscription/t/001_rep_changes.pl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,8 @@
8686
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub, tap_pub_ins_only"
8787
);
8888

89-
$node_publisher->wait_for_catchup($appname);
90-
91-
# Also wait for initial table sync to finish
92-
my $synced_query =
93-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
94-
$node_subscriber->poll_query_until('postgres', $synced_query)
95-
or die "Timed out while waiting for subscriber to synchronize data";
89+
# Wait for initial table sync to finish
90+
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
9691

9792
my $result =
9893
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_notrep");

src/test/subscription/t/002_types.pl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,8 @@
112112
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub WITH (slot_name = tap_sub_slot)"
113113
);
114114

115-
$node_publisher->wait_for_catchup($appname);
116-
117-
# Wait for initial sync to finish as well
118-
my $synced_query =
119-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('s', 'r');";
120-
$node_subscriber->poll_query_until('postgres', $synced_query)
121-
or die "Timed out while waiting for subscriber to synchronize data";
115+
# Wait for initial sync to finish
116+
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
122117

123118
# Insert initial test data
124119
$node_publisher->safe_psql(

src/test/subscription/t/004_sync.pl

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,8 @@
3737
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub"
3838
);
3939

40-
$node_publisher->wait_for_catchup($appname);
41-
42-
# Also wait for initial table sync to finish
43-
my $synced_query =
44-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
45-
$node_subscriber->poll_query_until('postgres', $synced_query)
46-
or die "Timed out while waiting for subscriber to synchronize data";
40+
# Wait for initial table sync to finish
41+
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
4742

4843
my $result =
4944
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_rep");
@@ -69,8 +64,7 @@
6964
$node_subscriber->safe_psql('postgres', "DELETE FROM tab_rep;");
7065

7166
# wait for sync to finish this time
72-
$node_subscriber->poll_query_until('postgres', $synced_query)
73-
or die "Timed out while waiting for subscriber to synchronize data";
67+
$node_subscriber->wait_for_subscription_sync;
7468

7569
# check that all data is synced
7670
$result =
@@ -105,8 +99,7 @@
10599
);
106100

107101
# and wait for data sync to finish again
108-
$node_subscriber->poll_query_until('postgres', $synced_query)
109-
or die "Timed out while waiting for subscriber to synchronize data";
102+
$node_subscriber->wait_for_subscription_sync;
110103

111104
# check that all data is synced
112105
$result =
@@ -131,8 +124,7 @@
131124
"ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION");
132125

133126
# wait for sync to finish
134-
$node_subscriber->poll_query_until('postgres', $synced_query)
135-
or die "Timed out while waiting for subscriber to synchronize data";
127+
$node_subscriber->wait_for_subscription_sync;
136128

137129
$result = $node_subscriber->safe_psql('postgres',
138130
"SELECT count(*) FROM tab_rep_next");

src/test/subscription/t/005_encoding.pl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,8 @@
3030
"CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION mypub;"
3131
);
3232

33-
$node_publisher->wait_for_catchup($appname);
34-
35-
# Wait for initial sync to finish as well
36-
my $synced_query =
37-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('s', 'r');";
38-
$node_subscriber->poll_query_until('postgres', $synced_query)
39-
or die "Timed out while waiting for subscriber to synchronize data";
33+
# Wait for initial sync to finish
34+
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
4035

4136
$node_publisher->safe_psql('postgres',
4237
q{INSERT INTO test1 VALUES (1, E'Mot\xc3\xb6rhead')}); # hand-rolled UTF-8

src/test/subscription/t/006_rewrite.pl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,8 @@
2626
"CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION mypub;"
2727
);
2828

29-
$node_publisher->wait_for_catchup($appname);
30-
31-
# Wait for initial sync to finish as well
32-
my $synced_query =
33-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('s', 'r');";
34-
$node_subscriber->poll_query_until('postgres', $synced_query)
35-
or die "Timed out while waiting for subscriber to synchronize data";
29+
# Wait for initial sync to finish
30+
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
3631

3732
$node_publisher->safe_psql('postgres',
3833
q{INSERT INTO test1 (a, b) VALUES (1, 'one'), (2, 'two');});

src/test/subscription/t/008_diff_schema.pl

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,8 @@
3636
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub"
3737
);
3838

39-
$node_publisher->wait_for_catchup($appname);
40-
41-
# Also wait for initial table sync to finish
42-
my $synced_query =
43-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
44-
$node_subscriber->poll_query_until('postgres', $synced_query)
45-
or die "Timed out while waiting for subscriber to synchronize data";
39+
# Wait for initial table sync to finish
40+
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
4641

4742
my $result =
4843
$node_subscriber->safe_psql('postgres',
@@ -105,8 +100,7 @@
105100
$node_subscriber->safe_psql('postgres',
106101
"ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION");
107102

108-
$node_subscriber->poll_query_until('postgres', $synced_query)
109-
or die "Timed out while waiting for subscriber to synchronize data";
103+
$node_subscriber->wait_for_subscription_sync;
110104

111105
# Add replica identity column. (The serial is not necessary, but it's
112106
# a convenient way to get a default on the new column so that rows

src/test/subscription/t/010_truncate.pl

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@
6464
);
6565

6666
# Wait for initial sync of all subscriptions
67-
my $synced_query =
68-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
69-
$node_subscriber->poll_query_until('postgres', $synced_query)
70-
or die "Timed out while waiting for subscriber to synchronize data";
67+
$node_subscriber->wait_for_subscription_sync;
7168

7269
# insert data to truncate
7370

@@ -180,8 +177,7 @@
180177
);
181178

182179
# wait for initial data sync
183-
$node_subscriber->poll_query_until('postgres', $synced_query)
184-
or die "Timed out while waiting for subscriber to synchronize data";
180+
$node_subscriber->wait_for_subscription_sync;
185181

186182
# insert data to truncate
187183

src/test/subscription/t/100_bugs.pl

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,8 @@
152152
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=tap_sub' PUBLICATION tap_pub"
153153
);
154154

155-
$node_publisher->wait_for_catchup('tap_sub');
156-
157-
# Also wait for initial table sync to finish
158-
my $synced_query =
159-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('s', 'r');";
160-
$node_subscriber->poll_query_until('postgres', $synced_query)
161-
or die "Timed out while waiting for subscriber to synchronize data";
155+
# Wait for initial table sync to finish
156+
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
162157

163158
is( $node_subscriber->safe_psql(
164159
'postgres', "SELECT * FROM tab_replidentity_index"),

0 commit comments

Comments
 (0)
0