Skip to content

Commit de3de0a

Browse files
committed
Improve TAP test function PostgresNode::poll_query_until().
Add an optional "expected" argument to override the default assumption that we're waiting for the query to return "t". This allows replacing a handwritten polling loop in recovery/t/007_sync_rep.pl with use of poll_query_until(); AFAICS that's the only remaining ad-hoc polling loop in our TAP tests. Change poll_query_until() to probe ten times per second not once per second. Like some similar changes I've been making recently, the one-second interval seems to be rooted in ancient traditions rather than the actual likely wait duration on modern machines. I'd consider reducing it further if there were a convenient way to spawn just one psql for the whole loop rather than one per probe attempt. Discussion: https://postgr.es/m/12486.1498938782@sss.pgh.pa.us
1 parent 2dca034 commit de3de0a

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

src/test/perl/PostgresNode.pm

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,36 +1213,43 @@ sub psql
12131213

12141214
=pod
12151215
1216-
=item $node->poll_query_until(dbname, query)
1216+
=item $node->poll_query_until($dbname, $query [, $expected ])
12171217
1218-
Run a query once a second, until it returns 't' (i.e. SQL boolean true).
1219-
Continues polling if psql returns an error result. Times out after 180 seconds.
1218+
Run B<$query> repeatedly, until it returns the B<$expected> result
1219+
('t', or SQL boolean true, by default).
1220+
Continues polling if B<psql> returns an error result.
1221+
Times out after 180 seconds.
1222+
Returns 1 if successful, 0 if timed out.
12201223
12211224
=cut
12221225

12231226
sub poll_query_until
12241227
{
1225-
my ($self, $dbname, $query) = @_;
1228+
my ($self, $dbname, $query, $expected) = @_;
12261229

1227-
my $max_attempts = 180;
1228-
my $attempts = 0;
1230+
$expected = 't' unless defined($expected); # default value
1231+
1232+
my $cmd =
1233+
[ 'psql', '-XAt', '-c', $query, '-d', $self->connstr($dbname) ];
12291234
my ($stdout, $stderr);
1235+
my $max_attempts = 180 * 10;
1236+
my $attempts = 0;
12301237

12311238
while ($attempts < $max_attempts)
12321239
{
1233-
my $cmd =
1234-
[ 'psql', '-XAt', '-c', $query, '-d', $self->connstr($dbname) ];
12351240
my $result = IPC::Run::run $cmd, '>', \$stdout, '2>', \$stderr;
12361241

12371242
chomp($stdout);
12381243
$stdout =~ s/\r//g if $TestLib::windows_os;
1239-
if ($stdout eq "t")
1244+
1245+
if ($stdout eq $expected)
12401246
{
12411247
return 1;
12421248
}
12431249

1244-
# Wait a second before retrying.
1245-
sleep 1;
1250+
# Wait 0.1 second before retrying.
1251+
select undef, undef, undef, 0.1;
1252+
12461253
$attempts++;
12471254
}
12481255

src/test/recovery/t/007_sync_rep.pl

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
my $check_sql =
1010
"SELECT application_name, sync_priority, sync_state FROM pg_stat_replication ORDER BY application_name;";
1111

12-
# Check that sync_state of each standby is expected.
12+
# Check that sync_state of each standby is expected (waiting till it is).
1313
# If $setting is given, synchronous_standby_names is set to it and
1414
# the configuration file is reloaded before the test.
1515
sub test_sync_state
@@ -23,24 +23,7 @@ sub test_sync_state
2323
$self->reload;
2424
}
2525

26-
my $timeout_max = 30;
27-
my $timeout = 0;
28-
my $result;
29-
30-
# A reload may take some time to take effect on busy machines,
31-
# hence use a loop with a timeout to give some room for the test
32-
# to pass.
33-
while ($timeout < $timeout_max)
34-
{
35-
$result = $self->safe_psql('postgres', $check_sql);
36-
37-
last if ($result eq $expected);
38-
39-
$timeout++;
40-
sleep 1;
41-
}
42-
43-
is($result, $expected, $msg);
26+
ok( $self->poll_query_until('postgres', $check_sql, $expected), $msg);
4427
}
4528

4629
# Initialize master node

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