Skip to content

Commit 2adb8de

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 1dd49df commit 2adb8de

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ our @EXPORT = qw(
2525
system_or_bail
2626
system_log
2727
run_log
28+
pump_until
2829
2930
command_ok
3031
command_fails

src/test/perl/PostgresNode.pm

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,6 +2065,41 @@ sub wait_for_slot_catchup
20652065

20662066
=pod
20672067
2068+
=item $node->wait_for_log(regexp, offset)
2069+
2070+
Waits for the contents of the server log file, starting at the given offset, to
2071+
match the supplied regular expression. Checks the entire log if no offset is
2072+
given. Times out after $TestLib::timeout_default seconds.
2073+
2074+
If successful, returns the length of the entire log file, in bytes.
2075+
2076+
=cut
2077+
2078+
sub wait_for_log
2079+
{
2080+
my ($self, $regexp, $offset) = @_;
2081+
$offset = 0 unless defined $offset;
2082+
2083+
my $max_attempts = 10 * $TestLib::timeout_default;
2084+
my $attempts = 0;
2085+
2086+
while ($attempts < $max_attempts)
2087+
{
2088+
my $log = TestLib::slurp_file($self->logfile, $offset);
2089+
2090+
return $offset+length($log) if ($log =~ m/$regexp/);
2091+
2092+
# Wait 0.1 second before retrying.
2093+
usleep(100_000);
2094+
2095+
$attempts++;
2096+
}
2097+
2098+
croak "timed out waiting for match: $regexp";
2099+
}
2100+
2101+
=pod
2102+
20682103
=item $node->query_hash($dbname, $query, @columns)
20692104
20702105
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
@@ -36,6 +36,7 @@ our @EXPORT = qw(
3636
system_or_bail
3737
system_log
3838
run_log
39+
pump_until
3940
4041
command_ok
4142
command_fails
@@ -238,6 +239,36 @@ sub run_log
238239
return IPC::Run::run(@_);
239240
}
240241

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

0 commit comments

Comments
 (0)