Skip to content

Commit fc589b2

Browse files
PCSM-221. Add test to reproduce pcsm duplicate key error when handling shard key updates
1 parent 6fec171 commit fc589b2

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

tests/test_collections_sharded.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# pylint: disable=missing-docstring,redefined-outer-name
2+
import threading
3+
import time
24
from datetime import datetime
35

46
import pytest
@@ -85,3 +87,28 @@ def test_create_collection_with_collation_with_shard_key_index_prefix(
8587
)
8688

8789
t.compare_all_sharded()
90+
91+
@pytest.mark.parametrize("phase", [Runner.Phase.APPLY, Runner.Phase.CLONE])
92+
def test_shard_key_update_duplicate_key_error(t: Testing, phase: Runner.Phase):
93+
"""
94+
Test to reproduce pcsm duplicate key error when handling shard key updates
95+
"""
96+
db_name = "test_db"
97+
collection_name = "test_collection"
98+
coll = t.source[db_name][collection_name]
99+
t.source.admin.command("shardCollection", f"{db_name}.{collection_name}", key={"key_id": 1})
100+
coll.insert_one({"key_id": 0, "name": "item_0", "value": "value_0"})
101+
def perform_shard_key_updates():
102+
num_updates = 20
103+
for i in range(1, num_updates + 1):
104+
key_id = 100 + i
105+
new_key_id = 5000 + i
106+
coll.insert_one({"key_id": key_id, "name": f"test_doc_{i}", "value": f"value_{key_id}"})
107+
coll.update_one({"key_id": key_id}, {"$set": {"key_id": new_key_id, "shard_key_updated": True}})
108+
time.sleep(0.05)
109+
update_thread = threading.Thread(target=perform_shard_key_updates)
110+
update_thread.start()
111+
with t.run(phase):
112+
update_thread.join()
113+
time.sleep(5)
114+
t.compare_all_sharded()

0 commit comments

Comments
 (0)