Skip to content

Commit bfb2a58

Browse files
committed
CSHARP-1492: Added UnregisterAndDisposeCluster to ClusterRegistry.
1 parent b11826d commit bfb2a58

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

src/MongoDB.Driver.Core/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@
3434
[assembly: InternalsVisibleTo("MongoDB.Driver.Core.TestHelpers")]
3535
[assembly: InternalsVisibleTo("MongoDB.Driver.Core.Tests")]
3636
[assembly: InternalsVisibleTo("MongoDB.Driver.Core.Tests.Dotnet")]
37+
[assembly: InternalsVisibleTo("MongoDB.Driver.Tests")]
38+
[assembly: InternalsVisibleTo("MongoDB.Driver.Tests.Dotnet")]
3739
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

src/MongoDB.Driver/ClusterRegistry.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,19 @@ namespace MongoDB.Driver
3535
/// <summary>
3636
/// Represents a registry of already created clusters.
3737
/// </summary>
38-
internal class ClusterRegistry
38+
public class ClusterRegistry
3939
{
4040
#region static
4141
// static fields
4242
private static readonly ClusterRegistry __instance = new ClusterRegistry();
4343

4444
// static properties
45+
/// <summary>
46+
/// Gets the default cluster registry.
47+
/// </summary>
48+
/// <value>
49+
/// The default cluster registry.
50+
/// </value>
4551
public static ClusterRegistry Instance
4652
{
4753
get { return __instance; }
@@ -155,7 +161,7 @@ private TcpStreamSettings ConfigureTcp(TcpStreamSettings settings, ClusterKey cl
155161
writeTimeout: clusterKey.SocketTimeout);
156162
}
157163

158-
public ICluster GetOrCreateCluster(ClusterKey clusterKey)
164+
internal ICluster GetOrCreateCluster(ClusterKey clusterKey)
159165
{
160166
lock (_lock)
161167
{
@@ -178,5 +184,34 @@ SslPolicyErrors sslPolicyErrors
178184
{
179185
return true;
180186
}
187+
188+
/// <summary>
189+
/// Unregisters and disposes the cluster.
190+
/// </summary>
191+
/// <param name="cluster">The cluster.</param>
192+
public void UnregisterAndDisposeCluster(ICluster cluster)
193+
{
194+
Ensure.IsNotNull(cluster, nameof(cluster));
195+
196+
lock (_lock)
197+
{
198+
ClusterKey clusterKey = null;
199+
foreach (var keyValuePair in _registry)
200+
{
201+
if (object.ReferenceEquals(keyValuePair.Value, cluster))
202+
{
203+
clusterKey = keyValuePair.Key;
204+
break;
205+
}
206+
}
207+
208+
if (clusterKey != null)
209+
{
210+
_registry.Remove(clusterKey);
211+
}
212+
}
213+
214+
cluster.Dispose();
215+
}
181216
}
182217
}

tests/MongoDB.Driver.Tests/ClusterRegistryTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414
*/
1515

1616
using System;
17+
using System.Collections.Generic;
1718
using System.Linq;
1819
using System.Net;
20+
using System.Reflection;
1921
using System.Security.Authentication;
2022
using System.Security.Cryptography.X509Certificates;
2123
using FluentAssertions;
2224
using MongoDB.Bson;
2325
using MongoDB.Driver;
2426
using MongoDB.Driver.Core.Clusters;
2527
using MongoDB.Driver.Core.Clusters.ServerSelectors;
28+
using MongoDB.Driver.Core.Misc;
2629
using Xunit;
2730

2831
namespace MongoDB.Driver.Tests
@@ -127,5 +130,35 @@ public void GetOrCreateCluster_should_return_the_same_cluster_if_client_settings
127130
cluster2.Should().BeSameAs(cluster1);
128131
}
129132
}
133+
134+
[Fact]
135+
public void UnregisterAndDisposeCluster_should_unregister_and_dispose_the_cluster()
136+
{
137+
var subject = new ClusterRegistry();
138+
var settings = new MongoClientSettings();
139+
var clusterKey = settings.ToClusterKey();
140+
var cluster = subject.GetOrCreateCluster(clusterKey);
141+
142+
subject.UnregisterAndDisposeCluster(cluster);
143+
144+
subject._registry().Count.Should().Be(0);
145+
cluster._state().Should().Be(2);
146+
}
147+
}
148+
149+
internal static class ClusterRegistryTestsReflector
150+
{
151+
public static Dictionary<ClusterKey, ICluster> _registry(this ClusterRegistry clusterRegistry)
152+
{
153+
var fieldInfo = typeof(ClusterRegistry).GetField("_registry", BindingFlags.NonPublic | BindingFlags.Instance);
154+
return (Dictionary<ClusterKey, ICluster>)fieldInfo.GetValue(clusterRegistry);
155+
}
156+
157+
public static int _state(this ICluster cluster)
158+
{
159+
var fieldInfo = typeof(SingleServerCluster).GetField("_state", BindingFlags.NonPublic | BindingFlags.Instance);
160+
var interlockedInt32 = (InterlockedInt32)fieldInfo.GetValue(cluster);
161+
return interlockedInt32.Value;
162+
}
130163
}
131164
}

0 commit comments

Comments
 (0)