Skip to content

Commit 4c92981

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 c7f0275 commit 4c92981

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/test/perl/PostgresNode.pm

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,9 +1616,6 @@ sub command_checks_all
16161616
Run a command on the node, then verify that $expected_sql appears in the
16171617
server log file.
16181618
1619-
Reads the whole log file so be careful when working with large log outputs.
1620-
The log file is truncated prior to running the command, however.
1621-
16221619
=cut
16231620

16241621
sub issues_sql_like
@@ -1628,10 +1625,11 @@ sub issues_sql_like
16281625
local $ENV{PGHOST} = $self->host;
16291626
local $ENV{PGPORT} = $self->port;
16301627

1631-
truncate $self->logfile, 0;
1628+
my $log_location = -s $self->logfile;
1629+
16321630
my $result = TestLib::run_log($cmd);
16331631
ok($result, "@$cmd exit code 0");
1634-
my $log = TestLib::slurp_file($self->logfile);
1632+
my $log = TestLib::slurp_file($self->logfile, $log_location);
16351633
like($log, $expected_sql, "$test_name: SQL found in server log");
16361634
return;
16371635
}

src/test/perl/TestLib.pm

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use warnings;
1313
use Config;
1414
use Cwd;
1515
use Exporter 'import';
16-
use Fcntl qw(:mode);
16+
use Fcntl qw(:mode :seek);
1717
use File::Basename;
1818
use File::Find;
1919
use File::Spec;
@@ -80,7 +80,7 @@ BEGIN
8080
if ($windows_os)
8181
{
8282
require Win32API::File;
83-
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle));
83+
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle setFilePointer));
8484
}
8585
}
8686

@@ -253,13 +253,18 @@ sub slurp_dir
253253

254254
sub slurp_file
255255
{
256-
my ($filename) = @_;
256+
my ($filename, $offset) = @_;
257257
local $/;
258258
my $contents;
259259
if ($Config{osname} ne 'MSWin32')
260260
{
261261
open(my $in, '<', $filename)
262262
or die "could not read \"$filename\": $!";
263+
if (defined($offset))
264+
{
265+
seek($in, $offset, SEEK_SET)
266+
or die "could not seek \"$filename\": $!";
267+
}
263268
$contents = <$in>;
264269
close $in;
265270
}
@@ -269,6 +274,11 @@ sub slurp_file
269274
or die "could not open \"$filename\": $^E";
270275
OsFHandleOpen(my $fh = IO::Handle->new(), $fHandle, 'r')
271276
or die "could not read \"$filename\": $^E\n";
277+
if (defined($offset))
278+
{
279+
setFilePointer($fh, $offset, qw(FILE_BEGIN))
280+
or die "could not seek \"$filename\": $^E\n";
281+
}
272282
$contents = <$fh>;
273283
CloseHandle($fHandle)
274284
or die "could not close \"$filename\": $^E\n";

0 commit comments

Comments
 (0)