Skip to content

Commit d834236

Browse files
committed
PYTHON-2214 Tolerate StaleConfig errors in test_create_collection
1 parent 07c834e commit d834236

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

test/test_transactions.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,15 @@ def test_create_collection(self):
214214
db = client.pymongo_test
215215
coll = db.test_create_collection
216216
self.addCleanup(coll.drop)
217-
with client.start_session() as s, s.start_transaction():
218-
coll2 = db.create_collection(coll.name, session=s)
217+
218+
# Use with_transaction to avoid StaleConfig errors on sharded clusters.
219+
def create_and_insert(session):
220+
coll2 = db.create_collection(coll.name, session=session)
219221
self.assertEqual(coll, coll2)
220-
coll.insert_one({}, session=s)
222+
coll.insert_one({}, session=session)
223+
224+
with client.start_session() as s:
225+
s.with_transaction(create_and_insert)
221226

222227
# Outside a transaction we raise CollectionInvalid on existing colls.
223228
with self.assertRaises(CollectionInvalid):

0 commit comments

Comments
 (0)