PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/MongoDB.Driver.Tests/ClusterRegistryTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 171 lines | 137 code | 19 blank | 15 comment | 0 complexity | dcb7412eca50ef933219344739ab1ac0 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2010-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. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Net;
  19. using System.Security.Authentication;
  20. using FluentAssertions;
  21. using MongoDB.Bson;
  22. using MongoDB.Bson.TestHelpers;
  23. using MongoDB.Driver.Core.Clusters;
  24. using MongoDB.Driver.Core.Compression;
  25. using MongoDB.Driver.Core.Configuration;
  26. using MongoDB.Driver.Core.Misc;
  27. using Xunit;
  28. namespace MongoDB.Driver.Tests
  29. {
  30. public class ClusterRegistryTests
  31. {
  32. [Fact]
  33. public void Instance_should_return_the_same_instance_every_time()
  34. {
  35. var subject1 = ClusterRegistry.Instance;
  36. var subject2 = ClusterRegistry.Instance;
  37. subject2.Should().BeSameAs(subject1);
  38. }
  39. [Fact]
  40. public void GetOrCreateCluster_should_return_a_cluster_with_the_correct_settings()
  41. {
  42. var clusterConfigurator = new Action<ClusterBuilder>(b => { });
  43. #pragma warning disable 618
  44. var credentials = new List<MongoCredential> { MongoCredential.CreateMongoCRCredential("source", "username", "password") };
  45. #pragma warning restore 618
  46. var servers = new[] { new MongoServerAddress("localhost"), new MongoServerAddress("127.0.0.1", 30000), new MongoServerAddress("[::1]", 27018) };
  47. var sslSettings = new SslSettings
  48. {
  49. CheckCertificateRevocation = true,
  50. EnabledSslProtocols = SslProtocols.Tls
  51. };
  52. var kmsProviders = new Dictionary<string, IReadOnlyDictionary<string, object>>()
  53. {
  54. { "local", new Dictionary<string, object>() { { "key" , "test" } } }
  55. };
  56. var schemaMap = new Dictionary<string, BsonDocument>()
  57. {
  58. { "db.coll", new BsonDocument() }
  59. };
  60. var clusterKey = new ClusterKey(
  61. allowInsecureTls: false,
  62. applicationName: "app1",
  63. clusterConfigurator: clusterConfigurator,
  64. compressors: new[] { new CompressorConfiguration(CompressorType.Zlib) },
  65. connectionMode: ConnectionMode.ReplicaSet,
  66. connectTimeout: TimeSpan.FromSeconds(1),
  67. credentials: credentials,
  68. heartbeatInterval: TimeSpan.FromSeconds(2),
  69. heartbeatTimeout: TimeSpan.FromSeconds(3),
  70. ipv6: true,
  71. kmsProviders: kmsProviders,
  72. localThreshold: TimeSpan.FromSeconds(4),
  73. maxConnectionIdleTime: TimeSpan.FromSeconds(5),
  74. maxConnectionLifeTime: TimeSpan.FromSeconds(6),
  75. maxConnectionPoolSize: 7,
  76. minConnectionPoolSize: 8,
  77. receiveBufferSize: 9,
  78. replicaSetName: "rs",
  79. schemaMap: schemaMap,
  80. scheme: ConnectionStringScheme.MongoDB,
  81. sdamLogFilename: "sdam.log",
  82. sendBufferSize: 10,
  83. servers: servers,
  84. serverSelectionTimeout: TimeSpan.FromSeconds(11),
  85. socketTimeout: TimeSpan.FromSeconds(12),
  86. sslSettings: sslSettings,
  87. useTls: true,
  88. waitQueueSize: 13,
  89. waitQueueTimeout: TimeSpan.FromSeconds(14));
  90. var subject = new ClusterRegistry();
  91. using (var cluster = subject.GetOrCreateCluster(clusterKey))
  92. {
  93. var expectedEndPoints = new EndPoint[]
  94. {
  95. new DnsEndPoint("localhost", 27017),
  96. new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30000),
  97. new IPEndPoint(IPAddress.Parse("[::1]"), 27018)
  98. };
  99. cluster.Settings.ConnectionMode.Should().Be(clusterKey.ConnectionMode.ToCore());
  100. cluster.Settings.KmsProviders.Should().BeEquivalentTo(kmsProviders);
  101. cluster.Settings.EndPoints.Should().Equal(expectedEndPoints);
  102. cluster.Settings.MaxServerSelectionWaitQueueSize.Should().Be(clusterKey.WaitQueueSize);
  103. cluster.Settings.ReplicaSetName.Should().Be(clusterKey.ReplicaSetName);
  104. cluster.Settings.SchemaMap.Should().BeEquivalentTo(schemaMap);
  105. cluster.Settings.Scheme.Should().Be(clusterKey.Scheme);
  106. cluster.Settings.ServerSelectionTimeout.Should().Be(clusterKey.ServerSelectionTimeout);
  107. cluster.Description.Servers.Select(s => s.EndPoint).Should().BeEquivalentTo(expectedEndPoints);
  108. // TODO: don't know how to test the rest of the settings because they are all private to the cluster
  109. }
  110. }
  111. [Fact]
  112. public void GetOrCreateCluster_should_return_a_different_cluster_if_client_settings_are_not_equal()
  113. {
  114. var clientSettings1 = new MongoClientSettings();
  115. var clientSettings2 = new MongoClientSettings() { IPv6 = true };
  116. var subject = new ClusterRegistry();
  117. using (var cluster1 = subject.GetOrCreateCluster(clientSettings1.ToClusterKey()))
  118. using (var cluster2 = subject.GetOrCreateCluster(clientSettings2.ToClusterKey()))
  119. {
  120. cluster2.Should().NotBeSameAs(cluster1);
  121. }
  122. }
  123. [Fact]
  124. public void GetOrCreateCluster_should_return_the_same_cluster_if_client_settings_are_equal()
  125. {
  126. var clientSettings1 = new MongoClientSettings();
  127. var clientSettings2 = new MongoClientSettings();
  128. var subject = new ClusterRegistry();
  129. using (var cluster1 = subject.GetOrCreateCluster(clientSettings1.ToClusterKey()))
  130. using (var cluster2 = subject.GetOrCreateCluster(clientSettings2.ToClusterKey()))
  131. {
  132. cluster2.Should().BeSameAs(cluster1);
  133. }
  134. }
  135. [Fact]
  136. public void UnregisterAndDisposeCluster_should_unregister_and_dispose_the_cluster()
  137. {
  138. var subject = new ClusterRegistry();
  139. var settings = new MongoClientSettings();
  140. var clusterKey = settings.ToClusterKey();
  141. var cluster = subject.GetOrCreateCluster(clusterKey);
  142. subject.UnregisterAndDisposeCluster(cluster);
  143. subject._registry().Count.Should().Be(0);
  144. cluster._state().Should().Be(2);
  145. }
  146. }
  147. internal static class ClusterRegistryReflector
  148. {
  149. public static Dictionary<ClusterKey, ICluster> _registry(this ClusterRegistry clusterRegistry) => (Dictionary<ClusterKey, ICluster>)Reflector.GetFieldValue(clusterRegistry, nameof(_registry));
  150. public static int _state(this ICluster cluster) => (int)((InterlockedInt32)Reflector.GetFieldValue(cluster, nameof(_state))).Value;
  151. }
  152. }