Skip to content

Commit 28af91b

Browse files
committed
Refactor routine to find single log content pattern in TAP tests
The same routine to check if a specific pattern can be found in the server logs was copied over four different test scripts. This refactors the whole to use a single routine located in PostgreSQL::Test::Cluster, named log_contains, to grab the contents of the server logs and check for a specific pattern. On HEAD, the code previously used assumed that slurp_file() could not handle an undefined offset, setting it to zero, but slurp_file() does do an extra fseek() before retrieving the log contents only if an offset is defined. In two places, the test was retrieving the full log contents with slurp_file() after calling substr() to apply an offset, ignoring that slurp_file() would be able to handle that. Backpatch all the way down to ease the introduction of new tests that could rely on the new routine. Author: Vignesh C Reviewed-by: Andrew Dunstan, Dagfinn Ilmari Mannsåker, Michael Paquier Discussion: https://postgr.es/m/CALDaNm0YSiLpjCmajwLfidQrFOrLNKPQir7s__PeVvh9U3uoTQ@mail.gmail.com Backpatch-through: 11
1 parent 30469a6 commit 28af91b

File tree

3 files changed

+23
-37
lines changed

3 files changed

+23
-37
lines changed

src/test/perl/PostgresNode.pm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2470,6 +2470,21 @@ sub log_check
24702470

24712471
=pod
24722472
2473+
=item $node->log_contains(pattern, offset)
2474+
2475+
Find pattern in logfile of node after offset byte.
2476+
2477+
=cut
2478+
2479+
sub log_contains
2480+
{
2481+
my ($self, $pattern, $offset) = @_;
2482+
2483+
return TestLib::slurp_file($self->logfile, $offset) =~ m/$pattern/;
2484+
}
2485+
2486+
=pod
2487+
24732488
=item $node->run_log(...)
24742489
24752490
Runs a shell command like TestLib::run_log, but with connection parameters set

src/test/recovery/t/019_replslot_limit.pl

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@
168168

169169
$node_standby->stop;
170170

171-
ok( !find_in_log(
172-
$node_standby,
171+
ok( !$node_standby->log_contains(
173172
"requested WAL segment [0-9A-F]+ has already been removed"),
174173
'check that required WAL segments are still available');
175174

@@ -191,8 +190,7 @@
191190
my $invalidated = 0;
192191
for (my $i = 0; $i < 10000; $i++)
193192
{
194-
if (find_in_log(
195-
$node_primary,
193+
if ($node_primary->log_contains(
196194
"invalidating slot \"rep1\" because its restart_lsn [0-9A-F/]+ exceeds max_slot_wal_keep_size",
197195
$logstart))
198196
{
@@ -215,7 +213,7 @@
215213
my $checkpoint_ended = 0;
216214
for (my $i = 0; $i < 10000; $i++)
217215
{
218-
if (find_in_log($node_primary, "checkpoint complete: ", $logstart))
216+
if ($node_primary->log_contains("checkpoint complete: ", $logstart))
219217
{
220218
$checkpoint_ended = 1;
221219
last;
@@ -245,8 +243,7 @@
245243
my $failed = 0;
246244
for (my $i = 0; $i < 10000; $i++)
247245
{
248-
if (find_in_log(
249-
$node_standby,
246+
if ($node_standby->log_contains(
250247
"requested WAL segment [0-9A-F]+ has already been removed",
251248
$logstart))
252249
{
@@ -351,8 +348,7 @@
351348
my $max_attempts = $TestLib::timeout_default;
352349
while ($max_attempts-- >= 0)
353350
{
354-
if (find_in_log(
355-
$node_primary3,
351+
if ($node_primary3->log_contains(
356352
"terminating process $senderpid to release replication slot \"rep3\"",
357353
$logstart))
358354
{
@@ -374,8 +370,7 @@
374370
$max_attempts = $TestLib::timeout_default;
375371
while ($max_attempts-- >= 0)
376372
{
377-
if (find_in_log(
378-
$node_primary3,
373+
if ($node_primary3->log_contains(
379374
'invalidating slot "rep3" because its restart_lsn', $logstart))
380375
{
381376
ok(1, "slot invalidation logged");
@@ -412,17 +407,3 @@ sub get_log_size
412407

413408
return (stat $node->logfile)[7];
414409
}
415-
416-
# find $pat in logfile of $node after $off-th byte
417-
sub find_in_log
418-
{
419-
my ($node, $pat, $off) = @_;
420-
421-
$off = 0 unless defined $off;
422-
my $log = TestLib::slurp_file($node->logfile);
423-
return 0 if (length($log) <= $off);
424-
425-
$log = substr($log, $off);
426-
427-
return $log =~ m/$pat/;
428-
}

src/test/recovery/t/033_replay_tsp_drops.pl

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,11 @@ sub test_tablespace
132132
{
133133
last
134134
if (
135-
find_in_log(
136-
$node_standby, qr!WARNING: ( [A-Z0-9]+:)? creating missing directory: pg_tblspc/!,
135+
$node_standby->log_contains(
136+
qr!WARNING: ( [A-Z0-9]+:)? creating missing directory: pg_tblspc/!,
137137
$logstart));
138138
usleep(100_000);
139139
}
140140
ok($max_attempts > 0, "invalid directory creation is detected");
141141

142142
done_testing();
143-
144-
# find $pat in logfile of $node after $off-th byte
145-
sub find_in_log
146-
{
147-
my ($node, $pat, $off) = @_;
148-
149-
my $log = PostgreSQL::Test::Utils::slurp_file($node->logfile, $off);
150-
151-
return $log =~ m/$pat/;
152-
}

0 commit comments

Comments
 (0)