Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f01e16b

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/[email protected]
1 parent bf0718c commit f01e16b

File tree

8 files changed

+59
-48
lines changed

8 files changed

+59
-48
lines changed

src/test/perl/PostgresNode.pm

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,6 +1944,50 @@ qq[SELECT '$target_lsn' <= ${mode}_lsn FROM pg_catalog.pg_replication_slots WHER
19441944

19451945
=pod
19461946
1947+
=item $node->wait_for_subscription_sync(publisher, subname, dbname)
1948+
1949+
Wait for all tables in pg_subscription_rel to complete the initial
1950+
synchronization (i.e to be either in 'syncdone' or 'ready' state).
1951+
1952+
If the publisher node is given, additionally, check if the subscriber has
1953+
caught up to what has been committed on the primary. This is useful to
1954+
ensure that the initial data synchronization has been completed after
1955+
creating a new subscription.
1956+
1957+
If there is no active replication connection from this peer, wait until
1958+
poll_query_until timeout.
1959+
1960+
This is not a test. It die()s on failure.
1961+
1962+
=cut
1963+
1964+
sub wait_for_subscription_sync
1965+
{
1966+
my ($self, $publisher, $subname, $dbname) = @_;
1967+
my $name = $self->name;
1968+
1969+
$dbname = defined($dbname) ? $dbname : 'postgres';
1970+
1971+
# Wait for all tables to finish initial sync.
1972+
print "Waiting for all subscriptions in \"$name\" to synchronize data\n";
1973+
my $query =
1974+
qq[SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');];
1975+
$self->poll_query_until($dbname, $query)
1976+
or croak "timed out waiting for subscriber to synchronize data";
1977+
1978+
# Then, wait for the replication to catchup if required.
1979+
if (defined($publisher))
1980+
{
1981+
croak 'subscription name must be specified' unless defined($subname);
1982+
$publisher->wait_for_catchup($subname, 'replay', $publisher->lsn('write'));
1983+
}
1984+
1985+
print "done\n";
1986+
return;
1987+
}
1988+
1989+
=pod
1990+
19471991
=item $node->wait_for_log(regexp, offset)
19481992
19491993
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: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@
8585
or die "Timed out while waiting for subscriber to catch up";
8686

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

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

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@
119119
or die "Timed out while waiting for subscriber to catch up";
120120

121121
# Wait for initial sync to finish as well
122-
my $synced_query =
123-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('s', 'r');";
124-
$node_subscriber->poll_query_until('postgres', $synced_query)
125-
or die "Timed out while waiting for subscriber to synchronize data";
122+
$node_subscriber->wait_for_subscription_sync;
126123

127124
# Insert initial test data
128125
$node_publisher->safe_psql(

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@
4444
or die "Timed out while waiting for subscriber to catch up";
4545

4646
# Also wait for initial table sync to finish
47-
my $synced_query =
48-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
49-
$node_subscriber->poll_query_until('postgres', $synced_query)
50-
or die "Timed out while waiting for subscriber to synchronize data";
47+
$node_subscriber->wait_for_subscription_sync;
5148

5249
my $result =
5350
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM tab_rep");
@@ -73,8 +70,7 @@
7370
$node_subscriber->safe_psql('postgres', "DELETE FROM tab_rep;");
7471

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

7975
# check that all data is synced
8076
$result =
@@ -109,8 +105,7 @@
109105
);
110106

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

115110
# check that all data is synced
116111
$result =
@@ -137,8 +132,7 @@
137132
"ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION");
138133

139134
# wait for sync to finish
140-
$node_subscriber->poll_query_until('postgres', $synced_query)
141-
or die "Timed out while waiting for subscriber to synchronize data";
135+
$node_subscriber->wait_for_subscription_sync;
142136

143137
$result = $node_subscriber->safe_psql('postgres',
144138
"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
@@ -39,13 +39,8 @@ sub wait_for_caught_up
3939
"CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION mypub;"
4040
);
4141

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

5045
$node_publisher->safe_psql('postgres',
5146
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
@@ -42,13 +42,8 @@ sub wait_for_caught_up
4242
"CREATE SUBSCRIPTION mysub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION mypub;"
4343
);
4444

45-
wait_for_caught_up($node_publisher, $appname);
46-
47-
# Wait for initial sync to finish as well
48-
my $synced_query =
49-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('s', 'r');";
50-
$node_subscriber->poll_query_until('postgres', $synced_query)
51-
or die "Timed out while waiting for subscriber to synchronize data";
45+
# Wait for initial sync to finish
46+
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
5247

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

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,8 @@ sub wait_for_caught_up
4242
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=$appname' PUBLICATION tap_pub"
4343
);
4444

45-
wait_for_caught_up($node_publisher, $appname);
46-
47-
# Also wait for initial table sync to finish
48-
my $synced_query =
49-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r', 's');";
50-
$node_subscriber->poll_query_until('postgres', $synced_query)
51-
or die "Timed out while waiting for subscriber to synchronize data";
45+
# Wait for initial table sync to finish
46+
$node_subscriber->wait_for_subscription_sync($node_publisher, $appname);
5247

5348
my $result =
5449
$node_subscriber->safe_psql('postgres', "SELECT count(*), count(c), count(d = 999) FROM test_tab");
@@ -102,8 +97,7 @@ sub wait_for_caught_up
10297
$node_subscriber->safe_psql('postgres',
10398
"ALTER SUBSCRIPTION tap_sub REFRESH PUBLICATION");
10499

105-
$node_subscriber->poll_query_until('postgres', $synced_query)
106-
or die "Timed out while waiting for subscriber to synchronize data";
100+
$node_subscriber->wait_for_subscription_sync;
107101

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

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,8 @@ sub wait_for_caught_up
161161
"CREATE SUBSCRIPTION tap_sub CONNECTION '$publisher_connstr application_name=tap_sub' PUBLICATION tap_pub"
162162
);
163163

164-
wait_for_caught_up($node_publisher, 'tap_sub');
165-
166-
# Also wait for initial table sync to finish
167-
my $synced_query =
168-
"SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('s', 'r');";
169-
$node_subscriber->poll_query_until('postgres', $synced_query)
170-
or die "Timed out while waiting for subscriber to synchronize data";
164+
# Wait for initial table sync to finish
165+
$node_subscriber->wait_for_subscription_sync($node_publisher, 'tap_sub');
171166

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

0 commit comments

Comments
 (0)