|
| 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