Skip to content

Commit 3c5b068

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/[email protected]
1 parent ef387be commit 3c5b068

File tree

2 files changed

+25
-42
lines changed

2 files changed

+25
-42
lines changed

src/test/perl/PostgresNode.pm

+9-37
Original file line numberDiff line numberDiff line change
@@ -1917,21 +1917,7 @@ sub connect_ok
19171917
@log_unlike = @{ $params{log_unlike} };
19181918
}
19191919

1920-
if (@log_like or @log_unlike)
1921-
{
1922-
# Don't let previous log entries match for this connection.
1923-
# On Windows, the truncation would not work, so rotate the log
1924-
# file before restarting the server afresh.
1925-
if ($TestLib::windows_os)
1926-
{
1927-
$self->rotate_logfile;
1928-
$self->restart;
1929-
}
1930-
else
1931-
{
1932-
truncate $self->logfile, 0;
1933-
}
1934-
}
1920+
my $log_location = -s $self->logfile;
19351921

19361922
# Never prompt for a password, any callers of this routine should
19371923
# have set up things properly, and this should not block.
@@ -1950,7 +1936,8 @@ sub connect_ok
19501936
}
19511937
if (@log_like or @log_unlike)
19521938
{
1953-
my $log_contents = TestLib::slurp_file($self->logfile);
1939+
my $log_contents = TestLib::slurp_file($self->logfile,
1940+
$log_location);
19541941

19551942
while (my $regex = shift @log_like)
19561943
{
@@ -2001,21 +1988,7 @@ sub connect_fails
20011988
@log_unlike = @{ $params{log_unlike} };
20021989
}
20031990

2004-
if (@log_like or @log_unlike)
2005-
{
2006-
# Don't let previous log entries match for this connection.
2007-
# On Windows, the truncation would not work, so rotate the log
2008-
# file before restarting the server afresh.
2009-
if ($TestLib::windows_os)
2010-
{
2011-
$self->rotate_logfile;
2012-
$self->restart;
2013-
}
2014-
else
2015-
{
2016-
truncate $self->logfile, 0;
2017-
}
2018-
}
1991+
my $log_location = -s $self->logfile;
20191992

20201993
# Never prompt for a password, any callers of this routine should
20211994
# have set up things properly, and this should not block.
@@ -2034,7 +2007,8 @@ sub connect_fails
20342007

20352008
if (@log_like or @log_unlike)
20362009
{
2037-
my $log_contents = TestLib::slurp_file($self->logfile);
2010+
my $log_contents = TestLib::slurp_file($self->logfile,
2011+
$log_location);
20382012

20392013
while (my $regex = shift @log_like)
20402014
{
@@ -2194,9 +2168,6 @@ sub command_checks_all
21942168
Run a command on the node, then verify that $expected_sql appears in the
21952169
server log file.
21962170
2197-
Reads the whole log file so be careful when working with large log outputs.
2198-
The log file is truncated prior to running the command, however.
2199-
22002171
=cut
22012172

22022173
sub issues_sql_like
@@ -2207,10 +2178,11 @@ sub issues_sql_like
22072178

22082179
local %ENV = $self->_get_env();
22092180

2210-
truncate $self->logfile, 0;
2181+
my $log_location = -s $self->logfile;
2182+
22112183
my $result = TestLib::run_log($cmd);
22122184
ok($result, "@$cmd exit code 0");
2213-
my $log = TestLib::slurp_file($self->logfile);
2185+
my $log = TestLib::slurp_file($self->logfile, $log_location);
22142186
like($log, $expected_sql, "$test_name: SQL found in server log");
22152187
return;
22162188
}

src/test/perl/TestLib.pm

+16-5
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use Carp;
4747
use Config;
4848
use Cwd;
4949
use Exporter 'import';
50-
use Fcntl qw(:mode);
50+
use Fcntl qw(:mode :seek);
5151
use File::Basename;
5252
use File::Find;
5353
use File::Spec;
@@ -124,7 +124,7 @@ BEGIN
124124
if ($windows_os)
125125
{
126126
require Win32API::File;
127-
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle));
127+
Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle setFilePointer));
128128
}
129129

130130
# Specifies whether to use Unix sockets for test setups. On
@@ -430,21 +430,27 @@ sub slurp_dir
430430

431431
=pod
432432
433-
=item slurp_file(filename)
433+
=item slurp_file(filename [, $offset])
434434
435-
Return the full contents of the specified file.
435+
Return the full contents of the specified file, beginning from an
436+
offset position if specified.
436437
437438
=cut
438439

439440
sub slurp_file
440441
{
441-
my ($filename) = @_;
442+
my ($filename, $offset) = @_;
442443
local $/;
443444
my $contents;
444445
if ($Config{osname} ne 'MSWin32')
445446
{
446447
open(my $in, '<', $filename)
447448
or croak "could not read \"$filename\": $!";
449+
if (defined($offset))
450+
{
451+
seek($in, $offset, SEEK_SET)
452+
or croak "could not seek \"$filename\": $!";
453+
}
448454
$contents = <$in>;
449455
close $in;
450456
}
@@ -454,6 +460,11 @@ sub slurp_file
454460
or croak "could not open \"$filename\": $^E";
455461
OsFHandleOpen(my $fh = IO::Handle->new(), $fHandle, 'r')
456462
or croak "could not read \"$filename\": $^E\n";
463+
if (defined($offset))
464+
{
465+
setFilePointer($fh, $offset, qw(FILE_BEGIN))
466+
or croak "could not seek \"$filename\": $^E\n";
467+
}
457468
$contents = <$fh>;
458469
CloseHandle($fHandle)
459470
or croak "could not close \"$filename\": $^E\n";

0 commit comments

Comments
 (0)