Skip to content

Commit a226e8f

Browse files
CSHARP-3017: Ensure that the WriteConcernError "errInfo" object is propagated.
1 parent ea4d8d3 commit a226e8f

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

src/MongoDB.Driver.Core/Core/Misc/Feature.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class Feature
5454
private static readonly Feature __explainCommand = new Feature("ExplainCommand", new SemanticVersion(3, 0, 0));
5555
private static readonly Feature __failPoints = new Feature("FailPoints", new SemanticVersion(2, 4, 0));
5656
private static readonly Feature __failPointsFailCommand = new Feature("FailPointsFailCommand", new SemanticVersion(4, 0, 0));
57+
private static readonly Feature __failPointsFailCommandForSharded = new Feature("FailPointsFailCommandForSharded", new SemanticVersion(4, 1, 5));
5758
private static readonly Feature __findAndModifyWriteConcern = new Feature("FindAndModifyWriteConcern", new SemanticVersion(3, 2, 0));
5859
private static readonly Feature __findCommand = new Feature("FindCommand", new SemanticVersion(3, 2, 0));
5960
private static readonly Feature __geoNearCommand = new Feature("GeoNearCommand", new SemanticVersion(1, 0, 0), new SemanticVersion(4, 1, 0, ""));
@@ -243,6 +244,11 @@ public class Feature
243244
/// </summary>
244245
public static Feature FailPointsFailCommand => __failPointsFailCommand;
245246

247+
/// <summary>
248+
/// Gets the fail points fail command for sharded feature.
249+
/// </summary>
250+
public static Feature FailPointsFailCommandForSharded => __failPointsFailCommandForSharded;
251+
246252
/// <summary>
247253
/// Gets the find and modify write concern feature.
248254
/// </summary>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* Copyright 2020-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using FluentAssertions;
17+
using MongoDB.Bson;
18+
using MongoDB.Driver.Core.Bindings;
19+
using MongoDB.Driver.Core.Clusters;
20+
using MongoDB.Driver.Core.Misc;
21+
using MongoDB.Driver.Core.TestHelpers;
22+
using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
23+
using Xunit;
24+
25+
namespace MongoDB.Driver.Tests
26+
{
27+
public class MongoCollectionProseTests
28+
{
29+
[SkippableFact]
30+
public void WriteConcernError_errInfo_should_be_propagated()
31+
{
32+
var failPointFeature = CoreTestConfiguration.Cluster.Description.Type == ClusterType.Sharded
33+
? Feature.FailPointsFailCommandForSharded
34+
: Feature.FailPointsFailCommand;
35+
RequireServer.Check().Supports(failPointFeature);
36+
37+
var failPointCommand = @"
38+
{
39+
configureFailPoint : 'failCommand',
40+
data : {
41+
failCommands : ['insert'],
42+
writeConcernError : {
43+
code : 100,
44+
codeName : 'UnsatisfiableWriteConcern',
45+
errmsg : 'Not enough data-bearing nodes',
46+
errInfo : {
47+
writeConcern : {
48+
w : 2,
49+
wtimeout : 0,
50+
provenance : 'clientSupplied'
51+
}
52+
}
53+
}
54+
},
55+
mode: { times: 1 }
56+
}";
57+
58+
using (ConfigureFailPoint(failPointCommand))
59+
{
60+
var client = DriverTestConfiguration.Client;
61+
var database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
62+
var collection = database.GetCollection<BsonDocument>(DriverTestConfiguration.CollectionNamespace.CollectionName);
63+
64+
var exception = Record.Exception(() => collection.InsertOne(new BsonDocument()));
65+
66+
exception.Should().NotBeNull();
67+
var e = exception.InnerException.Should().BeOfType<MongoBulkWriteException<BsonDocument>>().Subject;
68+
var writeConcernError = e.WriteConcernError;
69+
writeConcernError.Code.Should().Be(100);
70+
writeConcernError.CodeName.Should().Be("UnsatisfiableWriteConcern");
71+
writeConcernError.Details.Should().Be("{ writeConcern : { w : 2, wtimeout : 0, provenance : 'clientSupplied' } }");
72+
writeConcernError.Message.Should().Be("Not enough data-bearing nodes");
73+
}
74+
}
75+
76+
// private methods
77+
private FailPoint ConfigureFailPoint(string failpointCommand)
78+
{
79+
var cluster = DriverTestConfiguration.Client.Cluster;
80+
var session = NoCoreSession.NewHandle();
81+
return FailPoint.Configure(cluster, session, BsonDocument.Parse(failpointCommand));
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)