Skip to content

Commit 394e3ad

Browse files
authored
Improve testing connection closes for parallel test run (#600)
* close connection used to clear database get connection count from proper database instead of whole db * add connection closing
1 parent ebc364d commit 394e3ad

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

tests/test.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
TEST_MYSQL = True
203203
connect_args = {
204204
'user': DB_USER, 'password': DB_USER_PASSWORD,
205+
'database': DB_NAME,
205206
'read_timeout': SOCKET_CONNECT_TIMEOUT,
206207
'write_timeout': SOCKET_CONNECT_TIMEOUT,
207208
}
@@ -764,6 +765,7 @@ def drop_tables():
764765
'{}://{}:{}/{}'.format(DB_DRIVER, DB_HOST, DB_PORT, DB_NAME),
765766
connect_args=connect_args)
766767
metadata.drop_all(engine_raw)
768+
engine_raw.dispose()
767769

768770

769771
# Set this to False to not rebuild binaries on setup.
@@ -2605,11 +2607,11 @@ def tearDown(self):
26052607

26062608
def getActiveConnectionCount(self, cursor):
26072609
if TEST_MYSQL:
2608-
query = "SHOW STATUS WHERE `variable_name` = 'Threads_connected';"
2609-
cursor.execute(query)
2610-
return int(cursor.fetchone()[1])
2610+
query = "select count(*) from information_schema.processlist where db=%s;"
2611+
cursor.execute(query, [DB_NAME])
2612+
return int(cursor.fetchone()[0])
26112613
else:
2612-
cursor.execute('select count(*) from pg_stat_activity;')
2614+
cursor.execute('SELECT numbackends FROM pg_stat_database where datname=%s;', [DB_NAME])
26132615
return int(cursor.fetchone()[0])
26142616

26152617
def getConnectionLimit(self, connection=None):
@@ -2705,7 +2707,6 @@ def testClosingConnectionsWithDB(self):
27052707
connection.autocommit = True
27062708
with TestConnectionClosing.mysql_closing(connection.cursor()) as cursor:
27072709
current_connection_count = self.getActiveConnectionCount(cursor)
2708-
27092710
with self.get_connection():
27102711
self.assertEqual(self.getActiveConnectionCount(cursor),
27112712
current_connection_count+1)
@@ -10814,7 +10815,7 @@ def setUp(self):
1081410815

1081510816
def testPlainConnectionAfterDeny(self):
1081610817
async def _testPlainConnectionAfterDeny():
10817-
# We use raw connecitons to specify ssl='prefer'
10818+
# We use raw connections to specify ssl='prefer'
1081810819
# which would ask for ssl connection first.
1081910820
# And then after receiving a deny, it would ask for a plain connection
1082010821
conn = await asyncpg.connect(
@@ -10824,6 +10825,7 @@ async def _testPlainConnectionAfterDeny():
1082410825
**asyncpg_connect_args
1082510826
)
1082610827
await conn.fetch('SELECT 1', timeout=STATEMENT_TIMEOUT)
10828+
await conn.close()
1082710829

1082810830
loop = asyncio.new_event_loop() # create new to avoid concurrent usage of the loop in the current thread and allow parallel execution in the future
1082910831
loop.run_until_complete(_testPlainConnectionAfterDeny())
@@ -11025,6 +11027,7 @@ async def test():
1102511027
await conn.execute(insert_query, data['id'], data['value_bytes'])
1102611028
row = await conn.fetchrow(select_query, data['id'])
1102711029
self.assertEqual(data['value_bytes'], row['value_bytes'])
11030+
await conn.close()
1102811031

1102911032
loop = asyncio.new_event_loop()
1103011033
loop.run_until_complete(test())
@@ -11077,6 +11080,7 @@ async def test():
1107711080
# that our data is not saved due to the rollback.
1107811081
row = await conn.fetchrow(select_query, data['id'])
1107911082
self.assertEqual(row, None)
11083+
await conn.close()
1108011084

1108111085
loop = asyncio.new_event_loop()
1108211086
loop.run_until_complete(test())
@@ -11143,6 +11147,7 @@ async def test():
1114311147

1114411148
row = await conn.fetchrow(select_query, data['id'])
1114511149
self.assertEqual(row, None)
11150+
await conn.close()
1114611151

1114711152
loop = asyncio.new_event_loop()
1114811153
loop.run_until_complete(test())

0 commit comments

Comments
 (0)