Skip to content

Commit 6a18027

Browse files
authored
PYTHON-2534 Avoid race in test_pool_paused_error_is_retryable (#704)
1 parent 65aa7c8 commit 6a18027

File tree

3 files changed

+57
-38
lines changed

3 files changed

+57
-38
lines changed

test/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def __init__(
154154
self.old_min_heartbeat_interval = None
155155
self.old_kill_cursor_frequency = None
156156
self.old_events_queue_frequency = None
157-
self._enabled = True
157+
self._enabled = False
158158
self._stack = None
159159

160160
def enable(self):
@@ -776,8 +776,8 @@ def require_failCommand_blockConnection(self, func):
776776
"""
777777
return self._require(
778778
lambda: (self.test_commands_enabled and (
779-
(not self.is_mongos and self.version >= (4, 2, 9))) or
780-
(self.is_mongos and self.version >= (4, 4))),
779+
(not self.is_mongos and self.version >= (4, 2, 9)) or
780+
(self.is_mongos and self.version >= (4, 4)))),
781781
"failCommand blockConnection is not supported",
782782
func=func)
783783

test/test_retryable_reads.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -163,23 +163,33 @@ def test_pool_paused_error_is_retryable(self):
163163
maxPoolSize=1,
164164
event_listeners=[cmap_listener, cmd_listener])
165165
self.addCleanup(client.close)
166-
threads = [FindThread(client.pymongo_test.test) for _ in range(2)]
167-
fail_command = {
168-
'mode': {'times': 1},
169-
'data': {
170-
'failCommands': ['find'],
171-
'blockConnection': True,
172-
'blockTimeMS': 1000,
173-
'errorCode': 91,
174-
},
175-
}
176-
with self.fail_point(fail_command):
177-
for thread in threads:
178-
thread.start()
179-
for thread in threads:
180-
thread.join()
181-
for thread in threads:
182-
self.assertTrue(thread.passed)
166+
for _ in range(10):
167+
cmap_listener.reset()
168+
cmd_listener.reset()
169+
threads = [FindThread(client.pymongo_test.test) for _ in range(2)]
170+
fail_command = {
171+
'mode': {'times': 1},
172+
'data': {
173+
'failCommands': ['find'],
174+
'blockConnection': True,
175+
'blockTimeMS': 1000,
176+
'errorCode': 91,
177+
},
178+
}
179+
with self.fail_point(fail_command):
180+
for thread in threads:
181+
thread.start()
182+
for thread in threads:
183+
thread.join()
184+
for thread in threads:
185+
self.assertTrue(thread.passed)
186+
187+
# It's possible that SDAM can rediscover the server and mark the
188+
# pool ready before the thread in the wait queue has a chance
189+
# to run. Repeat the test until the thread actually encounters
190+
# a PoolClearedError.
191+
if cmap_listener.event_count(ConnectionCheckOutFailedEvent):
192+
break
183193

184194
# Via CMAP monitoring, assert that the first check out succeeds.
185195
cmap_events = cmap_listener.events_by_type((

test/test_retryable_writes.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -515,24 +515,33 @@ def test_pool_paused_error_is_retryable(self):
515515
maxPoolSize=1,
516516
event_listeners=[cmap_listener, cmd_listener])
517517
self.addCleanup(client.close)
518-
threads = [InsertThread(client.pymongo_test.test) for _ in range(2)]
519-
fail_command = {
520-
'mode': {'times': 1},
521-
'data': {
522-
'failCommands': ['insert'],
523-
'blockConnection': True,
524-
'blockTimeMS': 1000,
525-
'errorCode': 91,
526-
'errorLabels': ['RetryableWriteError'],
527-
},
528-
}
529-
with self.fail_point(fail_command):
530-
for thread in threads:
531-
thread.start()
532-
for thread in threads:
533-
thread.join()
534-
for thread in threads:
535-
self.assertTrue(thread.passed)
518+
for _ in range(10):
519+
cmap_listener.reset()
520+
cmd_listener.reset()
521+
threads = [InsertThread(client.pymongo_test.test) for _ in range(2)]
522+
fail_command = {
523+
'mode': {'times': 1},
524+
'data': {
525+
'failCommands': ['insert'],
526+
'blockConnection': True,
527+
'blockTimeMS': 1000,
528+
'errorCode': 91,
529+
'errorLabels': ['RetryableWriteError'],
530+
},
531+
}
532+
with self.fail_point(fail_command):
533+
for thread in threads:
534+
thread.start()
535+
for thread in threads:
536+
thread.join()
537+
for thread in threads:
538+
self.assertTrue(thread.passed)
539+
# It's possible that SDAM can rediscover the server and mark the
540+
# pool ready before the thread in the wait queue has a chance
541+
# to run. Repeat the test until the thread actually encounters
542+
# a PoolClearedError.
543+
if cmap_listener.event_count(ConnectionCheckOutFailedEvent):
544+
break
536545

537546
# Via CMAP monitoring, assert that the first check out succeeds.
538547
cmap_events = cmap_listener.events_by_type((

0 commit comments

Comments
 (0)