Skip to content

Commit 3b45322

Browse files
committed
Fix and clarify test_insert_large_batch.
Using separate collections avoids an apparent race in mongos 2.6's dropCollection, as well as clarifying the test. Further clarify it by explicitly naming and specifying batches and the ids of documents within them.
1 parent a1ccdcd commit 3b45322

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

test/test_collection.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,57 +1749,59 @@ def test_insert_large_document(self):
17491749
self.db.test.update({"bar": "x"}, {"bar": "x" * (max_size - 32)})
17501750

17511751
def test_insert_large_batch(self):
1752-
collection = self.db.test_insert_large_batch
1752+
db = self.client.test_insert_large_batch
1753+
self.addCleanup(self.client.drop_database, 'test_insert_large_batch')
17531754
max_bson_size = self.client.max_bson_size
17541755
if client_context.version.at_least(2, 5, 4, -1):
17551756
# Write commands are limited to 16MB + 16k per batch
17561757
big_string = 'x' * int(max_bson_size / 2)
17571758
else:
17581759
big_string = 'x' * (max_bson_size - 100)
1759-
collection.drop()
1760-
self.assertEqual(0, collection.count())
1761-
1762-
# Batch insert that requires 2 batches
1763-
batch = [{'x': big_string}, {'x': big_string},
1764-
{'x': big_string}, {'x': big_string}]
1765-
self.assertTrue(collection.insert(batch, w=1))
1766-
self.assertEqual(4, collection.count())
17671760

1768-
batch[1]['_id'] = batch[0]['_id']
1761+
# Batch insert that requires 2 batches.
1762+
successful_insert = [{'x': big_string}, {'x': big_string},
1763+
{'x': big_string}, {'x': big_string}]
1764+
db.collection_0.insert(successful_insert, w=1)
1765+
self.assertEqual(4, db.collection_0.count())
17691766

17701767
# Test that inserts fail after first error.
1771-
collection.drop()
1772-
self.assertRaises(DuplicateKeyError, collection.insert, batch)
1773-
self.assertEqual(1, collection.count())
1768+
insert_second_fails = [{'_id': 'id0', 'x': big_string},
1769+
{'_id': 'id0', 'x': big_string},
1770+
{'_id': 'id1', 'x': big_string},
1771+
{'_id': 'id2', 'x': big_string}]
1772+
1773+
with self.assertRaises(DuplicateKeyError):
1774+
db.collection_1.insert(insert_second_fails)
1775+
1776+
self.assertEqual(1, db.collection_1.count())
17741777

17751778
# 2 batches, 2nd insert fails, don't continue on error.
1776-
collection.drop()
1777-
self.assertTrue(collection.insert(batch, w=0))
1778-
wait_until(lambda: 1 == collection.count(),
1779+
self.assertTrue(db.collection_2.insert(insert_second_fails, w=0))
1780+
wait_until(lambda: 1 == db.collection_2.count(),
17791781
'insert 1 document')
17801782

17811783
# 2 batches, ids of docs 0 and 1 are dupes, ids of docs 2 and 3 are
17821784
# dupes. Acknowledged, continue on error.
1783-
collection.drop()
1784-
batch[3]['_id'] = batch[2]['_id']
1785-
try:
1786-
collection.insert(batch, continue_on_error=True, w=1)
1787-
except OperationFailure as e:
1788-
# Make sure we report the last error, not the first.
1789-
self.assertTrue(str(batch[2]['_id']) in str(e))
1790-
else:
1791-
self.fail('OperationFailure not raised.')
1792-
# Only the first and third documents should be inserted.
1793-
self.assertEqual(2, collection.count())
1785+
insert_two_failures = [{'_id': 'id0', 'x': big_string},
1786+
{'_id': 'id0', 'x': big_string},
1787+
{'_id': 'id1', 'x': big_string},
1788+
{'_id': 'id1', 'x': big_string}]
1789+
1790+
with self.assertRaises(OperationFailure) as context:
1791+
db.collection_3.insert(insert_two_failures,
1792+
continue_on_error=True, w=1)
1793+
1794+
self.assertIn('id1', str(context.exception))
17941795

1795-
# 2 batches, 2 errors, unacknowledged, continue on error
1796-
collection.drop()
1797-
self.assertTrue(
1798-
collection.insert(batch, continue_on_error=True, w=0))
17991796
# Only the first and third documents should be inserted.
1800-
wait_until(lambda: 2 == collection.count(),
1797+
self.assertEqual(2, db.collection_3.count())
1798+
1799+
# 2 batches, 2 errors, unacknowledged, continue on error.
1800+
db.collection_4.insert(insert_two_failures, continue_on_error=True, w=0)
1801+
1802+
# Only the first and third documents are inserted.
1803+
wait_until(lambda: 2 == db.collection_4.count(),
18011804
'insert 2 documents')
1802-
collection.drop()
18031805

18041806
def test_numerous_inserts(self):
18051807
# Ensure we don't exceed server's 1000-document batch size limit.

0 commit comments

Comments
 (0)