Skip to content

Commit e8a0cf9

Browse files
committed
Backpatch addition of wait_for_log(), pump_until().
These were originally introduced in a2ab9c0 and a2ab9c0, as they are needed by a about-to-be-backpatched test. Discussion: https://postgr.es/m/20220413002626.udl7lll7f3o7nre7@alap3.anarazel.de Backpatch: 10-14
1 parent d6ae041 commit e8a0cf9

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/test/perl/PostgreSQL/Test/Utils.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ our @EXPORT = qw(
2222
system_or_bail
2323
system_log
2424
run_log
25+
pump_until
2526
2627
command_ok
2728
command_fails

src/test/perl/PostgresNode.pm

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ package PostgresNode;
8383
use strict;
8484
use warnings;
8585

86+
use Carp;
8687
use Config;
8788
use Cwd;
8889
use Exporter 'import';
@@ -1932,6 +1933,41 @@ qq[SELECT '$target_lsn' <= ${mode}_lsn FROM pg_catalog.pg_replication_slots WHER
19321933

19331934
=pod
19341935
1936+
=item $node->wait_for_log(regexp, offset)
1937+
1938+
Waits for the contents of the server log file, starting at the given offset, to
1939+
match the supplied regular expression. Checks the entire log if no offset is
1940+
given. Times out after $TestLib::timeout_default seconds.
1941+
1942+
If successful, returns the length of the entire log file, in bytes.
1943+
1944+
=cut
1945+
1946+
sub wait_for_log
1947+
{
1948+
my ($self, $regexp, $offset) = @_;
1949+
$offset = 0 unless defined $offset;
1950+
1951+
my $max_attempts = 10 * $TestLib::timeout_default;
1952+
my $attempts = 0;
1953+
1954+
while ($attempts < $max_attempts)
1955+
{
1956+
my $log = TestLib::slurp_file($self->logfile, $offset);
1957+
1958+
return $offset+length($log) if ($log =~ m/$regexp/);
1959+
1960+
# Wait 0.1 second before retrying.
1961+
usleep(100_000);
1962+
1963+
$attempts++;
1964+
}
1965+
1966+
croak "timed out waiting for match: $regexp";
1967+
}
1968+
1969+
=pod
1970+
19351971
=item $node->query_hash($dbname, $query, @columns)
19361972
19371973
Execute $query on $dbname, replacing any appearance of the string __COLUMNS__

src/test/perl/TestLib.pm

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ our @EXPORT = qw(
3131
system_or_bail
3232
system_log
3333
run_log
34+
pump_until
3435
3536
command_ok
3637
command_fails
@@ -232,6 +233,36 @@ sub run_log
232233
return IPC::Run::run(@_);
233234
}
234235

236+
=pod
237+
238+
=item pump_until(proc, timeout, stream, until)
239+
240+
Pump until string is matched on the specified stream, or timeout occurs.
241+
242+
=cut
243+
244+
sub pump_until
245+
{
246+
my ($proc, $timeout, $stream, $until) = @_;
247+
$proc->pump_nb();
248+
while (1)
249+
{
250+
last if $$stream =~ /$until/;
251+
if ($timeout->is_expired)
252+
{
253+
diag("pump_until: timeout expired when searching for \"$until\" with stream: \"$$stream\"");
254+
return 0;
255+
}
256+
if (not $proc->pumpable())
257+
{
258+
diag("pump_until: process terminated unexpectedly when searching for \"$until\" with stream: \"$$stream\"");
259+
return 0;
260+
}
261+
$proc->pump();
262+
}
263+
return 1;
264+
}
265+
235266
# Generate a string made of the given range of ASCII characters
236267
sub generate_ascii_string
237268
{

0 commit comments

Comments
 (0)