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

Commit e480c6d

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 0e8acd3 commit e480c6d

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/test/perl/PostgresNode.pm

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,9 +1781,6 @@ sub command_checks_all
17811781
Run a command on the node, then verify that $expected_sql appears in the
17821782
server log file.
17831783
1784-
Reads the whole log file so be careful when working with large log outputs.
1785-
The log file is truncated prior to running the command, however.
1786-
17871784
=cut
17881785

17891786
sub issues_sql_like
@@ -1795,10 +1792,11 @@ sub issues_sql_like
17951792
local $ENV{PGHOST} = $self->host;
17961793
local $ENV{PGPORT} = $self->port;
17971794

1798-
truncate $self->logfile, 0;
1795+
my $log_location = -s $self->logfile;
1796+
17991797
my $result = TestLib::run_log($cmd);
18001798
ok($result, "@$cmd exit code 0");
1801-
my $log = TestLib::slurp_file($self->logfile);
1799+
my $log = TestLib::slurp_file($self->logfile, $log_location);
18021800
like($log, $expected_sql, "$test_name: SQL found in server log");
18031801
return;
18041802
}

src/test/perl/TestLib.pm

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use warnings;
4646
use Config;
4747
use Cwd;
4848
use Exporter 'import';
49-
use Fcntl qw(:mode);
49+
use Fcntl qw(:mode :seek);
5050
use File::Basename;
5151
use File::Find;
5252
use File::Spec;
@@ -117,7 +117,7 @@ BEGIN
117117
if ($windows_os)
118118
{
119119
require Win32API::File;
120-
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle));
120+
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle setFilePointer));
121121
}
122122

123123
# Specifies whether to use Unix sockets for test setups. On
@@ -402,21 +402,27 @@ sub slurp_dir
402402

403403
=pod
404404
405-
=item slurp_file(filename)
405+
=item slurp_file(filename [, $offset])
406406
407-
Return the full contents of the specified file.
407+
Return the full contents of the specified file, beginning from an
408+
offset position if specified.
408409
409410
=cut
410411

411412
sub slurp_file
412413
{
413-
my ($filename) = @_;
414+
my ($filename, $offset) = @_;
414415
local $/;
415416
my $contents;
416417
if ($Config{osname} ne 'MSWin32')
417418
{
418419
open(my $in, '<', $filename)
419420
or die "could not read \"$filename\": $!";
421+
if (defined($offset))
422+
{
423+
seek($in, $offset, SEEK_SET)
424+
or die "could not seek \"$filename\": $!";
425+
}
420426
$contents = <$in>;
421427
close $in;
422428
}
@@ -426,6 +432,11 @@ sub slurp_file
426432
or die "could not open \"$filename\": $^E";
427433
OsFHandleOpen(my $fh = IO::Handle->new(), $fHandle, 'r')
428434
or die "could not read \"$filename\": $^E\n";
435+
if (defined($offset))
436+
{
437+
setFilePointer($fh, $offset, qw(FILE_BEGIN))
438+
or die "could not seek \"$filename\": $^E\n";
439+
}
429440
$contents = <$fh>;
430441
CloseHandle($fHandle)
431442
or die "could not close \"$filename\": $^E\n";

0 commit comments

Comments
 (0)