Skip to content

Commit 1f1e65b

Browse files
CSHARP-3384: Define error handling behavior of writeErrors and writeConcernError on Mongos (mongodb#484)
1 parent 434090e commit 1f1e65b

File tree

3 files changed

+159
-14
lines changed

3 files changed

+159
-14
lines changed

tests/MongoDB.Driver.Core.Tests/Specifications/server-discovery-and-monitoring/ServerDiscoveryAndMonitoringTestRunner.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ private void ApplyApplicationError(BsonDocument applicationError)
8484
case "command":
8585
var response = applicationError["response"].AsBsonDocument;
8686
var command = new BsonDocument("Link", "start!");
87-
simulatedException = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, command, response, "errmsg");
88-
Ensure.IsNotNull(simulatedException, nameof(simulatedException));
87+
simulatedException = ExceptionMapper.MapNotPrimaryOrNodeIsRecovering(connectionId, command, response, "errmsg"); // can return null
8988
break;
9089
case "network":
9190
{
@@ -104,24 +103,33 @@ private void ApplyApplicationError(BsonDocument applicationError)
104103
}
105104

106105
var mockConnection = new Mock<IConnectionHandle>();
106+
107107
var isMasterResult = new IsMasterResult(new BsonDocument { { "compressors", new BsonArray() } });
108108
var serverVersion = WireVersionHelper.MapWireVersionToServerVersion(maxWireVersion);
109109
var buildInfoResult = new BuildInfoResult(new BsonDocument { { "version", serverVersion } });
110-
mockConnection.SetupGet(c => c.Description)
110+
mockConnection
111+
.SetupGet(c => c.Description)
111112
.Returns(new ConnectionDescription(connectionId, isMasterResult, buildInfoResult));
113+
112114
var generation = applicationError.Contains("generation") ? applicationError["generation"].AsInt32 : 0;
113-
mockConnection.SetupGet(c => c.Generation).Returns(generation);
114-
var when = applicationError["when"].AsString;
115-
switch (when)
115+
mockConnection
116+
.SetupGet(c => c.Generation)
117+
.Returns(generation);
118+
119+
if (simulatedException != null)
116120
{
117-
case "beforeHandshakeCompletes":
118-
server.HandleBeforeHandshakeCompletesException(mockConnection.Object, simulatedException);
119-
break;
120-
case "afterHandshakeCompletes":
121-
server.HandleChannelException(mockConnection.Object, simulatedException);
122-
break;
123-
default:
124-
throw new ArgumentException($"Unsupported value of {when} for when.");
121+
var when = applicationError["when"].AsString;
122+
switch (when)
123+
{
124+
case "beforeHandshakeCompletes":
125+
server.HandleBeforeHandshakeCompletesException(mockConnection.Object, simulatedException);
126+
break;
127+
case "afterHandshakeCompletes":
128+
server.HandleChannelException(mockConnection.Object, simulatedException);
129+
break;
130+
default:
131+
throw new ArgumentException($"Unsupported value of {when} for when.");
132+
}
125133
}
126134
}
127135

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"description": "writeErrors field is ignored",
3+
"uri": "mongodb://a/?replicaSet=rs",
4+
"phases": [
5+
{
6+
"description": "Primary A is discovered",
7+
"responses": [
8+
[
9+
"a:27017",
10+
{
11+
"ok": 1,
12+
"ismaster": true,
13+
"hosts": [
14+
"a:27017"
15+
],
16+
"setName": "rs",
17+
"minWireVersion": 0,
18+
"maxWireVersion": 9,
19+
"topologyVersion": {
20+
"processId": {
21+
"$oid": "000000000000000000000001"
22+
},
23+
"counter": {
24+
"$numberLong": "1"
25+
}
26+
}
27+
}
28+
]
29+
],
30+
"outcome": {
31+
"servers": {
32+
"a:27017": {
33+
"type": "RSPrimary",
34+
"setName": "rs",
35+
"topologyVersion": {
36+
"processId": {
37+
"$oid": "000000000000000000000001"
38+
},
39+
"counter": {
40+
"$numberLong": "1"
41+
}
42+
},
43+
"pool": {
44+
"generation": 0
45+
}
46+
}
47+
},
48+
"topologyType": "ReplicaSetWithPrimary",
49+
"logicalSessionTimeoutMinutes": null,
50+
"setName": "rs"
51+
}
52+
},
53+
{
54+
"description": "Ignore command error with writeErrors field",
55+
"applicationErrors": [
56+
{
57+
"address": "a:27017",
58+
"when": "afterHandshakeCompletes",
59+
"maxWireVersion": 9,
60+
"type": "command",
61+
"response": {
62+
"ok": 1,
63+
"writeErrors": [
64+
{
65+
"errmsg": "NotMasterNoSlaveOk",
66+
"code": 13435
67+
}
68+
]
69+
}
70+
}
71+
],
72+
"outcome": {
73+
"servers": {
74+
"a:27017": {
75+
"type": "RSPrimary",
76+
"setName": "rs",
77+
"topologyVersion": {
78+
"processId": {
79+
"$oid": "000000000000000000000001"
80+
},
81+
"counter": {
82+
"$numberLong": "1"
83+
}
84+
},
85+
"pool": {
86+
"generation": 0
87+
}
88+
}
89+
},
90+
"topologyType": "ReplicaSetWithPrimary",
91+
"logicalSessionTimeoutMinutes": null,
92+
"setName": "rs"
93+
}
94+
}
95+
]
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
description: writeErrors field is ignored
2+
uri: mongodb://a/?replicaSet=rs
3+
phases:
4+
- description: Primary A is discovered
5+
responses:
6+
- - a:27017
7+
- ok: 1
8+
ismaster: true
9+
hosts:
10+
- a:27017
11+
setName: rs
12+
minWireVersion: 0
13+
maxWireVersion: 9
14+
topologyVersion: &topologyVersion_1_1
15+
processId:
16+
"$oid": '000000000000000000000001'
17+
counter:
18+
"$numberLong": '1'
19+
outcome: &outcome
20+
servers:
21+
a:27017:
22+
type: RSPrimary
23+
setName: rs
24+
topologyVersion: *topologyVersion_1_1
25+
pool:
26+
generation: 0
27+
topologyType: ReplicaSetWithPrimary
28+
logicalSessionTimeoutMinutes: null
29+
setName: rs
30+
31+
- description: Ignore command error with writeErrors field
32+
applicationErrors:
33+
- address: a:27017
34+
when: afterHandshakeCompletes
35+
maxWireVersion: 9
36+
type: command
37+
response:
38+
ok: 1
39+
writeErrors:
40+
- { errmsg: NotMasterNoSlaveOk, code: 13435 }
41+
outcome: *outcome

0 commit comments

Comments
 (0)