Skip to content

CSHARP-5392: Add integration tests for transactions write concern behavior #1733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make it inherit from LoggableTestClass, so we get logs on failure.

{
private string _collectionName = "txn-test-col";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constant?

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async Task?

{
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<BsonDocument>(_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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to use Record in order to validate the absence of exception. The test will just fail on exception.
It's usually used to validate that exception was thrown.

collection.Find(new BsonDocument("n", 1)).First().Should().NotBeNull().And.Subject["n"].AsInt32.Should().Be(1);
}
}
}