Skip to content

Commit ca5a84a

Browse files
PCSM-221. Add test to reproduce pcsm duplicate key error when handling shard key updates
1 parent 45f6fa8 commit ca5a84a

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
@@ -68,3 +70,28 @@ def test_create_collection_with_collation_with_shard_key_index_prefix(
6870
)
6971

7072
t.compare_all_sharded()
73+
74+
@pytest.mark.parametrize("phase", [Runner.Phase.APPLY, Runner.Phase.CLONE])
75+
def test_shard_key_update_duplicate_key_error(t: Testing, phase: Runner.Phase):
76+
"""
77+
Test to reproduce pcsm duplicate key error when handling shard key updates
78+
"""
79+
db_name = "test_db"
80+
collection_name = "test_collection"
81+
coll = t.source[db_name][collection_name]
82+
t.source.admin.command("shardCollection", f"{db_name}.{collection_name}", key={"key_id": 1})
83+
coll.insert_one({"key_id": 0, "name": "item_0", "value": "value_0"})
84+
def perform_shard_key_updates():
85+
num_updates = 20
86+
for i in range(1, num_updates + 1):
87+
key_id = 100 + i
88+
new_key_id = 5000 + i
89+
coll.insert_one({"key_id": key_id, "name": f"test_doc_{i}", "value": f"value_{key_id}"})
90+
coll.update_one({"key_id": key_id}, {"$set": {"key_id": new_key_id, "shard_key_updated": True}})
91+
time.sleep(0.05)
92+
update_thread = threading.Thread(target=perform_shard_key_updates)
93+
update_thread.start()
94+
with t.run(phase):
95+
update_thread.join()
96+
time.sleep(5)
97+
t.compare_all_sharded()

0 commit comments

Comments
 (0)