Skip to content

Commit c2aa519

Browse files
committed
CSHARP-2129: Insert with RetryWrites=true should work on standalone.
1 parent 4b8703a commit c2aa519

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.Threading.Tasks;
1919
using MongoDB.Driver.Core.Bindings;
2020
using MongoDB.Driver.Core.Connections;
21+
using MongoDB.Driver.Core.Servers;
2122

2223
namespace MongoDB.Driver.Core.Operations
2324
{
@@ -129,7 +130,9 @@ public static async Task<TResult> ExecuteAsync<TResult>(IRetryableWriteOperation
129130
// privates static methods
130131
private static bool AreRetryableWritesSupported(ConnectionDescription connectionDescription)
131132
{
132-
return connectionDescription.IsMasterResult.LogicalSessionTimeout != null;
133+
return
134+
connectionDescription.IsMasterResult.LogicalSessionTimeout != null &&
135+
connectionDescription.IsMasterResult.ServerType != ServerType.Standalone;
133136
}
134137

135138
private static bool IsRetryableException(Exception ex)

tests/MongoDB.Driver.Tests/RetryableWritesTests.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,36 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
1617
using FluentAssertions;
1718
using MongoDB.Bson;
1819
using MongoDB.Bson.TestHelpers.XunitExtensions;
1920
using MongoDB.Driver.Core;
2021
using MongoDB.Driver.Core.Clusters;
22+
using MongoDB.Driver.Core.Configuration;
2123
using MongoDB.Driver.Core.Events;
2224
using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
2325
using MongoDB.Driver.TestHelpers;
26+
using Xunit;
2427

2528
namespace MongoDB.Driver.Tests
2629
{
2730
public class RetryableWritesTests
2831
{
32+
[SkippableFact]
33+
public void Insert_with_RetryWrites_true_should_work_whether_retryable_writes_are_supported_or_not()
34+
{
35+
RequireServer.Check();
36+
37+
using (var client = GetClient())
38+
{
39+
var database = client.GetDatabase(DriverTestConfiguration.DatabaseNamespace.DatabaseName);
40+
var collection = database.GetCollection<BsonDocument>(DriverTestConfiguration.CollectionNamespace.CollectionName);
41+
var document = new BsonDocument("x", 1);
42+
collection.InsertOne(document);
43+
}
44+
}
45+
2946
[SkippableFact]
3047
public void TxnNumber_should_be_included_with_FindOneAndDelete()
3148
{
@@ -159,21 +176,31 @@ public void TxnNumber_should_be_included_with_UpdateOne()
159176
}
160177
}
161178

162-
private DisposableMongoClient GetClient(EventCapturer capturer)
179+
private DisposableMongoClient GetClient()
180+
{
181+
return GetClient(cb => { });
182+
}
183+
184+
private DisposableMongoClient GetClient(Action<ClusterBuilder> clusterConfigurator)
163185
{
164186
var connectionString = CoreTestConfiguration.ConnectionString.ToString();
165187
var clientSettings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
166188
clientSettings.RetryWrites = true;
167-
clientSettings.ClusterConfigurator = cb => cb.Subscribe(capturer);
189+
clientSettings.ClusterConfigurator = cb => cb.Subscribe(clusterConfigurator);
168190

169191
return new DisposableMongoClient(new MongoClient(clientSettings));
170192
}
171193

194+
private DisposableMongoClient GetClient(EventCapturer capturer)
195+
{
196+
return GetClient(cb => cb.Subscribe(capturer));
197+
}
198+
172199
private void RequireSupportForRetryableWrites()
173200
{
174201
RequireServer.Check()
175-
.VersionGreaterThanOrEqualTo("3.6.0-rc0")
176-
.ClusterTypes(ClusterType.Sharded, ClusterType.ReplicaSet);
202+
.VersionGreaterThanOrEqualTo("3.6.0-rc0")
203+
.ClusterTypes(ClusterType.Sharded, ClusterType.ReplicaSet);
177204
}
178205
}
179206
}

0 commit comments

Comments
 (0)