From b493b0c6f9b2fea66ac7ddda767de7c0410aa6d3 Mon Sep 17 00:00:00 2001 From: adelinowona Date: Wed, 16 Jul 2025 17:11:16 -0400 Subject: [PATCH 1/2] CSHARP-5392: Add integration tests for transactions write concern behavior --- .../transactions/TransactionsProseTests.cs | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 tests/MongoDB.Driver.Tests/Specifications/transactions/TransactionsProseTests.cs diff --git a/tests/MongoDB.Driver.Tests/Specifications/transactions/TransactionsProseTests.cs b/tests/MongoDB.Driver.Tests/Specifications/transactions/TransactionsProseTests.cs new file mode 100644 index 00000000000..d1430847d2c --- /dev/null +++ b/tests/MongoDB.Driver.Tests/Specifications/transactions/TransactionsProseTests.cs @@ -0,0 +1,73 @@ +/* Copyright 2010-present MongoDB Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using FluentAssertions; +using MongoDB.Bson; +using MongoDB.Driver.Core.Clusters; +using MongoDB.Driver.Core.TestHelpers.XunitExtensions; +using MongoDB.TestHelpers.XunitExtensions; +using Xunit; + +namespace MongoDB.Driver.Tests.Specifications.transactions +{ + [Trait("Category", "Integration")] + public class TransactionsProseTests + { + private string _collectionName = "txn-test-col"; + private string _databaseName = "txn-test"; + + // https://github.com/mongodb/specifications/blob/fc7996db26d0ea92091a5034c6acb287ef7282fe/source/transactions/tests/README.md#10-write-concern-not-inherited-from-collection-object-inside-transaction + [Theory] + [ParameterAttributeData] + public async void Ensure_write_concern_is_not_inherited_from_collection_object_inside_transaction([Values(false, true)] bool async) + { + RequireServer.Check().ClusterTypes(ClusterType.LoadBalanced, ClusterType.ReplicaSet, ClusterType.Sharded); + + using var client = DriverTestConfiguration.CreateMongoClient(); + var database = client.GetDatabase(_databaseName).WithWriteConcern(WriteConcern.WMajority); + database.DropCollection(_collectionName); + + var collection = client.GetDatabase(_databaseName).GetCollection(_collectionName) + .WithWriteConcern(WriteConcern.Unacknowledged); + + Exception exception; + using (var session = client.StartSession()) + { + session.StartTransaction(); + + if (async) + { + exception = await Record.ExceptionAsync( async () => + { + await collection.InsertOneAsync(new BsonDocument("n", 1)); + await session.CommitTransactionAsync(); + }); + } + else + { + exception = Record.Exception(() => + { + collection.InsertOne(new BsonDocument("n", 1)); + session.CommitTransaction(); + }); + } + } + + exception.Should().BeNull(); + collection.Find(new BsonDocument("n", 1)).First().Should().NotBeNull().And.Subject["n"].AsInt32.Should().Be(1); + } + } +} \ No newline at end of file From 829ac1a232568c0d2db9f41a246ace32cae6071a Mon Sep 17 00:00:00 2001 From: adelinowona Date: Mon, 21 Jul 2025 13:47:17 -0400 Subject: [PATCH 2/2] address pr comments --- .../transactions/TransactionsProseTests.cs | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/tests/MongoDB.Driver.Tests/Specifications/transactions/TransactionsProseTests.cs b/tests/MongoDB.Driver.Tests/Specifications/transactions/TransactionsProseTests.cs index d1430847d2c..7a8f8cb2b2c 100644 --- a/tests/MongoDB.Driver.Tests/Specifications/transactions/TransactionsProseTests.cs +++ b/tests/MongoDB.Driver.Tests/Specifications/transactions/TransactionsProseTests.cs @@ -13,60 +13,58 @@ * limitations under the License. */ -using System; +using System.Threading.Tasks; using FluentAssertions; using MongoDB.Bson; using MongoDB.Driver.Core.Clusters; +using MongoDB.Driver.Core.TestHelpers.Logging; using MongoDB.Driver.Core.TestHelpers.XunitExtensions; using MongoDB.TestHelpers.XunitExtensions; using Xunit; +using Xunit.Abstractions; namespace MongoDB.Driver.Tests.Specifications.transactions { [Trait("Category", "Integration")] - public class TransactionsProseTests + public class TransactionsProseTests : LoggableTestClass { - private string _collectionName = "txn-test-col"; - private string _databaseName = "txn-test"; + private const string CollectionName = "txn-test-col"; + private const string DatabaseName = "txn-test"; + + public TransactionsProseTests(ITestOutputHelper output) : base(output) + { + } // https://github.com/mongodb/specifications/blob/fc7996db26d0ea92091a5034c6acb287ef7282fe/source/transactions/tests/README.md#10-write-concern-not-inherited-from-collection-object-inside-transaction [Theory] [ParameterAttributeData] - public async void Ensure_write_concern_is_not_inherited_from_collection_object_inside_transaction([Values(false, true)] bool async) + public async Task Ensure_write_concern_is_not_inherited_from_collection_object_inside_transaction([Values(false, true)] bool async) { RequireServer.Check().ClusterTypes(ClusterType.LoadBalanced, ClusterType.ReplicaSet, ClusterType.Sharded); using var client = DriverTestConfiguration.CreateMongoClient(); - var database = client.GetDatabase(_databaseName).WithWriteConcern(WriteConcern.WMajority); - database.DropCollection(_collectionName); + var database = client.GetDatabase(DatabaseName).WithWriteConcern(WriteConcern.WMajority); + database.DropCollection(CollectionName); - var collection = client.GetDatabase(_databaseName).GetCollection(_collectionName) + var collection = client.GetDatabase(DatabaseName).GetCollection(CollectionName) .WithWriteConcern(WriteConcern.Unacknowledged); - Exception exception; using (var session = client.StartSession()) { session.StartTransaction(); if (async) { - exception = await Record.ExceptionAsync( async () => - { - await collection.InsertOneAsync(new BsonDocument("n", 1)); - await session.CommitTransactionAsync(); - }); + await collection.InsertOneAsync(new BsonDocument("n", 1)); + await session.CommitTransactionAsync(); } else { - exception = Record.Exception(() => - { - collection.InsertOne(new BsonDocument("n", 1)); - session.CommitTransaction(); - }); + collection.InsertOne(new BsonDocument("n", 1)); + session.CommitTransaction(); } } - exception.Should().BeNull(); collection.Find(new BsonDocument("n", 1)).First().Should().NotBeNull().And.Subject["n"].AsInt32.Should().Be(1); } }