Skip to content

Commit d48212c

Browse files
committed
Allow TestLib::slurp_file to skip contents, and use as needed
In order to avoid getting old logfile contents certain functions in PostgresNode were doing one of two things. On Windows it rotated the logfile and restarted the server, while elsewhere it truncated the log file. Both of these are unnecessary. We borrow from the buildfarm which does this instead: note the size of the logfile before we start, and then when fetching the logfile skip to that position before accumulating contents. This is spelled differently on Windows but the effect is the same. This is largely centralized in TestLib's slurp_file function, which has a new optional parameter, the offset to skip to before starting to reading the file. Code in the client becomes much neater. Backpatch to all live branches. Michael Paquier, slightly modified by me. Discussion: https://postgr.es/m/YHajnhcMAI3++pJL@paquier.xyz
1 parent 041f4ef commit d48212c

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/test/perl/PostgresNode.pm

+3-5
Original file line numberDiff line numberDiff line change
@@ -1456,9 +1456,6 @@ sub command_like
14561456
Run a command on the node, then verify that $expected_sql appears in the
14571457
server log file.
14581458
1459-
Reads the whole log file so be careful when working with large log outputs.
1460-
The log file is truncated prior to running the command, however.
1461-
14621459
=cut
14631460

14641461
sub issues_sql_like
@@ -1468,10 +1465,11 @@ sub issues_sql_like
14681465
local $ENV{PGHOST} = $self->host;
14691466
local $ENV{PGPORT} = $self->port;
14701467

1471-
truncate $self->logfile, 0;
1468+
my $log_location = -s $self->logfile;
1469+
14721470
my $result = TestLib::run_log($cmd);
14731471
ok($result, "@$cmd exit code 0");
1474-
my $log = TestLib::slurp_file($self->logfile);
1472+
my $log = TestLib::slurp_file($self->logfile, $log_location);
14751473
like($log, $expected_sql, "$test_name: SQL found in server log");
14761474
}
14771475

src/test/perl/TestLib.pm

+13-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use warnings;
1313
use Config;
1414
use Cwd;
1515
use Exporter 'import';
16+
use Fcntl qw(:mode :seek);
1617
use File::Basename;
1718
use File::Spec;
1819
use File::Temp ();
@@ -66,7 +67,7 @@ BEGIN
6667
if ($windows_os)
6768
{
6869
require Win32API::File;
69-
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle));
70+
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle setFilePointer));
7071
}
7172
}
7273

@@ -215,13 +216,18 @@ sub slurp_dir
215216

216217
sub slurp_file
217218
{
218-
my ($filename) = @_;
219+
my ($filename, $offset) = @_;
219220
local $/;
220221
my $contents;
221222
if ($Config{osname} ne 'MSWin32')
222223
{
223224
open(my $in, '<', $filename)
224225
or die "could not read \"$filename\": $!";
226+
if (defined($offset))
227+
{
228+
seek($in, $offset, SEEK_SET)
229+
or die "could not seek \"$filename\": $!";
230+
}
225231
$contents = <$in>;
226232
close $in;
227233
}
@@ -231,6 +237,11 @@ sub slurp_file
231237
or die "could not open \"$filename\": $^E";
232238
OsFHandleOpen(my $fh = IO::Handle->new(), $fHandle, 'r')
233239
or die "could not read \"$filename\": $^E\n";
240+
if (defined($offset))
241+
{
242+
setFilePointer($fh, $offset, qw(FILE_BEGIN))
243+
or die "could not seek \"$filename\": $^E\n";
244+
}
234245
$contents = <$fh>;
235246
CloseHandle($fHandle)
236247
or die "could not close \"$filename\": $^E\n";

0 commit comments

Comments
 (0)