]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
psql: Add test for query canceling
authorPeter Eisentraut <peter@eisentraut.org>
Fri, 20 Aug 2021 09:28:56 +0000 (11:28 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Fri, 20 Aug 2021 09:38:16 +0000 (11:38 +0200)
Query canceling in psql was accidentally broken by
3a5130672296ed4e682403a77a9a3ad3d21cef75 (since reverted), so having
some test coverage for that seems useful.

Reviewed-by: Fabien COELHO <coelho@cri.ensmp.fr>
Discussion: https://www.postgresql.org/message-id/18c78a01-4a34-9dd4-f78b-6860f1420c8e@enterprisedb.com

src/bin/psql/t/020_cancel.pl [new file with mode: 0644]

diff --git a/src/bin/psql/t/020_cancel.pl b/src/bin/psql/t/020_cancel.pl
new file mode 100644 (file)
index 0000000..60b6d1f
--- /dev/null
@@ -0,0 +1,40 @@
+
+# Copyright (c) 2021, PostgreSQL Global Development Group
+
+use strict;
+use warnings;
+
+use PostgresNode;
+use TestLib;
+use Test::More tests => 2;
+
+my $tempdir = TestLib::tempdir;
+
+my $node = PostgresNode->new('main');
+$node->init;
+$node->start;
+
+# Test query canceling by sending SIGINT to a running psql
+#
+# 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();
+
+       local $SIG{ALRM} = sub {
+               my $psql_pid = TestLib::slurp_file("$tempdir/psql.pid");
+               kill 'INT', $psql_pid;
+       };
+       alarm 1;
+
+       my $stdin = "\\! echo \$PPID >$tempdir/psql.pid\nselect pg_sleep(3);";
+       my ($stdout, $stderr);
+       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');
+}