Skip to content

Commit b9e5320

Browse files
committed
Make CreateCluster in CoreTestConfiguration thread safe.
1 parent 824691a commit b9e5320

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/MongoDB.Driver.Core.TestHelpers/CoreTestConfiguration.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,20 @@ public static ClusterBuilder ConfigureLogging(ClusterBuilder builder)
143143

144144
public static ICluster CreateCluster()
145145
{
146-
var hasWritableServer = false;
146+
var hasWritableServer = 0;
147147
var builder = ConfigureCluster();
148148
var cluster = builder.BuildCluster();
149149
cluster.DescriptionChanged += (o, e) =>
150150
{
151-
hasWritableServer = e.NewClusterDescription.Servers.Any(
151+
var anyWritableServer = e.NewClusterDescription.Servers.Any(
152152
description => description.Type.IsWritable());
153+
Interlocked.Exchange(ref hasWritableServer, anyWritableServer ? 1 : 0);
153154
};
154155
cluster.Initialize();
155156

156157
// wait until the cluster has connected to a writable server
157-
SpinWait.SpinUntil(() => hasWritableServer, TimeSpan.FromSeconds(30));
158-
if (!hasWritableServer)
158+
SpinWait.SpinUntil(() => Interlocked.CompareExchange(ref hasWritableServer, 0, 0) != 0, TimeSpan.FromSeconds(30));
159+
if (Interlocked.CompareExchange(ref hasWritableServer, 0, 0) == 0)
159160
{
160161
var message = string.Format(
161162
"Test cluster has no writable server. Client view of the cluster is {0}.",

0 commit comments

Comments
 (0)