Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d380ec9

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 26cf324 commit d380ec9

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
@@ -1673,9 +1673,6 @@ sub command_checks_all
16731673
Run a command on the node, then verify that $expected_sql appears in the
16741674
server log file.
16751675
1676-
Reads the whole log file so be careful when working with large log outputs.
1677-
The log file is truncated prior to running the command, however.
1678-
16791676
=cut
16801677

16811678
sub issues_sql_like
@@ -1687,10 +1684,11 @@ sub issues_sql_like
16871684
local $ENV{PGHOST} = $self->host;
16881685
local $ENV{PGPORT} = $self->port;
16891686

1690-
truncate $self->logfile, 0;
1687+
my $log_location = -s $self->logfile;
1688+
16911689
my $result = TestLib::run_log($cmd);
16921690
ok($result, "@$cmd exit code 0");
1693-
my $log = TestLib::slurp_file($self->logfile);
1691+
my $log = TestLib::slurp_file($self->logfile, $log_location);
16941692
like($log, $expected_sql, "$test_name: SQL found in server log");
16951693
return;
16961694
}

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;
@@ -82,7 +82,7 @@ BEGIN
8282
if ($windows_os)
8383
{
8484
require Win32API::File;
85-
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle));
85+
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle setFilePointer));
8686
}
8787
}
8888

@@ -259,13 +259,18 @@ sub slurp_dir
259259

260260
sub slurp_file
261261
{
262-
my ($filename) = @_;
262+
my ($filename, $offset) = @_;
263263
local $/;
264264
my $contents;
265265
if ($Config{osname} ne 'MSWin32')
266266
{
267267
open(my $in, '<', $filename)
268268
or die "could not read \"$filename\": $!";
269+
if (defined($offset))
270+
{
271+
seek($in, $offset, SEEK_SET)
272+
or die "could not seek \"$filename\": $!";
273+
}
269274
$contents = <$in>;
270275
close $in;
271276
}
@@ -275,6 +280,11 @@ sub slurp_file
275280
or die "could not open \"$filename\": $^E";
276281
OsFHandleOpen(my $fh = IO::Handle->new(), $fHandle, 'r')
277282
or die "could not read \"$filename\": $^E\n";
283+
if (defined($offset))
284+
{
285+
setFilePointer($fh, $offset, qw(FILE_BEGIN))
286+
or die "could not seek \"$filename\": $^E\n";
287+
}
278288
$contents = <$fh>;
279289
CloseHandle($fHandle)
280290
or die "could not close \"$filename\": $^E\n";

0 commit comments

Comments
 (0)