|
3 | 3 | import gc
|
4 | 4 | import psycopg2
|
5 | 5 | import psycopg2.extras
|
| 6 | +import psycopg2.extensions |
6 | 7 | import pytest
|
7 | 8 | import socket
|
8 | 9 | import time
|
@@ -287,6 +288,65 @@ def inner():
|
287 | 288 | yield from task
|
288 | 289 |
|
289 | 290 |
|
| 291 | +@asyncio.coroutine |
| 292 | +def test_cancelled_connection_is_usable_asap(connect, loop): |
| 293 | + @asyncio.coroutine |
| 294 | + def inner(future, cursor): |
| 295 | + future.set_result(None) |
| 296 | + yield from cursor.execute("SELECT pg_sleep(10)") |
| 297 | + |
| 298 | + fut = asyncio.Future(loop=loop) |
| 299 | + conn = yield from connect() |
| 300 | + cur = yield from conn.cursor() |
| 301 | + task = ensure_future(inner(fut, cur), loop=loop) |
| 302 | + yield from fut |
| 303 | + yield from asyncio.sleep(0.1, loop=loop) |
| 304 | + |
| 305 | + task.cancel() |
| 306 | + |
| 307 | + for tick in range(100): |
| 308 | + yield from asyncio.sleep(0, loop=loop) |
| 309 | + status = conn._conn.get_transaction_status() |
| 310 | + if status == psycopg2.extensions.TRANSACTION_STATUS_IDLE: |
| 311 | + cur = yield from conn.cursor() |
| 312 | + yield from cur.execute("SELECT 1") |
| 313 | + ret = yield from cur.fetchone() |
| 314 | + assert (1,) == ret |
| 315 | + break |
| 316 | + else: |
| 317 | + assert False, "Cancelled connection transaction status never got idle" |
| 318 | + |
| 319 | + |
| 320 | +@asyncio.coroutine |
| 321 | +def test_cancelled_connection_is_not_usable_until_cancellation(connect, loop): |
| 322 | + @asyncio.coroutine |
| 323 | + def inner(future, cursor): |
| 324 | + future.set_result(None) |
| 325 | + yield from cursor.execute("SELECT pg_sleep(10)") |
| 326 | + |
| 327 | + fut = asyncio.Future(loop=loop) |
| 328 | + conn = yield from connect() |
| 329 | + cur = yield from conn.cursor() |
| 330 | + task = ensure_future(inner(fut, cur), loop=loop) |
| 331 | + yield from fut |
| 332 | + yield from asyncio.sleep(0.1, loop=loop) |
| 333 | + |
| 334 | + task.cancel() |
| 335 | + |
| 336 | + for i in range(100): |
| 337 | + yield from asyncio.sleep(0) |
| 338 | + if conn._cancelling: |
| 339 | + break |
| 340 | + else: |
| 341 | + assert False, "Connection did not start cancelling" |
| 342 | + |
| 343 | + cur = yield from conn.cursor() |
| 344 | + with pytest.raises(RuntimeError) as e: |
| 345 | + yield from cur.execute('SELECT 1') |
| 346 | + assert str(e.value) == ('cursor.execute() called while connection ' |
| 347 | + 'is being cancelled') |
| 348 | + |
| 349 | + |
290 | 350 | @asyncio.coroutine
|
291 | 351 | def test_close2(connect, loop):
|
292 | 352 | conn = yield from connect()
|
|
0 commit comments