def test_cancelled_connection_is_not_usable_until_cancellation(connect, loop): @asyncio.coroutine def inner(future, cursor): future.set_result(None) yield from cursor.execute("SELECT pg_sleep(10)") fut = asyncio.Future(loop=loop) conn = yield from connect() cur = yield from conn.cursor() task = ensure_future(inner(fut, cur), loop=loop) yield from fut yield from asyncio.sleep(0.1, loop=loop) task.cancel() for i in range(100): yield from asyncio.sleep(0) if conn._cancelling: break else: assert False, "Connection did not start cancelling" cur = yield from conn.cursor() with pytest.raises(RuntimeError) as e: yield from cur.execute('SELECT 1') assert str(e.value) == ('cursor.execute() called while connection ' 'is being cancelled')
def test_cancelled_connection_is_usable_asap(connect, loop): @asyncio.coroutine def inner(future, cursor): future.set_result(None) yield from cursor.execute("SELECT pg_sleep(10)") fut = asyncio.Future(loop=loop) conn = yield from connect() cur = yield from conn.cursor() task = ensure_future(inner(fut, cur), loop=loop) yield from fut yield from asyncio.sleep(0.1, loop=loop) task.cancel() delay = 0.001 for tick in range(100): yield from asyncio.sleep(delay, loop=loop) status = conn._conn.get_transaction_status() if status == psycopg2.extensions.TRANSACTION_STATUS_IDLE: cur = yield from conn.cursor() yield from cur.execute("SELECT 1") ret = yield from cur.fetchone() assert (1,) == ret break delay *= 2 else: assert False, "Cancelled connection transaction status never got idle"
def test_cancelled_connection_is_usable_asap(connect, loop): @asyncio.coroutine def inner(future, cursor): future.set_result(None) yield from cursor.execute("SELECT pg_sleep(10)") fut = asyncio.Future(loop=loop) conn = yield from connect() cur = yield from conn.cursor() task = ensure_future(inner(fut, cur), loop=loop) yield from fut yield from asyncio.sleep(0.1, loop=loop) task.cancel() delay = 0.001 for tick in range(100): yield from asyncio.sleep(delay, loop=loop) status = conn._conn.get_transaction_status() if status == psycopg2.extensions.TRANSACTION_STATUS_IDLE: cur = yield from conn.cursor() yield from cur.execute("SELECT 1") ret = yield from cur.fetchone() assert (1, ) == ret break delay *= 2 else: assert False, "Cancelled connection transaction status never got idle"
def test_cancelled_connection_is_not_usable_until_cancellation(connect, loop): @asyncio.coroutine def inner(future, cursor): future.set_result(None) yield from cursor.execute("SELECT pg_sleep(10)") fut = asyncio.Future(loop=loop) conn = yield from connect() cur = yield from conn.cursor() task = ensure_future(inner(fut, cur), loop=loop) yield from fut yield from asyncio.sleep(0.1, loop=loop) task.cancel() for i in range(100): yield from asyncio.sleep(0) if conn._cancelling: break else: assert False, "Connection did not start cancelling" # cur = yield from conn.cursor() with pytest.raises(RuntimeError) as e: yield from cur.execute('SELECT 1') assert str(e.value) == ('cursor.execute() called while connection ' 'is being cancelled')
def test_cancel_pending_op(connect, loop): conn = yield from connect() cur = yield from conn.cursor() task = ensure_future(cur.execute("SELECT pg_sleep(10)"), loop=loop) yield from asyncio.sleep(0.01, loop=loop) yield from conn.cancel() with pytest.raises(asyncio.CancelledError): yield from task
def test_cancel_pending_op(connect, loop): fut = asyncio.Future(loop=loop) @asyncio.coroutine def inner(): fut.set_result(None) yield from cur.execute("SELECT pg_sleep(10)") conn = yield from connect() cur = yield from conn.cursor() task = ensure_future(inner(), loop=loop) yield from fut yield from asyncio.sleep(0.1, loop=loop) yield from conn.cancel() with pytest.raises(asyncio.CancelledError): yield from task
async def test_cancel_pending_op(connect, loop): def exception_handler(loop_, context): assert context['message'] == context['exception'].pgerror assert context['future'].exception() is context['exception'] assert loop_ is loop loop.set_exception_handler(exception_handler) fut = asyncio.Future(loop=loop) async def inner(): fut.set_result(None) await cur.execute("SELECT pg_sleep(10)") conn = await connect() cur = await conn.cursor() task = ensure_future(inner(), loop=loop) await fut await asyncio.sleep(0.1, loop=loop) await conn.cancel() with pytest.raises(asyncio.CancelledError): await task