From: Daniel Gustafsson <daniel@yesql.se>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Fabien COELHO <coelho@cri.ensmp.fr>
Cc: Peter Eisentraut <peter.eisentraut@enterprisedb.com>
Cc: pgsql-committers@lists.postgresql.org
Subject: Re: pgsql: psql: Add test for query canceling
Date: Mon, 23 Aug 2021 13:12:28 +0200
Message-ID: <F8E93259-D461-48DB-B572-D5FCE66CBC6F@yesql.se> (raw)
In-Reply-To: <1983764.1629499242@sss.pgh.pa.us>
References: <E1mH14Q-0002gh-HS@gemulon.postgresql.org>
<DD279DFC-DD59-458E-B469-75DC941765A4@yesql.se>
<alpine.DEB.2.22.394.2108201736060.498998@pseudo>
<5F16EBAA-30D3-4C0D-840C-C2FF2739E370@yesql.se>
<1957503.1629485278@sss.pgh.pa.us>
<E92FAE70-66D7-4826-88D0-379004D385C7@yesql.se>
<1983764.1629499242@sss.pgh.pa.us>
> On 21 Aug 2021, at 00:40, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> Daniel Gustafsson <daniel@yesql.se> writes:
>> On 20 Aug 2021, at 20:47, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>>> I think you should drop the overly-cute bit with a SIGALRM handler,
>>> and instead have a loop-with-delay around an attempt to read the
>>> psql.pid file, after launching the psql run without an immediate
>>> wait for termination.
>
>> This could perhaps be done with a PostgresNode::interactive_psql
>> session?
>
> Yeah, we do have that infrastructure available in the
> 010_tab_completion.pl test.
In order to satisfy my own curiosity I decided to take a stab at this which
turned into the attached. I wasn’t able to reliably know that the pg_sleep
query had started except letting the timer expire, but that may very well be
the rust on my Perl skills showing.
Not sure if this moves the needle on this in either direction, but I figured I
should report back progress either way.
--
Daniel Gustafsson https://vmware.com/Attachments:psql_query_cancel_test.patch (application/octet-stream, 3.4K) download
From 98753c29da00612ccbefd6d47c23adde33cc91fd Mon Sep 17 00:00:00 2001
From: Daniel Gustafsson <daniel@yesql.se>
Date: Mon, 23 Aug 2021 13:04:32 +0200
Subject: [PATCH] Refactor psql query cancellation test to use interactive_psql
Utilize the interactive_psql client to inject query cancellation.
Discussion: https://www.postgresql.org/message-id/flat/E1mH14Q-0002gh-HS%40gemulon.postgresql.org
---
src/bin/psql/t/020_cancel.pl | 66 ++++++++++++++++++++----------------
1 file changed, 36 insertions(+), 30 deletions(-)
diff --git a/src/bin/psql/t/020_cancel.pl b/src/bin/psql/t/020_cancel.plindex 7713fff8e4..ed654869d4 100644--- a/src/bin/psql/t/020_cancel.pl+++ b/src/bin/psql/t/020_cancel.pl@@ -6,42 +6,48 @@ use warnings;
use PostgresNode;
use TestLib;
-use Test::More tests => 2;+use IPC::Run qw(pump finish timer);+use Test::More tests => 4;-my $tempdir = TestLib::tempdir;+plan skip_all => "Testsuite requires a Unix shell" if ($windows_os);
my $node = PostgresNode->new('main');
$node->init;
$node->start;
# Test query canceling by sending SIGINT to a running psql
-#+my $in;+my $out;+my $timer = timer(5);+my $h = $node->interactive_psql('postgres', \$in, \$out, $timer);+
# There is, as of this writing, no documented way to get the PID of
# the process from IPC::Run. As a workaround, we have psql print its
-# own PID (which is the parent of the shell launched by psql) to a-# file.-SKIP: {- skip "cancel test requires a Unix shell", 2 if $windows_os;-- local %ENV = $node->_get_env();-- my ($stdin, $stdout, $stderr);-- # Test whether shell supports $PPID. It's part of POSIX, but some- # pre-/non-POSIX shells don't support it (e.g., NetBSD, Solaris).- $stdin = "\\! echo \$PPID";- IPC::Run::run(['psql', '-X', '-v', 'ON_ERROR_STOP=1'], '<', \$stdin, '>', \$stdout, '2>', \$stderr);- $stdout =~ /^\d+$/ or skip "shell apparently does not support \$PPID", 2;-- local $SIG{ALRM} = sub {- my $psql_pid = TestLib::slurp_file("$tempdir/psql.pid");- kill 'INT', $psql_pid;- };- alarm 1;-- $stdin = "\\! echo \$PPID >$tempdir/psql.pid\nselect pg_sleep(3);";- my $result = IPC::Run::run(['psql', '-X', '-v', 'ON_ERROR_STOP=1'], '<', \$stdin, '>', \$stdout, '2>', \$stderr);-- ok(!$result, 'query failed as expected');- like($stderr, qr/canceling statement due to user request/, 'query was canceled');-}+# own PID (which is the parent of the shell launched by psql)+$out = '';+$timer->start(5);+$in = "\\! echo \"\$PPID\"\"x\"\n";+pump $h until ($out =~ /[^\D]+x/ || $timer->is_expired);+ok(!$timer->is_expired, "PPID timer expired");++# Grab PID from the output and clear the output buffer. If no PID was found+# then skip the rest of the tests as the platform doesn't support PPID.+(my $psql_pid = $out) =~ s/[^\d]//g;+$out = '';+scalar($psql_pid > 0) or skip "shell apparently does not support \$PPID", 3;++# Put the interactive client to sleep. Pumping on the output is tricky so for+# now allow the timer to expire and assume we are at sleep by then.+$timer->start(2);+$in .= "SELECT pg_sleep(15);\n";+pump $h until $timer->is_expired;++# Signal the client and await the expected output+$timer->start(10);+my $killed = kill 'INT', $psql_pid;+ok($killed eq '1', "psql was signaled");+pump $h until ($out =~ /request/ || $timer->is_expired);+like($out, qr/canceling statement due to user request/, 'query was canceled');+ok(!$timer->is_expired, "Sleep query timer expired");++$node->stop;--
2.24.3 (Apple Git-128)
=
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: daniel@yesql.se, tgl@sss.pgh.pa.us, coelho@cri.ensmp.fr, peter.eisentraut@enterprisedb.com
Subject: Re: pgsql: psql: Add test for query canceling
In-Reply-To: <F8E93259-D461-48DB-B572-D5FCE66CBC6F@yesql.se>
References: <E1mH14Q-0002gh-HS@gemulon.postgresql.org> <DD279DFC-DD59-458E-B469-75DC941765A4@yesql.se> <alpine.DEB.2.22.394.2108201736060.498998@pseudo> <5F16EBAA-30D3-4C0D-840C-C2FF2739E370@yesql.se> <1957503.1629485278@sss.pgh.pa.us> <E92FAE70-66D7-4826-88D0-379004D385C7@yesql.se> <1983764.1629499242@sss.pgh.pa.us> <F8E93259-D461-48DB-B572-D5FCE66CBC6F@yesql.se>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox