Skip to content
This repository was archived by the owner on Apr 20, 2019. It is now read-only.

Commit a49559a

Browse files
committed
CSHARP-2309: Transaction JSON tests should be run using the async API also.
1 parent dfe12cf commit a49559a

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

tests/MongoDB.Driver.Tests/JsonDrivenTests/JsonDrivenAbortTransactionTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System.Collections.Generic;
1717
using System.Threading;
18+
using System.Threading.Tasks;
1819
using MongoDB.Bson;
1920
using MongoDB.Bson.TestHelpers.JsonDrivenTests;
2021

@@ -44,5 +45,10 @@ protected override void CallMethod(CancellationToken cancellationToken)
4445
{
4546
_session.AbortTransaction(cancellationToken);
4647
}
48+
49+
protected override Task CallMethodAsync(CancellationToken cancellationToken)
50+
{
51+
return _session.AbortTransactionAsync(cancellationToken);
52+
}
4753
}
4854
}

tests/MongoDB.Driver.Tests/JsonDrivenTests/JsonDrivenCommitTransactionTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System.Collections.Generic;
1717
using System.Threading;
18+
using System.Threading.Tasks;
1819
using MongoDB.Bson;
1920
using MongoDB.Bson.TestHelpers.JsonDrivenTests;
2021

@@ -44,5 +45,10 @@ protected override void CallMethod(CancellationToken cancellationToken)
4445
{
4546
_session.CommitTransaction(cancellationToken);
4647
}
48+
49+
protected override Task CallMethodAsync(CancellationToken cancellationToken)
50+
{
51+
return _session.CommitTransactionAsync(cancellationToken);
52+
}
4753
}
4854
}

tests/MongoDB.Driver.Tests/JsonDrivenTests/JsonDrivenStartTransactionTest.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System.Collections.Generic;
1717
using System.Threading;
18+
using System.Threading.Tasks;
1819
using MongoDB.Bson;
1920
using MongoDB.Bson.TestHelpers.JsonDrivenTests;
2021

@@ -48,6 +49,12 @@ protected override void CallMethod(CancellationToken cancellationToken)
4849
_session.StartTransaction(_options);
4950
}
5051

52+
protected override Task CallMethodAsync(CancellationToken cancellationToken)
53+
{
54+
_session.StartTransaction(_options); // there is no async version, just call the sync version
55+
return Task.FromResult(true);
56+
}
57+
5158
protected override void SetArgument(string name, BsonValue value)
5259
{
5360
switch (name)

tests/MongoDB.Driver.Tests/Specifications/transactions/TransactionTestRunner.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private void Run(BsonDocument shared, BsonDocument test)
7777
//}
7878

7979
JsonDrivenHelper.EnsureAllFieldsAreValid(shared, "_path", "database_name", "collection_name", "data", "tests");
80-
JsonDrivenHelper.EnsureAllFieldsAreValid(test, "description", "clientOptions", "failPoint", "sessionOptions", "operations", "expectations", "outcome");
80+
JsonDrivenHelper.EnsureAllFieldsAreValid(test, "description", "clientOptions", "failPoint", "sessionOptions", "operations", "expectations", "outcome", "async");
8181

8282
_databaseName = shared["database_name"].AsString;
8383
_collectionName = shared["collection_name"].AsString;
@@ -281,7 +281,14 @@ private void ExecuteOperations(IMongoClient client, Dictionary<string, object> o
281281
var jsonDrivenTest = factory.CreateTest(receiver, name);
282282

283283
jsonDrivenTest.Arrange(operation);
284-
jsonDrivenTest.Act(CancellationToken.None);
284+
if (test["async"].AsBoolean)
285+
{
286+
jsonDrivenTest.ActAsync(CancellationToken.None).GetAwaiter().GetResult();
287+
}
288+
else
289+
{
290+
jsonDrivenTest.Act(CancellationToken.None);
291+
}
285292
jsonDrivenTest.Assert();
286293
}
287294
}
@@ -415,6 +422,20 @@ protected override string PathPrefix
415422
}
416423
}
417424

425+
// protected methods
426+
protected override IEnumerable<JsonDrivenTestCase> CreateTestCases(BsonDocument document)
427+
{
428+
foreach (var testCase in base.CreateTestCases(document))
429+
{
430+
foreach (var async in new[] { false, true })
431+
{
432+
var name = $"{testCase.Name}:async={async}";
433+
var test = testCase.Test.DeepClone().AsBsonDocument.Add("async", async);
434+
yield return new JsonDrivenTestCase(name, testCase.Shared, test);
435+
}
436+
}
437+
}
438+
418439
protected override bool ShouldReadJsonDocument(string path)
419440
{
420441
return base.ShouldReadJsonDocument(path); // && path.EndsWith("commit.json");

0 commit comments

Comments
 (0)