| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
use strict; |
| 5 |
use warnings FATAL => 'all'; |
| 6 |
|
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
use Time::HiRes qw(usleep); |
| 11 |
|
| 12 |
# Test query canceling by sending SIGINT to a running psql |
| 13 |
if ($windows_os) |
| 14 |
{ |
| 15 |
plan skip_all => 'sending SIGINT on Windows terminates the test itself'; |
| 16 |
} |
| 17 |
|
| 18 |
my $node = PostgreSQL::Test::Cluster->new('main'); |
| 19 |
$node->init; |
| 20 |
$node->start; |
| 21 |
|
| 22 |
local %ENV = $node->_get_env(); |
| 23 |
|
| 24 |
my ($stdin, $stdout, $stderr); |
| 25 |
my $h = IPC::Run::start( |
| 26 |
[ |
| 27 |
'psql', '--no-psqlrc', '--set' => 'ON_ERROR_STOP=1', |
| 28 |
], |
| 29 |
'<' => \$stdin, |
| 30 |
'>' => \$stdout, |
| 31 |
'2>' => \$stderr); |
| 32 |
|
| 33 |
# Send sleep command and wait until the server has registered it |
| 34 |
$stdin = "select pg_sleep($PostgreSQL::Test::Utils::timeout_default);\n"; |
| 35 |
pump $h while length $stdin; |
| 36 |
$node->poll_query_until('postgres', |
| 37 |
q{SELECT (SELECT count(*) FROM pg_stat_activity WHERE query ~ '^select pg_sleep') > 0;} |
| 38 |
) or die "timed out"; |
| 39 |
|
| 40 |
# Send cancel request |
| 41 |
$h->signal('INT'); |
| 42 |
|
| 43 |
my $result = finish $h; |
| 44 |
|
| 45 |
ok(!$result, 'query failed as expected'); |
| 46 |
like( |
| 47 |
$stderr, |
| 48 |
qr/canceling statement due to user request/, |
| 49 |
'query was canceled'); |
| 50 |
|
| 51 |
done_testing(); |