Re: Implement generalized sub routine find_in_log for tap test

From: Dagfinn Ilmari Mannsåker <ilmari(at)ilmari(dot)org>
To: vignesh C <vignesh21(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Implement generalized sub routine find_in_log for tap test
Date: 2023-05-25 17:34:20
Message-ID: 87cz2otiw3.fsf@wibble.ilmari.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

vignesh C <vignesh21(at)gmail(dot)com> writes:

> Hi,
>
> The recovery tap test has 4 implementations of find_in_log sub routine
> for various uses, I felt we can generalize these and have a single
> function for the same. The attached patch is an attempt to have a
> generalized sub routine find_in_log which can be used by all of them.
> Thoughts?

+1 on factoring out this common code. Just a few comments on the implementation.

> diff --git a/src/test/perl/PostgreSQL/Test/Utils.pm b/src/test/perl/PostgreSQL/Test/Utils.pm
> index a27fac83d2..5c9b2f6c03 100644
> --- a/src/test/perl/PostgreSQL/Test/Utils.pm
> +++ b/src/test/perl/PostgreSQL/Test/Utils.pm
> @@ -67,6 +67,7 @@ our @EXPORT = qw(
> slurp_file
> append_to_file
> string_replace_file
> + find_in_log
> check_mode_recursive
> chmod_recursive
> check_pg_config
> @@ -579,6 +580,28 @@ sub string_replace_file
>
> =pod
>
> +
> +=item find_in_log(node, pattern, offset)
> +
> +Find pattern in logfile of node after offset byte.
> +
> +=cut
> +
> +sub find_in_log
> +{
> + my ($node, $pattern, $offset) = @_;
> +
> + $offset = 0 unless defined $offset;
> + my $log = PostgreSQL::Test::Utils::slurp_file($node->logfile);

Since this function is in the same package, there's no need to qualify
it with the full name. I know the callers you copied it from did, but
they wouldn't have had to either, since it's exported by default (in the
@EXPORT array above), unless the use statement has an explicit argument
list that excludes it.

> + return 0 if (length($log) <= 0 || length($log) <= $offset);
> +
> + $log = substr($log, $offset);

Also, the existing callers don't seem to have got the memo that
slurp_file() takes an optinal offset parameter, which will cause it to
seek to that postion before slurping the file, which is more efficient
than reading the whole file in and substr-ing it. There's not much
point in the length checks either, since regex-matching against an empty
string is very cheap (and if the provide pattern can match the empty
string the whole function call is rather pointless).

> + return $log =~ m/$pattern/;
> +}

All in all, it could be simplified to:

sub find_in_log {
my ($node, $pattern, $offset) = @_;

return slurp_file($node->logfile, $offset) =~ $pattern;
}

However, none of the other functions in ::Utils know anything about node
objects, which makes me think it should be a method on the node itself
(i.e. in PostgreSQL::Test::Cluster) instead. Also, I think log_contains
would be a better name, since it just returns a boolean. The name
find_in_log makes me think it would return the log lines matching the
pattern, or the position of the match in the file.

In that case, the slurp_file() call would have to be fully qualified,
since ::Cluster uses an empty import list to avoid polluting the method
namespace with imported functions.

- ilmari

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jacob Champion 2023-05-25 17:45:14 Re: Docs: Encourage strong server verification with SCRAM
Previous Message Stephen Frost 2023-05-25 17:29:12 Re: Docs: Encourage strong server verification with SCRAM