async def test_async_wait_cancel_01(self): # Test that client protocol handles waits interrupted # by closing. lock_key = tb.gen_lock_key() con2 = await self.connect(database=self.con.dbname) await self.con.fetchone( 'select sys::advisory_lock(<int64>$0)', lock_key) try: async with tg.TaskGroup() as g: async def exec_to_fail(): with self.assertRaises(ConnectionAbortedError): await con2.fetchall( 'select sys::advisory_lock(<int64>$0)', lock_key) g.create_task(exec_to_fail()) await asyncio.sleep(0.1) await con2.close() finally: self.assertEqual( await self.con.fetchall( 'select sys::advisory_unlock(<int64>$0)', lock_key), [True])
async def test_async_wait_cancel_01(self): underscored_lock = await self.con.query_one(""" SELECT EXISTS( SELECT schema::Function FILTER .name = 'sys::_advisory_lock' ) """) if not underscored_lock: self.skipTest("No sys::_advisory_lock function") # Test that client protocol handles waits interrupted # by closing. lock_key = tb.gen_lock_key() con2 = await self.connect(database=self.con.dbname) async with self.con.raw_transaction() as tx: self.assertTrue(await tx.query_one( 'select sys::_advisory_lock(<int64>$0)', lock_key)) try: async with tg.TaskGroup() as g: fut = asyncio.Future() async def exec_to_fail(): with self.assertRaises(( edgedb.ClientConnectionClosedError, ConnectionResetError, )): async with con2.raw_transaction() as tx2: fut.set_result(None) await tx2.query( 'select sys::_advisory_lock(<int64>$0)', lock_key, ) g.create_task(exec_to_fail()) await asyncio.wait_for(fut, 1) await asyncio.sleep(0.1) with self.assertRaises(asyncio.TimeoutError): # aclose() will ask the server nicely to disconnect, # but since the server is blocked on the lock, # aclose() will timeout and get cancelled, which, # in turn, will terminate the connection rudely, # and exec_to_fail() will get ConnectionResetError. await compat.wait_for(con2.aclose(), timeout=0.5) finally: self.assertEqual( await tx.query( 'select sys::_advisory_unlock(<int64>$0)', lock_key), [True])
async def test_async_wait_cancel_01(self): underscored_lock = await self.con.query_one(""" SELECT EXISTS( SELECT schema::Function FILTER .name = 'sys::_advisory_lock' ) """) if not underscored_lock: self.skipTest("No sys::_advisory_lock function") # Test that client protocol handles waits interrupted # by closing. lock_key = tb.gen_lock_key() con2 = await self.connect(database=self.con.dbname) async with self.con.transaction(): await self.con.query_one('select sys::_advisory_lock(<int64>$0)', lock_key) try: async with tg.TaskGroup() as g: async def exec_to_fail(): with self.assertRaises(ConnectionAbortedError): async with con2.transaction(): await con2.query( 'select sys::_advisory_lock(<int64>$0)', lock_key, ) g.create_task(exec_to_fail()) await asyncio.sleep(0.1) await con2.aclose() finally: self.assertEqual( await self.con.query('select sys::_advisory_unlock(<int64>$0)', lock_key), [True])
async def test_async_wait_cancel_01(self): underscored_lock = await self.client.query_single(""" SELECT EXISTS( SELECT schema::Function FILTER .name = 'sys::_advisory_lock' ) """) if not underscored_lock: self.skipTest("No sys::_advisory_lock function") # Test that client protocol handles waits interrupted # by closing. lock_key = tb.gen_lock_key() client = self.client.with_retry_options(RetryOptions(attempts=1)) client2 = self.test_client( database=self.client.dbname).with_retry_options( RetryOptions(attempts=1)) await client2.ensure_connected() async for tx in client.transaction(): async with tx: self.assertTrue(await tx.query_single( 'select sys::_advisory_lock(<int64>$0)', lock_key)) try: async with tg.TaskGroup() as g: fut = asyncio.Future() async def exec_to_fail(): with self.assertRaises(( edgedb.ClientConnectionClosedError, ConnectionResetError, )): async for tx2 in client2.transaction(): async with tx2: # start the lazy transaction await tx2.query('SELECT 42;') fut.set_result(None) await tx2.query( 'select sys::_advisory_lock(' + '<int64>$0)', lock_key, ) g.create_task(exec_to_fail()) await asyncio.wait_for(fut, 1) await asyncio.sleep(0.1) with self.assertRaises(asyncio.TimeoutError): # aclose() will ask the server nicely to # disconnect, but since the server is blocked on # the lock, aclose() will timeout and get # cancelled, which, in turn, will terminate the # connection rudely, and exec_to_fail() will get # ConnectionResetError. await compat.wait_for(client2.aclose(), timeout=0.5) finally: self.assertEqual( await tx.query('select sys::_advisory_unlock(<int64>$0)', lock_key), [True])