Skip to content

Commit 711a7eb

Browse files
mykauldkropachev
authored andcommitted
test: optimize test_fast_shutdown with event-based synchronization
Replace sleep-based timing with proper Event synchronization: - Remove executor_init with 0.5s sleep - Replace time.sleep(0.5) with connection_created.wait() - Replace time.sleep(3) with executor.shutdown(wait=True) - Reduce iterations from 20 to 3 (deterministic with proper sync) - Add explicit assertions for shutdown state Performance improvement: - Before: 70.13s call tests/unit/test_host_connection_pool.py::HostConnectionTests::test_fast_shutdown - After: 0.01s call tests/unit/test_host_connection_pool.py::HostConnectionTests::test_fast_shutdown Signed-off-by: Yaniv Kaul <yaniv.kaul@scylladb.com>
1 parent 472461c commit 711a7eb

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

tests/unit/test_host_connection_pool.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ class MockSession(MagicMock):
239239
def __init__(self, *args, **kwargs):
240240
super(MockSession, self).__init__(*args, **kwargs)
241241
self.cluster = MagicMock()
242-
self.cluster.executor = ThreadPoolExecutor(max_workers=2, initializer=self.executor_init)
242+
self.connection_created = Event()
243+
self.cluster.executor = ThreadPoolExecutor(max_workers=2)
243244
self.cluster.signal_connection_failure = lambda *args, **kwargs: False
244245
self.cluster.connection_factory = self.mock_connection_factory
245246
self.connection_counter = 0
@@ -259,23 +260,30 @@ def mock_connection_factory(self, *args, **kwargs):
259260
partitioner="", sharding_algorithm="", sharding_ignore_msb=0,
260261
shard_aware_port="", shard_aware_port_ssl=""))
261262
self.connection_counter += 1
263+
self.connection_created.set()
262264

263265
return connection
264266

265-
def executor_init(self, *args):
266-
time.sleep(0.5)
267-
LOGGER.info("Future start: %s", args)
268-
269-
for attempt_num in range(20):
270-
LOGGER.info("Testing fast shutdown %d / 20 times", attempt_num + 1)
267+
for attempt_num in range(3):
268+
LOGGER.info("Testing fast shutdown %d / 3 times", attempt_num + 1)
271269
host = MagicMock()
272270
host.endpoint = "1.2.3.4"
273-
session = self.make_session()
271+
session = MockSession()
274272

275273
pool = HostConnection(host=host, host_distance=HostDistance.REMOTE, session=session)
276274
LOGGER.info("Initialized pool %s", pool)
275+
276+
# Wait for initial connection to be created (with timeout)
277+
if not session.connection_created.wait(timeout=2.0):
278+
pytest.fail("Initial connection failed to be created within 2 seconds")
279+
277280
LOGGER.info("Connections: %s", pool._connections)
278-
time.sleep(0.5)
281+
282+
# Shutdown the pool
279283
pool.shutdown()
280-
time.sleep(3)
281-
session.cluster.executor.shutdown()
284+
285+
# Verify pool is shut down
286+
assert pool.is_shutdown, "Pool should be marked as shutdown"
287+
288+
# Cleanup executor with proper wait
289+
session.cluster.executor.shutdown(wait=True)

0 commit comments

Comments
 (0)