Skip to content

Commit 7aafd82

Browse files
committed
tests: fix flaky TestTwistedConnection.test_connection_initialization
Patch reactor.running to False in setUp() so that maybe_start() always enters the branch that spawns the reactor thread. Without this, leaked global reactor state from prior tests can leave reactor.running as True, causing maybe_start() to skip thread creation and the reactor.run mock to never be called — making the assertion in test_connection_initialization fail intermittently. Observed in CI on PyPy 3.11 + macOS x86 (Rosetta 2), where timing differences make the reactor state leak more likely.
1 parent 153c913 commit 7aafd82

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

tests/unit/io/test_twistedreactor.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,23 @@ def setUp(self):
9999
self.reactor_cft_patcher = patch(
100100
'twisted.internet.reactor.callFromThread')
101101
self.reactor_run_patcher = patch('twisted.internet.reactor.run')
102+
# Patch reactor.running to False so maybe_start() always enters
103+
# the branch that spawns the reactor thread. Without this, leaked
104+
# reactor state from prior tests can cause reactor.running to be
105+
# True, making maybe_start() a no-op and the reactor.run mock
106+
# never called — leading to a flaky test_connection_initialization.
107+
self.reactor_running_patcher = patch(
108+
'twisted.internet.reactor.running', new=False)
102109
self.mock_reactor_cft = self.reactor_cft_patcher.start()
103110
self.mock_reactor_run = self.reactor_run_patcher.start()
111+
self.reactor_running_patcher.start()
104112
self.obj_ut = twistedreactor.TwistedConnection(DefaultEndPoint('1.2.3.4'),
105113
cql_version='3.0.1')
106114

107115
def tearDown(self):
108116
self.reactor_cft_patcher.stop()
109117
self.reactor_run_patcher.stop()
118+
self.reactor_running_patcher.stop()
110119

111120
def test_connection_initialization(self):
112121
"""

0 commit comments

Comments
 (0)