Skip to content

Commit 1c93528

Browse files
CSHARP-3533/CSHARP-3619: Test that session is unpinned when executing a non-transaction operation. / Unpin sessions after all abortTransaction attempts. (#540)
CSHARP-3533/CSHARP-3619: Test that session is unpinned when executing a non-transaction operation. / Unpin sessions after all abortTransaction attempts.
1 parent b2668fb commit 1c93528

File tree

76 files changed

+666
-20
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+666
-20
lines changed

src/MongoDB.Driver.Core/Core/Bindings/CoreSession.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ public bool IsInTransaction
139139
}
140140
catch (Exception exception) when (ShouldRetryEndTransactionException(exception))
141141
{
142+
// unpin if retryable error
143+
_currentTransaction.PinnedServer = null;
144+
142145
// ignore exception and retry
143146
}
144147
catch
@@ -159,6 +162,9 @@ public bool IsInTransaction
159162
finally
160163
{
161164
_currentTransaction.SetState(CoreTransactionState.Aborted);
165+
// The transaction is aborted.The session MUST be unpinned regardless
166+
// of whether the abortTransaction command succeeds or fails
167+
_currentTransaction.PinnedServer = null;
162168
}
163169
}
164170

@@ -182,6 +188,9 @@ public bool IsInTransaction
182188
}
183189
catch (Exception exception) when (ShouldRetryEndTransactionException(exception))
184190
{
191+
// unpin if retryable error
192+
_currentTransaction.PinnedServer = null;
193+
185194
// ignore exception and retry
186195
}
187196
catch
@@ -202,6 +211,9 @@ public bool IsInTransaction
202211
finally
203212
{
204213
_currentTransaction.SetState(CoreTransactionState.Aborted);
214+
// The transaction is aborted.The session MUST be unpinned regardless
215+
// of whether the abortTransaction command succeeds or fails
216+
_currentTransaction.PinnedServer = null;
205217
}
206218
}
207219

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -523,13 +523,7 @@ private void VerifyCollectionData(IEnumerable<BsonDocument> expectedDocuments)
523523
public class TestCaseFactory : JsonDrivenTestCaseFactory
524524
{
525525
// protected properties
526-
protected override string PathPrefix
527-
{
528-
get
529-
{
530-
return "MongoDB.Driver.Tests.Specifications.transactions.tests.";
531-
}
532-
}
526+
protected override string PathPrefix => "MongoDB.Driver.Tests.Specifications.transactions.tests.legacy";
533527

534528
// protected methods
535529
protected override IEnumerable<JsonDrivenTestCase> CreateTestCases(BsonDocument document)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* Copyright 2021-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 System.Collections.Generic;
17+
using MongoDB.Bson;
18+
using MongoDB.Bson.TestHelpers.JsonDrivenTests;
19+
using MongoDB.Driver.Tests.UnifiedTestOperations;
20+
using Xunit;
21+
22+
namespace MongoDB.Driver.Tests.Specifications.transactions
23+
{
24+
public sealed class TransactionUnifiedTestRunner
25+
{
26+
[SkippableTheory]
27+
[ClassData(typeof(TestCaseFactory))]
28+
public void Run(JsonDrivenTestCase testCase)
29+
{
30+
using (var runner = new UnifiedTestRunner())
31+
{
32+
runner.Run(testCase);
33+
}
34+
}
35+
36+
// nested types
37+
public class TestCaseFactory : JsonDrivenTestCaseFactory
38+
{
39+
// protected properties
40+
protected override string PathPrefix => "MongoDB.Driver.Tests.Specifications.transactions.tests.unified.";
41+
42+
// protected methods
43+
protected override IEnumerable<JsonDrivenTestCase> CreateTestCases(BsonDocument document)
44+
{
45+
foreach (var testCase in base.CreateTestCases(document))
46+
{
47+
foreach (var async in new[] { false, true })
48+
{
49+
var name = $"{testCase.Name}:async={async}";
50+
var test = testCase.Test.DeepClone().AsBsonDocument.Add("async", async);
51+
yield return new JsonDrivenTestCase(name, testCase.Shared, test);
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)