Skip to content

Commit 933dae4

Browse files
committed
CSHARP-3048: RetryableWrites are only supported if session Id has a value.
1 parent 2fbc196 commit 933dae4

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/MongoDB.Driver.Core/Core/Operations/RetryableWriteOperationExecutor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ private static bool DoesContextAllowRetries(RetryableWriteContext context)
145145
return
146146
context.RetryRequested &&
147147
AreRetryableWritesSupported(context.Channel.ConnectionDescription) &&
148+
context.Binding.Session.Id != null &&
148149
!context.Binding.Session.IsInTransaction;
149150
}
150151

tests/MongoDB.Driver.Core.Tests/Core/Operations/RetryableWriteOperationExecutorTests.cs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,30 @@ namespace MongoDB.Driver.Core.Tests.Core.Operations
3333
public class RetryableWriteOperationExecutorTests
3434
{
3535
[Theory]
36-
[InlineData(false, false, false, false)]
37-
[InlineData(false, false, true, false)]
38-
[InlineData(false, true, false, false)]
39-
[InlineData(false, true, true, false)]
40-
[InlineData(true, false, false, false)]
41-
[InlineData(true, false, true, false)]
42-
[InlineData(true, true, false, true)]
43-
[InlineData(true, true, true, false)]
36+
[InlineData(false, false, false, false, false)]
37+
[InlineData(false, false, false, true, false)]
38+
[InlineData(false, false, true, false, false)]
39+
[InlineData(false, false, true, true, false)]
40+
[InlineData(false, true, false, false, false)]
41+
[InlineData(false, true, false, true, false)]
42+
[InlineData(false, true, true, false, false)]
43+
[InlineData(false, true, true, true, false)]
44+
[InlineData(true, false, false, false, false)]
45+
[InlineData(true, false, false, true, false)]
46+
[InlineData(true, false, true, false, false)]
47+
[InlineData(true, false, true, true, false)]
48+
[InlineData(true, true, false, false, false)]
49+
[InlineData(true, true, false, true, false)]
50+
[InlineData(true, true, true, false, true)]
51+
[InlineData(true, true, true, false, true)]
4452
public void DoesContextAllowRetries_should_return_expected_result(
4553
bool retryRequested,
4654
bool areRetryableWritesSupported,
55+
bool hasSessionId,
4756
bool isInTransaction,
4857
bool expectedResult)
4958
{
50-
var context = CreateContext(retryRequested, areRetryableWritesSupported, isInTransaction);
59+
var context = CreateContext(retryRequested, areRetryableWritesSupported, hasSessionId, isInTransaction);
5160

5261
var result = RetryableWriteOperationExecutorReflector.DoesContextAllowRetries(context);
5362

@@ -72,10 +81,10 @@ public void IsOperationAcknowledged_should_return_expected_result(
7281
}
7382

7483
// private methods
75-
private IWriteBinding CreateBinding(bool areRetryableWritesSupported, bool isInTransaction)
84+
private IWriteBinding CreateBinding(bool areRetryableWritesSupported, bool hasSessionId, bool isInTransaction)
7685
{
7786
var mockBinding = new Mock<IWriteBinding>();
78-
var session = CreateSession(isInTransaction);
87+
var session = CreateSession(hasSessionId, isInTransaction);
7988
var channelSource = CreateChannelSource(areRetryableWritesSupported);
8089
mockBinding.SetupGet(m => m.Session).Returns(session);
8190
mockBinding.Setup(m => m.GetWriteChannelSource(CancellationToken.None)).Returns(channelSource);
@@ -116,9 +125,9 @@ private ConnectionDescription CreateConnectionDescription(bool areRetryableWrite
116125
return connectionDescription;
117126
}
118127

119-
private RetryableWriteContext CreateContext(bool retryRequested, bool areRetryableWritesSupported, bool isInTransaction)
128+
private RetryableWriteContext CreateContext(bool retryRequested, bool areRetryableWritesSupported, bool hasSessionId, bool isInTransaction)
120129
{
121-
var binding = CreateBinding(areRetryableWritesSupported, isInTransaction);
130+
var binding = CreateBinding(areRetryableWritesSupported, hasSessionId, isInTransaction);
122131
return RetryableWriteContext.Create(binding, retryRequested, CancellationToken.None);
123132
}
124133

@@ -130,9 +139,10 @@ private IRetryableWriteOperation<BsonDocument> CreateOperation(bool withWriteCon
130139
return mockOperation.Object;
131140
}
132141

133-
private ICoreSessionHandle CreateSession(bool isInTransaction)
142+
private ICoreSessionHandle CreateSession(bool hasSessionId, bool isInTransaction)
134143
{
135144
var mockSession = new Mock<ICoreSessionHandle>();
145+
mockSession.SetupGet(m => m.Id).Returns(hasSessionId ? new BsonDocument() : null);
136146
mockSession.SetupGet(m => m.IsInTransaction).Returns(isInTransaction);
137147
return mockSession.Object;
138148
}

0 commit comments

Comments
 (0)