Skip to content

Commit 1dd4217

Browse files
authored
PYTHON-4919 Resync tests for retryable writes (mongodb#2006)
1 parent c9d9d7c commit 1dd4217

16 files changed

+705
-100
lines changed

test/asynchronous/test_retryable_writes.py

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2017 MongoDB, Inc.
1+
# Copyright 2017-present MongoDB, Inc.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -43,7 +43,6 @@
4343
from bson.int64 import Int64
4444
from bson.raw_bson import RawBSONDocument
4545
from bson.son import SON
46-
from pymongo.asynchronous.mongo_client import AsyncMongoClient
4746
from pymongo.errors import (
4847
AutoReconnect,
4948
ConnectionFailure,
@@ -226,47 +225,6 @@ async def test_supported_single_statement_no_retry(self):
226225
f"{msg} sent txnNumber with {event.command_name}",
227226
)
228227

229-
@async_client_context.require_no_standalone
230-
async def test_supported_single_statement_supported_cluster(self):
231-
for method, args, kwargs in retryable_single_statement_ops(self.db.retryable_write_test):
232-
msg = f"{method.__name__}(*{args!r}, **{kwargs!r})"
233-
self.listener.reset()
234-
await method(*args, **kwargs)
235-
commands_started = self.listener.started_events
236-
self.assertEqual(len(self.listener.succeeded_events), 1, msg)
237-
first_attempt = commands_started[0]
238-
self.assertIn(
239-
"lsid",
240-
first_attempt.command,
241-
f"{msg} sent no lsid with {first_attempt.command_name}",
242-
)
243-
initial_session_id = first_attempt.command["lsid"]
244-
self.assertIn(
245-
"txnNumber",
246-
first_attempt.command,
247-
f"{msg} sent no txnNumber with {first_attempt.command_name}",
248-
)
249-
250-
# There should be no retry when the failpoint is not active.
251-
if async_client_context.is_mongos or not async_client_context.test_commands_enabled:
252-
self.assertEqual(len(commands_started), 1)
253-
continue
254-
255-
initial_transaction_id = first_attempt.command["txnNumber"]
256-
retry_attempt = commands_started[1]
257-
self.assertIn(
258-
"lsid",
259-
retry_attempt.command,
260-
f"{msg} sent no lsid with {first_attempt.command_name}",
261-
)
262-
self.assertEqual(retry_attempt.command["lsid"], initial_session_id, msg)
263-
self.assertIn(
264-
"txnNumber",
265-
retry_attempt.command,
266-
f"{msg} sent no txnNumber with {first_attempt.command_name}",
267-
)
268-
self.assertEqual(retry_attempt.command["txnNumber"], initial_transaction_id, msg)
269-
270228
async def test_supported_single_statement_unsupported_cluster(self):
271229
if async_client_context.is_rs or async_client_context.is_mongos:
272230
raise SkipTest("This cluster supports retryable writes")
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
{
2+
"description": "aggregate with $out/$merge does not set txnNumber",
3+
"schemaVersion": "1.3",
4+
"runOnRequirements": [
5+
{
6+
"minServerVersion": "3.6",
7+
"topologies": [
8+
"replicaset",
9+
"sharded",
10+
"load-balanced"
11+
]
12+
}
13+
],
14+
"createEntities": [
15+
{
16+
"client": {
17+
"id": "client0",
18+
"observeEvents": [
19+
"commandStartedEvent"
20+
]
21+
}
22+
},
23+
{
24+
"database": {
25+
"id": "database0",
26+
"client": "client0",
27+
"databaseName": "retryable-writes-tests"
28+
}
29+
},
30+
{
31+
"collection": {
32+
"id": "collection0",
33+
"database": "database0",
34+
"collectionName": "coll0"
35+
}
36+
}
37+
],
38+
"initialData": [
39+
{
40+
"collectionName": "mergeCollection",
41+
"databaseName": "retryable-writes-tests",
42+
"documents": []
43+
}
44+
],
45+
"tests": [
46+
{
47+
"description": "aggregate with $out does not set txnNumber",
48+
"operations": [
49+
{
50+
"object": "collection0",
51+
"name": "aggregate",
52+
"arguments": {
53+
"pipeline": [
54+
{
55+
"$sort": {
56+
"x": 1
57+
}
58+
},
59+
{
60+
"$match": {
61+
"_id": {
62+
"$gt": 1
63+
}
64+
}
65+
},
66+
{
67+
"$out": "outCollection"
68+
}
69+
]
70+
}
71+
}
72+
],
73+
"expectEvents": [
74+
{
75+
"client": "client0",
76+
"events": [
77+
{
78+
"commandStartedEvent": {
79+
"commandName": "aggregate",
80+
"command": {
81+
"txnNumber": {
82+
"$$exists": false
83+
}
84+
}
85+
}
86+
}
87+
]
88+
}
89+
]
90+
},
91+
{
92+
"description": "aggregate with $merge does not set txnNumber",
93+
"runOnRequirements": [
94+
{
95+
"minServerVersion": "4.1.11"
96+
}
97+
],
98+
"operations": [
99+
{
100+
"object": "collection0",
101+
"name": "aggregate",
102+
"arguments": {
103+
"pipeline": [
104+
{
105+
"$sort": {
106+
"x": 1
107+
}
108+
},
109+
{
110+
"$match": {
111+
"_id": {
112+
"$gt": 1
113+
}
114+
}
115+
},
116+
{
117+
"$merge": {
118+
"into": "mergeCollection"
119+
}
120+
}
121+
]
122+
}
123+
}
124+
],
125+
"expectEvents": [
126+
{
127+
"client": "client0",
128+
"events": [
129+
{
130+
"commandStartedEvent": {
131+
"commandName": "aggregate",
132+
"command": {
133+
"txnNumber": {
134+
"$$exists": false
135+
}
136+
}
137+
}
138+
}
139+
]
140+
}
141+
]
142+
}
143+
]
144+
}

test/retryable_writes/unified/bulkWrite.json

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
{
1414
"client": {
1515
"id": "client0",
16-
"useMultipleMongoses": false
16+
"useMultipleMongoses": false,
17+
"observeEvents": [
18+
"commandStartedEvent"
19+
]
1720
}
1821
},
1922
{
@@ -121,6 +124,53 @@
121124
}
122125
]
123126
}
127+
],
128+
"expectEvents": [
129+
{
130+
"client": "client0",
131+
"events": [
132+
{
133+
"commandStartedEvent": {
134+
"commandName": "insert",
135+
"command": {
136+
"txnNumber": {
137+
"$$exists": true
138+
}
139+
}
140+
}
141+
},
142+
{
143+
"commandStartedEvent": {
144+
"commandName": "insert",
145+
"command": {
146+
"txnNumber": {
147+
"$$exists": true
148+
}
149+
}
150+
}
151+
},
152+
{
153+
"commandStartedEvent": {
154+
"commandName": "update",
155+
"command": {
156+
"txnNumber": {
157+
"$$exists": true
158+
}
159+
}
160+
}
161+
},
162+
{
163+
"commandStartedEvent": {
164+
"commandName": "delete",
165+
"command": {
166+
"txnNumber": {
167+
"$$exists": true
168+
}
169+
}
170+
}
171+
}
172+
]
173+
}
124174
]
125175
},
126176
{
@@ -510,6 +560,33 @@
510560
}
511561
]
512562
}
563+
],
564+
"expectEvents": [
565+
{
566+
"client": "client0",
567+
"events": [
568+
{
569+
"commandStartedEvent": {
570+
"commandName": "insert",
571+
"command": {
572+
"txnNumber": {
573+
"$$exists": true
574+
}
575+
}
576+
}
577+
},
578+
{
579+
"commandStartedEvent": {
580+
"commandName": "insert",
581+
"command": {
582+
"txnNumber": {
583+
"$$exists": true
584+
}
585+
}
586+
}
587+
}
588+
]
589+
}
513590
]
514591
},
515592
{
@@ -926,6 +1003,81 @@
9261003
]
9271004
}
9281005
]
1006+
},
1007+
{
1008+
"description": "collection bulkWrite with updateMany does not set txnNumber",
1009+
"operations": [
1010+
{
1011+
"object": "collection0",
1012+
"name": "bulkWrite",
1013+
"arguments": {
1014+
"requests": [
1015+
{
1016+
"updateMany": {
1017+
"filter": {},
1018+
"update": {
1019+
"$set": {
1020+
"x": 1
1021+
}
1022+
}
1023+
}
1024+
}
1025+
]
1026+
}
1027+
}
1028+
],
1029+
"expectEvents": [
1030+
{
1031+
"client": "client0",
1032+
"events": [
1033+
{
1034+
"commandStartedEvent": {
1035+
"commandName": "update",
1036+
"command": {
1037+
"txnNumber": {
1038+
"$$exists": false
1039+
}
1040+
}
1041+
}
1042+
}
1043+
]
1044+
}
1045+
]
1046+
},
1047+
{
1048+
"description": "collection bulkWrite with deleteMany does not set txnNumber",
1049+
"operations": [
1050+
{
1051+
"object": "collection0",
1052+
"name": "bulkWrite",
1053+
"arguments": {
1054+
"requests": [
1055+
{
1056+
"deleteMany": {
1057+
"filter": {}
1058+
}
1059+
}
1060+
]
1061+
}
1062+
}
1063+
],
1064+
"expectEvents": [
1065+
{
1066+
"client": "client0",
1067+
"events": [
1068+
{
1069+
"commandStartedEvent": {
1070+
"commandName": "delete",
1071+
"command": {
1072+
"txnNumber": {
1073+
"$$exists": false
1074+
}
1075+
}
1076+
}
1077+
}
1078+
]
1079+
}
1080+
]
9291081
}
9301082
]
9311083
}

0 commit comments

Comments
 (0)