|
9 | 9 | use Test::More; |
10 | 10 | use Time::HiResqw(usleep); |
11 | 11 |
|
12 | | -my$tempdir = PostgreSQL::Test::Utils::tempdir; |
13 | | - |
14 | | -my$node = PostgreSQL::Test::Cluster->new('main'); |
15 | | -$node->init; |
16 | | -$node->start; |
17 | | - |
18 | 12 | # Test query canceling by sending SIGINT to a running psql |
19 | 13 | # |
20 | 14 | # There is, as of this writing, no documented way to get the PID of |
21 | 15 | # the process from IPC::Run. As a workaround, we have psql print its |
22 | 16 | # own PID (which is the parent of the shell launched by psql) to a |
23 | 17 | # file. |
24 | | -SKIP: |
| 18 | +if ($windows_os) |
25 | 19 | { |
26 | | -skip"cancel test requires a Unix shell", 2if$windows_os; |
27 | | - |
28 | | -local%ENV =$node->_get_env(); |
29 | | - |
30 | | -my ($stdin,$stdout,$stderr); |
31 | | - |
32 | | -# Test whether shell supports $PPID. It's part of POSIX, but some |
33 | | -# pre-/non-POSIX shells don't support it (e.g., NetBSD). |
34 | | -$stdin ="\\! echo\$PPID"; |
35 | | -IPC::Run::run(['psql','-X','-v','ON_ERROR_STOP=1' ], |
36 | | -'<', \$stdin,'>', \$stdout,'2>', \$stderr); |
37 | | -$stdout =~/^\d+$/or skip"shell apparently does not support\$PPID", 2; |
38 | | - |
39 | | -# Now start the real test |
40 | | -my$h = IPC::Run::start(['psql','-X','-v','ON_ERROR_STOP=1' ], |
41 | | -\$stdin, \$stdout, \$stderr); |
42 | | - |
43 | | -# Get the PID |
44 | | -$stdout =''; |
45 | | -$stderr =''; |
46 | | -$stdin ="\\! echo\$PPID >$tempdir/psql.pid\n"; |
47 | | -pump$hwhilelength$stdin; |
48 | | -my$count; |
49 | | -my$psql_pid; |
50 | | -until ( |
51 | | --s"$tempdir/psql.pid" |
52 | | -and ($psql_pid = |
53 | | -PostgreSQL::Test::Utils::slurp_file("$tempdir/psql.pid")) =~ |
54 | | -/^\d+\n/s) |
55 | | -{ |
56 | | -($count++ < 100 *$PostgreSQL::Test::Utils::timeout_default) |
57 | | -ordie"pid file did not appear"; |
58 | | -usleep(10_000); |
59 | | -} |
60 | | - |
61 | | -# Send sleep command and wait until the server has registered it |
62 | | -$stdin ="select pg_sleep($PostgreSQL::Test::Utils::timeout_default);\n"; |
63 | | -pump$hwhilelength$stdin; |
64 | | -$node->poll_query_until('postgres', |
65 | | -q{SELECT (SELECT count(*) FROM pg_stat_activity WHERE query ~ '^select pg_sleep') > 0;} |
66 | | -)ordie"timed out"; |
67 | | - |
68 | | -# Send cancel request |
69 | | -kill'INT',$psql_pid; |
70 | | - |
71 | | -my$result = finish$h; |
72 | | - |
73 | | -ok(!$result,'query failed as expected'); |
74 | | -like( |
75 | | -$stderr, |
76 | | -qr/canceling statement due to user request/, |
77 | | -'query was canceled'); |
| 20 | +planskip_all=>"cancel test requires a Unix shell"; |
78 | 21 | } |
79 | 22 |
|
| 23 | +my$tempdir = PostgreSQL::Test::Utils::tempdir; |
| 24 | + |
| 25 | +my$node = PostgreSQL::Test::Cluster->new('main'); |
| 26 | +$node->init; |
| 27 | +$node->start; |
| 28 | + |
| 29 | +local%ENV =$node->_get_env(); |
| 30 | + |
| 31 | +my ($stdin,$stdout,$stderr); |
| 32 | + |
| 33 | +# Test whether shell supports $PPID. It's part of POSIX, but some |
| 34 | +# pre-/non-POSIX shells don't support it (e.g., NetBSD). |
| 35 | +$stdin ="\\! echo\$PPID"; |
| 36 | +IPC::Run::run(['psql','-X','-v','ON_ERROR_STOP=1' ], |
| 37 | +'<', \$stdin,'>', \$stdout,'2>', \$stderr); |
| 38 | +$stdout =~/^\d+$/or skip"shell apparently does not support\$PPID", 2; |
| 39 | + |
| 40 | +# Now start the real test |
| 41 | +my$h = IPC::Run::start(['psql','-X','-v','ON_ERROR_STOP=1' ], |
| 42 | +\$stdin, \$stdout, \$stderr); |
| 43 | + |
| 44 | +# Get the PID |
| 45 | +$stdout =''; |
| 46 | +$stderr =''; |
| 47 | +$stdin ="\\! echo\$PPID >$tempdir/psql.pid\n"; |
| 48 | +pump$hwhilelength$stdin; |
| 49 | +my$count; |
| 50 | +my$psql_pid; |
| 51 | +until ( |
| 52 | +-s"$tempdir/psql.pid" |
| 53 | +and |
| 54 | + ($psql_pid = PostgreSQL::Test::Utils::slurp_file("$tempdir/psql.pid")) |
| 55 | + =~/^\d+\n/s) |
| 56 | +{ |
| 57 | +($count++ < 100 *$PostgreSQL::Test::Utils::timeout_default) |
| 58 | +ordie"pid file did not appear"; |
| 59 | +usleep(10_000); |
| 60 | +} |
| 61 | + |
| 62 | +# Send sleep command and wait until the server has registered it |
| 63 | +$stdin ="select pg_sleep($PostgreSQL::Test::Utils::timeout_default);\n"; |
| 64 | +pump$hwhilelength$stdin; |
| 65 | +$node->poll_query_until('postgres', |
| 66 | +q{SELECT (SELECT count(*) FROM pg_stat_activity WHERE query ~ '^select pg_sleep') > 0;} |
| 67 | +)ordie"timed out"; |
| 68 | + |
| 69 | +# Send cancel request |
| 70 | +kill'INT',$psql_pid; |
| 71 | + |
| 72 | +my$result = finish$h; |
| 73 | + |
| 74 | +ok(!$result,'query failed as expected'); |
| 75 | +like( |
| 76 | +$stderr, |
| 77 | +qr/canceling statement due to user request/, |
| 78 | +'query was canceled'); |
| 79 | + |
80 | 80 | done_testing(); |