Skip to content

Commit

Permalink
Fix a bunch of ResourceWarnings in the test suite
Browse files Browse the repository at this point in the history
Make sure things get freed properly otherwise Python gets unhappy
with ResourceWarnings.
  • Loading branch information
elprans committed Nov 7, 2021
1 parent 3a90fef commit 2f4fe53
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
6 changes: 6 additions & 0 deletions asyncpg/_testbase/fuzzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ async def handle(self):
return_when=asyncio.FIRST_COMPLETED)

finally:
if self.proxy_to_backend_task is not None:
self.proxy_to_backend_task.cancel()

if self.proxy_from_backend_task is not None:
self.proxy_from_backend_task.cancel()

# Asyncio fails to properly remove the readers and writers
# when the task doing recv() or send() is cancelled, so
# we must remove the readers and writers manually before
Expand Down
2 changes: 1 addition & 1 deletion asyncpg/protocol/protocol.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ cdef class BaseProtocol(CoreProtocol):
pass
finally:
self.waiter = None
self.transport.abort()
self.transport.abort()

def _request_cancel(self):
self.cancel_waiter = self.create_future()
Expand Down
13 changes: 8 additions & 5 deletions tests/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1604,11 +1604,14 @@ async def test_nossl_connection_prefer_cancel(self):
dsn='postgresql://foo/postgres?sslmode=prefer',
host='localhost',
user='ssl_user')
self.assertFalse(con._protocol.is_ssl)
with self.assertRaises(asyncio.TimeoutError):
await con.execute('SELECT pg_sleep(5)', timeout=0.5)
val = await con.fetchval('SELECT 123')
self.assertEqual(val, 123)
try:
self.assertFalse(con._protocol.is_ssl)
with self.assertRaises(asyncio.TimeoutError):
await con.execute('SELECT pg_sleep(5)', timeout=0.5)
val = await con.fetchval('SELECT 123')
self.assertEqual(val, 123)
finally:
await con.close()

async def test_nossl_connection_pool(self):
pool = await self.create_pool(
Expand Down
3 changes: 3 additions & 0 deletions tests/test_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,7 @@ async def test_pool_expire_connections(self):
await pool.release(con)

self.assertIsNone(pool._holders[0]._con)
await pool.close()

async def test_pool_set_connection_args(self):
pool = await self.create_pool(database='postgres',
Expand Down Expand Up @@ -883,6 +884,8 @@ async def test_pool_set_connection_args(self):
con = await pool.acquire()
self.assertEqual(con.get_settings().application_name,
'set_conn_args_test_2')
await pool.release(con)
await pool.close()

async def test_pool_init_race(self):
pool = self.create_pool(database='postgres', min_size=1, max_size=1)
Expand Down

0 comments on commit 2f4fe53

Please sign in to comment.