38
38
from pymongo .results import _WriteResult , BulkWriteResult
39
39
40
40
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 )
42
45
from test .utils_selection_tests import parse_read_preference
43
46
44
47
# Location of JSON test specifications.
49
52
50
53
# Max number of operations to perform after a transaction to prove unpinning
51
54
# 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
54
58
55
59
56
60
# TODO: factor the following functions with test_crud.py.
@@ -187,17 +191,24 @@ def test_transaction_write_concern_override(self):
187
191
@client_context .require_transactions
188
192
@client_context .require_multiple_mongoses
189
193
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 ({})
191
202
self .addCleanup (client .close )
192
203
with client .start_session () as s :
193
204
# Session is pinned to Mongos.
194
205
with s .start_transaction ():
195
- client . test . test .insert_one ({}, session = s )
206
+ coll .insert_one ({}, session = s )
196
207
197
208
addresses = set ()
198
209
for _ in range (UNPIN_TEST_MAX_ATTEMPTS ):
199
210
with s .start_transaction ():
200
- cursor = client . test . test .find ({}, session = s )
211
+ cursor = coll .find ({}, session = s )
201
212
self .assertTrue (next (cursor ))
202
213
addresses .add (cursor .address )
203
214
# Break early if we can.
@@ -209,16 +220,23 @@ def test_unpin_for_next_transaction(self):
209
220
@client_context .require_transactions
210
221
@client_context .require_multiple_mongoses
211
222
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 ({})
213
231
self .addCleanup (client .close )
214
232
with client .start_session () as s :
215
233
# Session is pinned to Mongos.
216
234
with s .start_transaction ():
217
- client . test . test .insert_one ({}, session = s )
235
+ coll .insert_one ({}, session = s )
218
236
219
237
addresses = set ()
220
238
for _ in range (UNPIN_TEST_MAX_ATTEMPTS ):
221
- cursor = client . test . test .find ({}, session = s )
239
+ cursor = coll .find ({}, session = s )
222
240
self .assertTrue (next (cursor ))
223
241
addresses .add (cursor .address )
224
242
# Break early if we can.
0 commit comments