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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
#!perl
## Test asynchronous queries
use 5.008001;
use strict;
use warnings;
use lib 'blib/lib', 'blib/arch', 't';
use Test::More;
use Time::HiRes qw/sleep/;
use DBD::Pg ':async';
require 'dbdpg_test_setup.pl';
select(($|=1,select(STDERR),$|=1)[1]);
my $dbh = connect_database();
if (! $dbh) {
plan skip_all => 'Connection to database failed, cannot continue testing';
}
plan tests => 67;
isnt ($dbh, undef, 'Connect to database for async testing');
my ($t,$sth,$res);
my $pgversion = $dbh->{pg_server_version};
## First, test out do() in all its variants
$t=q{Method do() works as expected with no args };
eval {
$res = $dbh->do('SELECT 123');
};
is ($@, q{}, $t);
is ($res, 1, $t);
$t=q{Method do() works as expected with an unused attribute };
eval {
$res = $dbh->do('SELECT 123', {pg_nosuch => 'arg'});
};
is ($@, q{}, $t);
is ($res, 1, $t);
$t=q{Method do() works as expected with an unused attribute and a non-prepared param };
eval {
$res = $dbh->do('SET random_page_cost TO ?', undef, '2.2');
};
is ($@, q{}, $t);
is ($res, '0E0', $t);
$t=q{Method do() works as expected with an unused attribute and multiple real bind params };
eval {
$res = $dbh->do('SELECT count(*) FROM pg_class WHERE reltuples IN (?,?,?)', undef, 1,2,3);
};
is ($@, q{}, $t);
is ($res, 1, $t);
$t=q{Cancelling a non-async do() query gives an error };
eval {
$res = $dbh->pg_cancel();
};
like ($@, qr{No asynchronous query is running}, $t);
$t=q{Method do() works as expected with an asychronous flag };
eval {
$res = $dbh->do('SELECT 123', {pg_async => PG_ASYNC});
};
is ($@, q{}, $t);
is ($res, '0E0', $t);
$t=q{Database attribute "async_status" returns 1 after async query};
$res = $dbh->{pg_async_status};
is ($res, +1, $t);
sleep 1;
$t=q{Cancelling an async do() query works };
eval {
$res = $dbh->pg_cancel();
};
is ($@, q{}, $t);
$t=q{Database method pg_cancel returns a false value when cancellation works but finished};
is ($res, q{}, $t);
$t=q{Database attribute "async_status" returns -1 after pg_cancel};
$res = $dbh->{pg_async_status};
is ($res, -1, $t);
$t=q{Running do() after a cancelled query works};
eval {
$res = $dbh->do('SELECT 123');
};
is ($@, q{}, $t);
$t=q{Database attribute "async_status" returns 0 after normal query run};
$res = $dbh->{pg_async_status};
is ($res, 0, $t);
$t=q{Method pg_ready() fails after a non-async query};
eval {
$dbh->pg_ready();
};
like ($@, qr{No async}, $t);
$res = $dbh->do('SELECT 123', {pg_async => PG_ASYNC});
$t=q{Method pg_ready() works after a non-async query};
## Sleep a sub-second to make sure the server has caught up
sleep 0.2;
eval {
$res = $dbh->pg_ready();
};
is ($@, q{}, $t);
$t=q{Database method pg_ready() returns 1 after a completed async do()};
is ($res, 1, $t);
$res = $dbh->pg_ready();
$t=q{Database method pg_ready() returns true when called a second time};
is ($res, 1, $t);
$t=q{Database method pg_ready() returns 1 after a completed async do()};
is ($res, 1, $t);
$t=q{Cancelling an async do() query works };
eval {
$res = $dbh->pg_cancel();
};
is ($@, q{}, $t);
$t=q{Database method pg_cancel() returns expected false value for completed value};
is ($res, q{}, $t);
$t=q{Method do() runs after pg_cancel has cleared the async query};
eval {
$dbh->do('SELECT 456');
};
is ($@, q{}, $t);
$dbh->do(q{SELECT 'async2'}, {pg_async => PG_ASYNC});
$t=q{Method do() fails when async query has not been cleared};
eval {
$dbh->do(q{SELECT 'async_blocks'});
};
like ($@, qr{previous async}, $t);
$t=q{Database method pg_result works as expected};
eval {
$res = $dbh->pg_result();
};
is ($@, q{}, $t);
$t=q{Database method pg_result() returns correct value};
is ($res, 1, $t);
$t=q{Database method pg_result() fails when called twice};
eval {
$dbh->pg_result();
};
like ($@, qr{No async}, $t);
$t=q{Database method pg_cancel() fails when called after pg_result()};
eval {
$dbh->pg_cancel();
};
like ($@, qr{No async}, $t);
$t=q{Database method pg_ready() fails when called after pg_result()};
eval {
$dbh->pg_ready();
};
like ($@, qr{No async}, $t);
$t=q{Database method do() works after pg_result()};
eval {
$dbh->do('SELECT 123');
};
is ($@, q{}, $t);
SKIP: {
if ($pgversion < 80200) {
skip ('Need pg_sleep() to perform rest of async tests: your Postgres is too old', 14);
}
eval {
$dbh->do('SELECT pg_sleep(0)');
};
is ($@, q{}, 'Calling pg_sleep works as expected');
my $time = time();
eval {
$res = $dbh->do('SELECT pg_sleep(2)', {pg_async => PG_ASYNC});
};
$time = time()-$time;
$t = q{Database method do() returns right away when in async mode};
cmp_ok ($time, '<=', 1, $t);
$t=q{Method pg_ready() returns false when query is still running};
$res = $dbh->pg_ready();
is ($res, 0, $t);
pass ('Sleeping to allow query to finish');
sleep(3);
$t=q{Method pg_ready() returns true when query is finished};
$res = $dbh->pg_ready();
ok ($res, $t);
$t=q{Method do() will not work if async query not yet cleared};
eval {
$dbh->do('SELECT pg_sleep(2)', {pg_async => PG_ASYNC});
};
like ($@, qr{previous async}, $t);
$t=q{Database method pg_cancel() works while async query is running};
eval {
$res = $dbh->pg_cancel();
};
is ($@, q{}, $t);
$t=q{Database method pg_cancel returns false when query has already finished};
ok (!$res, $t);
$t=q{Database method pg_result() fails after async query has been cancelled};
eval {
$res = $dbh->pg_result();
};
like ($@, qr{No async}, $t);
$t=q{Database method do() cancels the previous async when requested};
eval {
$res = $dbh->do('SELECT pg_sleep(2)', {pg_async => PG_ASYNC + PG_OLDQUERY_CANCEL});
};
is ($@, q{}, $t);
$t=q{Database method pg_result works when async query is still running};
eval {
$res = $dbh->pg_result();
};
is ($@, q{}, $t);
## Now throw in some execute after the do()
$sth = $dbh->prepare('SELECT 567');
$t = q{Running execute after async do() gives an error};
$dbh->do('SELECT pg_sleep(2)', {pg_async => PG_ASYNC});
eval {
$res = $sth->execute();
};
like ($@, qr{previous async}, $t);
$t = q{Running execute after async do() works when told to cancel};
$sth = $dbh->prepare('SELECT 678', {pg_async => PG_OLDQUERY_CANCEL});
eval {
$sth->execute();
};
is ($@, q{}, $t);
$t = q{Running execute after async do() works when told to wait};
$dbh->do('SELECT pg_sleep(2)', {pg_async => PG_ASYNC});
$sth = $dbh->prepare('SELECT 678', {pg_async => PG_OLDQUERY_WAIT});
eval {
$sth->execute();
};
is ($@, q{}, $t);
$sth->finish();
} ## end of pg_sleep skip
$t=q{Method execute() works when prepare has PG_ASYNC flag};
$sth = $dbh->prepare('SELECT 123', {pg_async => PG_ASYNC});
eval {
$sth->execute();
};
is ($@, q{}, $t);
$t=q{Database attribute "async_status" returns 1 after prepare async};
$res = $dbh->{pg_async_status};
is ($res, 1, $t);
$t=q{Method do() fails when previous async prepare has been executed};
eval {
$dbh->do('SELECT 123');
};
like ($@, qr{previous async}, $t);
$t=q{Method execute() fails when previous async prepare has been executed};
eval {
$sth->execute();
};
like ($@, qr{previous async}, $t);
$t=q{Database method pg_cancel works if async query has already finished};
sleep 0.5;
eval {
$res = $sth->pg_cancel();
};
is ($@, q{}, $t);
$t=q{Statement method pg_cancel() returns a false value when cancellation works but finished};
is ($res, q{}, $t);
$t=q{Method do() fails when previous execute async has not been cleared};
$sth->execute();
$sth->finish(); ## Ideally, this would clear out the async, but it cannot at the moment
eval {
$dbh->do('SELECT 345');
};
like ($@, qr{previous async}, $t);
$dbh->pg_cancel;
$t=q{Directly after pg_cancel(), pg_async_status is -1};
is ($dbh->{pg_async_status}, -1, $t);
$t=q{Method execute() works when prepare has PG_ASYNC flag};
$sth->execute();
$t=q{After async execute, pg_async_status is 1};
is ($dbh->{pg_async_status}, 1, $t);
$t=q{Method pg_result works after a prepare/execute call};
eval {
$res = $dbh->pg_result;
};
is ($@, q{}, $t);
$t=q{Method pg_result() returns expected result after prepare/execute select};
is ($res, 1, $t);
$t=q{Method fetchall_arrayref works after pg_result};
eval {
$res = $sth->fetchall_arrayref();
};
is ($@, q{}, $t);
$t=q{Method fetchall_arrayref returns correct result after pg_result};
is_deeply ($res, [[123]], $t);
$dbh->do('CREATE TABLE dbd_pg_test5(id INT, t TEXT)');
$dbh->commit();
$sth->execute();
$t=q{Method prepare() works when passed in PG_OLDQUERY_CANCEL};
my $sth2;
my $SQL = 'INSERT INTO dbd_pg_test5(id) SELECT 123 UNION SELECT 456';
eval {
$sth2 = $dbh->prepare($SQL, {pg_async => PG_ASYNC + PG_OLDQUERY_CANCEL});
};
is ($@, q{}, $t);
$t=q{Fetch on cancelled statement handle fails};
eval {
$sth->fetch();
};
like ($@, qr{no statement executing}, $t);
$t=q{Method execute works after async + cancel prepare};
eval {
$sth2->execute();
};
is ($@, q{}, $t);
$t=q{Statement method pg_result works on async statement handle};
eval {
$res = $sth2->pg_result();
};
is ($@, q{}, $t);
$t=q{Statement method pg_result returns correct result after execute};
is ($res, 2, $t);
$sth2->execute();
$t=q{Database method pg_result works on async statement handle};
eval {
$res = $sth2->pg_result();
};
is ($@, q{}, $t);
$t=q{Database method pg_result returns correct result after execute};
is ($res, 2, $t);
$dbh->do('DROP TABLE dbd_pg_test5');
## TODO: More pg_sleep tests with execute
cleanup_database($dbh,'test');
$dbh->disconnect;
|