aboutsummaryrefslogtreecommitdiff
path: root/src/bin/psql/t/020_cancel.pl
blob: 154a24bca98c79db70664b0ae430c544f92f8ff6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Copyright (c) 2021-2025, PostgreSQL Global Development Group

use strict;
use warnings FATAL => 'all';

use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
use Time::HiRes qw(usleep);

# Test query canceling by sending SIGINT to a running psql
if ($windows_os)
{
	plan skip_all => 'sending SIGINT on Windows terminates the test itself';
}

my $node = PostgreSQL::Test::Cluster->new('main');
$node->init;
$node->start;

local %ENV = $node->_get_env();

my ($stdin, $stdout, $stderr);
my $h = IPC::Run::start(
	[
		'psql', '--no-psqlrc', '--set' => 'ON_ERROR_STOP=1',
	],
	'<' => \$stdin,
	'>' => \$stdout,
	'2>' => \$stderr);

# Send sleep command and wait until the server has registered it
$stdin = "select pg_sleep($PostgreSQL::Test::Utils::timeout_default);\n";
pump $h while length $stdin;
$node->poll_query_until('postgres',
	q{SELECT (SELECT count(*) FROM pg_stat_activity WHERE query ~ '^select pg_sleep') > 0;}
) or die "timed out";

# Send cancel request
$h->signal('INT');

my $result = finish $h;

ok(!$result, 'query failed as expected');
like(
	$stderr,
	qr/canceling statement due to user request/,
	'query was canceled');

done_testing();