=pod
-=item $node->poll_query_until(dbname, query)
+=item $node->poll_query_until($dbname, $query [, $expected ])
-Run a query once a second, until it returns 't' (i.e. SQL boolean true).
-Continues polling if psql returns an error result. Times out after 180 seconds.
+Run B<$query> repeatedly, until it returns the B<$expected> result
+('t', or SQL boolean true, by default).
+Continues polling if B<psql> returns an error result.
+Times out after 180 seconds.
+Returns 1 if successful, 0 if timed out.
=cut
sub poll_query_until
{
- my ($self, $dbname, $query) = @_;
+ my ($self, $dbname, $query, $expected) = @_;
- my $max_attempts = 180;
- my $attempts = 0;
+ $expected = 't' unless defined($expected); # default value
+
+ my $cmd =
+ [ 'psql', '-XAt', '-c', $query, '-d', $self->connstr($dbname) ];
my ($stdout, $stderr);
+ my $max_attempts = 180 * 10;
+ my $attempts = 0;
while ($attempts < $max_attempts)
{
- my $cmd =
- [ 'psql', '-XAt', '-c', $query, '-d', $self->connstr($dbname) ];
my $result = IPC::Run::run $cmd, '>', \$stdout, '2>', \$stderr;
chomp($stdout);
$stdout =~ s/\r//g if $TestLib::windows_os;
- if ($stdout eq "t")
+
+ if ($stdout eq $expected)
{
return 1;
}
- # Wait a second before retrying.
- sleep 1;
+ # Wait 0.1 second before retrying.
+ select undef, undef, undef, 0.1;
+
$attempts++;
}
my $check_sql =
"SELECT application_name, sync_priority, sync_state FROM pg_stat_replication ORDER BY application_name;";
-# Check that sync_state of each standby is expected.
+# Check that sync_state of each standby is expected (waiting till it is).
# If $setting is given, synchronous_standby_names is set to it and
# the configuration file is reloaded before the test.
sub test_sync_state
$self->reload;
}
- my $timeout_max = 30;
- my $timeout = 0;
- my $result;
-
- # A reload may take some time to take effect on busy machines,
- # hence use a loop with a timeout to give some room for the test
- # to pass.
- while ($timeout < $timeout_max)
- {
- $result = $self->safe_psql('postgres', $check_sql);
-
- last if ($result eq $expected);
-
- $timeout++;
- sleep 1;
- }
-
- is($result, $expected, $msg);
+ ok( $self->poll_query_until('postgres', $check_sql, $expected), $msg);
}
# Initialize master node