Skip to content

Commit cc13a45

Browse files
Copilotpetyaslavova
andcommitted
Improve test design based on code review feedback
- Simplify lock checking test by using outer scope instead of passing pool reference - Make performance test more maintainable with configurable timeouts and better error messages Co-authored-by: petyaslavova <[email protected]>
1 parent 0172e32 commit cc13a45

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

tests/test_asyncio/test_connection_pool.py

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -228,41 +228,27 @@ async def test_lock_not_held_during_connection_establishment(self):
228228
ensure_connection call, which involves socket connection and handshake.
229229
This is important for performance under high load.
230230
"""
231+
lock_states = []
231232

232233
class SlowConnectConnection(DummyConnection):
233234
"""Connection that simulates slow connection establishment"""
234235

235-
def __init__(self, **kwargs):
236-
super().__init__(**kwargs)
237-
self.connect_called = False
238-
self.lock_held_during_connect = None
239-
240236
async def connect(self):
241-
self.connect_called = True
242237
# Check if the pool's lock is held during connection
243-
pool = self.kwargs.get("_pool")
244-
if pool:
245-
self.lock_held_during_connect = pool._lock.locked()
238+
# We access the pool through the outer scope
239+
lock_states.append(pool._lock.locked())
246240
# Simulate slow connection
247241
await asyncio.sleep(0.01)
248242
self._connected = True
249243

250-
async with self.get_pool(
251-
connection_class=SlowConnectConnection,
252-
connection_kwargs={"_pool": None},
253-
) as pool:
254-
# Pass the pool to the connection so it can check lock status
255-
pool.connection_kwargs["_pool"] = pool
256-
244+
async with self.get_pool(connection_class=SlowConnectConnection) as pool:
257245
# Get a connection - this should call connect() outside the lock
258246
connection = await pool.get_connection()
259247

260-
# Verify connect was called
261-
assert connection.connect_called
262-
263248
# Verify the lock was NOT held during connect
249+
assert len(lock_states) > 0, "connect() should have been called"
264250
assert (
265-
connection.lock_held_during_connect is False
251+
lock_states[0] is False
266252
), "Lock should not be held during connection establishment"
267253

268254
await pool.release(connection)
@@ -272,13 +258,15 @@ async def test_concurrent_connection_acquisition_performance(self):
272258
Test that multiple concurrent connection acquisitions don't block
273259
each other during connection establishment.
274260
"""
261+
connection_delay = 0.05
262+
num_connections = 3
275263

276264
class SlowConnectConnection(DummyConnection):
277265
"""Connection that simulates slow connection establishment"""
278266

279267
async def connect(self):
280268
# Simulate slow connection (e.g., network latency, TLS handshake)
281-
await asyncio.sleep(0.05)
269+
await asyncio.sleep(connection_delay)
282270
self._connected = True
283271

284272
async with self.get_pool(
@@ -287,22 +275,22 @@ async def connect(self):
287275
# Start acquiring multiple connections concurrently
288276
start_time = asyncio.get_running_loop().time()
289277

290-
# Try to get 3 connections concurrently
278+
# Try to get connections concurrently
291279
connections = await asyncio.gather(
292-
pool.get_connection(),
293-
pool.get_connection(),
294-
pool.get_connection(),
280+
*[pool.get_connection() for _ in range(num_connections)]
295281
)
296282

297283
elapsed_time = asyncio.get_running_loop().time() - start_time
298284

299285
# With proper lock handling, these should complete mostly in parallel
300-
# If the lock was held during connect(), it would take 3 * 0.05 = 0.15s
301-
# With lock only during pop, it should take ~0.05s (connections in parallel)
302-
# We allow some overhead, so check it's less than 0.12s
303-
assert elapsed_time < 0.12, (
304-
f"Concurrent connections took {elapsed_time}s, "
305-
f"suggesting lock was held during connection establishment"
286+
# If the lock was held during connect(), it would take num_connections * connection_delay
287+
# With lock only during pop, it should take ~connection_delay (connections in parallel)
288+
# We allow 2.5x overhead for system variance
289+
max_allowed_time = connection_delay * 2.5
290+
assert elapsed_time < max_allowed_time, (
291+
f"Concurrent connections took {elapsed_time:.3f}s, "
292+
f"expected < {max_allowed_time:.3f}s. "
293+
f"This suggests lock was held during connection establishment."
306294
)
307295

308296
# Clean up

0 commit comments

Comments
 (0)