Skip to content

Commit 41b4234

Browse files
committed
PYTHON-1701 Avoid false positives in unpinning prose test
Wait until both mongoses are discovered, increase localThresholdMS, and increase iteration count.
1 parent ecc852c commit 41b4234

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

test/test_transactions.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@
3838
from pymongo.results import _WriteResult, BulkWriteResult
3939

4040
from test import unittest, client_context, IntegrationTest
41-
from test.utils import OvertCommandListener, rs_client, single_client
41+
from test.utils import (OvertCommandListener,
42+
rs_client,
43+
single_client,
44+
wait_until)
4245
from test.utils_selection_tests import parse_read_preference
4346

4447
# Location of JSON test specifications.
@@ -49,8 +52,9 @@
4952

5053
# Max number of operations to perform after a transaction to prove unpinning
5154
# occurs. Chosen so that there's a low false positive rate. With 2 mongoses,
52-
# 20 attempts yields a 1 in 1048576 chance of a false positive (1/(0.5^20)).
53-
UNPIN_TEST_MAX_ATTEMPTS = 20
55+
# 50 attempts yields a one in a quadrillion chance of a false positive
56+
# (1/(0.5^50)).
57+
UNPIN_TEST_MAX_ATTEMPTS = 50
5458

5559

5660
# TODO: factor the following functions with test_crud.py.
@@ -187,17 +191,24 @@ def test_transaction_write_concern_override(self):
187191
@client_context.require_transactions
188192
@client_context.require_multiple_mongoses
189193
def test_unpin_for_next_transaction(self):
190-
client = rs_client(client_context.mongos_seeds())
194+
# Increase localThresholdMS and wait until both nodes are discovered
195+
# to avoid false positives.
196+
client = rs_client(client_context.mongos_seeds(),
197+
localThresholdMS=1000)
198+
wait_until(lambda: len(client.nodes) > 1, "discover both mongoses")
199+
coll = client.test.test
200+
# Create the collection.
201+
coll.insert_one({})
191202
self.addCleanup(client.close)
192203
with client.start_session() as s:
193204
# Session is pinned to Mongos.
194205
with s.start_transaction():
195-
client.test.test.insert_one({}, session=s)
206+
coll.insert_one({}, session=s)
196207

197208
addresses = set()
198209
for _ in range(UNPIN_TEST_MAX_ATTEMPTS):
199210
with s.start_transaction():
200-
cursor = client.test.test.find({}, session=s)
211+
cursor = coll.find({}, session=s)
201212
self.assertTrue(next(cursor))
202213
addresses.add(cursor.address)
203214
# Break early if we can.
@@ -209,16 +220,23 @@ def test_unpin_for_next_transaction(self):
209220
@client_context.require_transactions
210221
@client_context.require_multiple_mongoses
211222
def test_unpin_for_non_transaction_operation(self):
212-
client = rs_client(client_context.mongos_seeds())
223+
# Increase localThresholdMS and wait until both nodes are discovered
224+
# to avoid false positives.
225+
client = rs_client(client_context.mongos_seeds(),
226+
localThresholdMS=1000)
227+
wait_until(lambda: len(client.nodes) > 1, "discover both mongoses")
228+
coll = client.test.test
229+
# Create the collection.
230+
coll.insert_one({})
213231
self.addCleanup(client.close)
214232
with client.start_session() as s:
215233
# Session is pinned to Mongos.
216234
with s.start_transaction():
217-
client.test.test.insert_one({}, session=s)
235+
coll.insert_one({}, session=s)
218236

219237
addresses = set()
220238
for _ in range(UNPIN_TEST_MAX_ATTEMPTS):
221-
cursor = client.test.test.find({}, session=s)
239+
cursor = coll.find({}, session=s)
222240
self.assertTrue(next(cursor))
223241
addresses.add(cursor.address)
224242
# Break early if we can.

0 commit comments

Comments
 (0)