Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q2ErE-0003LZ-SA for pgsql-hackers@arkaria.postgresql.org; Thu, 25 May 2023 17:34:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q2ErD-0004jN-EQ for pgsql-hackers@arkaria.postgresql.org; Thu, 25 May 2023 17:34:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q2ErC-0004iv-Nt for pgsql-hackers@lists.postgresql.org; Thu, 25 May 2023 17:34:35 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q2Er4-0026qq-Hj for pgsql-hackers@lists.postgresql.org; Thu, 25 May 2023 17:34:34 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id 1BFA03200564; Thu, 25 May 2023 13:34:24 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Thu, 25 May 2023 13:34:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ilmari.org; h=cc :cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to; s=fm2; t=1685036063; x=1685122463; bh=nZ eRkIHT2SdYD+X/3SihVywMVDvBkr1xdBAMkbBkRrQ=; b=QIo1ko9ZmcfbaxP1Qh brVcVrpO5F83fR5s47ZSDOfKUUQ914S09Ais9KCLHbLQ0bHeRx6wjM2tvu/WlxBv c6Pb7hmpLGharJuzqUeaA5uhUY0ydFi4x50KpobSDO3aZuZmOdVqtmffGbsRNXx/ YKEWLuE+nGw6SNxB4tmdYd4FSanYTGHRir8Kt2LfPe06+i7DGvfx5Tc2CaVdBiwH n/rzYCEWr9YvPcEc2DVDoEG4DYA1tgy1oGzWPFELFX+LVIBPfxcg/30a9ro3I2r2 JIIDVJaGP/40/dsx3tj0JP1apFDo++qB15QUlNruakVL+uqnymJfMPr+y1G64AYp ND7g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; t=1685036063; x=1685122463; bh=nZeRkIHT2SdYD +X/3SihVywMVDvBkr1xdBAMkbBkRrQ=; b=RlPyRpJ7Ov8Zn8BYo+QWKCqtm4ffT LHHUrImHkUlI8TeOMSMjmEfLr0FOHy8mdy4pgfH1h3A3Gbcuqgh9qot+aJXD0wbh GI17W8jwJ1Kk91/8TScSiamvz2MTgy407ultbXLBqktnT5Fq3HsaKpFMaYzo63oU G/u2vFzxkx2fatGLMbNNEBAEIYSKpdeVOqwa2iWoNa6Lu37CwGmK8fs0BKoDsgmT e6SBBjA8s6L/7CpGQuOFrlXFvg2mJ37aq61TdZvQ9sSAdQyCDdCsmGK7iIe6lhid qq6Ge9uHEBlyZtLpeIHP0Zh4lTvSClRRyQyq9GP7/uK6OAYbQ0b9vHKLg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrfeejjedguddugecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefhvfevufhfffgjkfgfgggtsehttdertddtreejnecuhfhrohhmpeffrghg fhhinhhnucfklhhmrghrihcuofgrnhhnshonkhgvrhcuoehilhhmrghrihesihhlmhgrrh hirdhorhhgqeenucggtffrrghtthgvrhhnpeekleejueefhfffkefhkedtfeeluedvieei hfehfeeffeeggeduffduheejjeeigfenucevlhhushhtvghrufhiiigvpedtnecurfgrrh grmhepmhgrihhlfhhrohhmpehilhhmrghrihesihhlmhgrrhhirdhorhhg X-ME-Proxy: Feedback-ID: i1ff147bf:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 25 May 2023 13:34:22 -0400 (EDT) From: =?utf-8?Q?Dagfinn_Ilmari_Manns=C3=A5ker?= To: vignesh C Cc: PostgreSQL Hackers Subject: Re: Implement generalized sub routine find_in_log for tap test References: Date: Thu, 25 May 2023 18:34:20 +0100 In-Reply-To: (vignesh C.'s message of "Thu, 25 May 2023 19:53:51 +0530") Message-ID: <87cz2otiw3.fsf@wibble.ilmari.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk vignesh C 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