PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/MongoDB.Driver.Tests/ClusterRegistryTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 126 lines | 96 code | 15 blank | 15 comment | 0 complexity | 1683a10ddc8a4d5fa20185d23754828d MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2010-2014 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.Linq;
  17. using System.Net;
  18. using System.Security.Authentication;
  19. using System.Security.Cryptography.X509Certificates;
  20. using FluentAssertions;
  21. using MongoDB.Bson;
  22. using MongoDB.Driver;
  23. using MongoDB.Driver.Core.Clusters;
  24. using MongoDB.Driver.Core.Clusters.ServerSelectors;
  25. using NUnit.Framework;
  26. namespace MongoDB.Driver.Tests
  27. {
  28. [TestFixture]
  29. public class ClusterRegistryTests
  30. {
  31. [Test]
  32. public void Instance_should_return_the_same_instance_every_time()
  33. {
  34. var subject1 = ClusterRegistry.Instance;
  35. var subject2 = ClusterRegistry.Instance;
  36. subject2.Should().BeSameAs(subject1);
  37. }
  38. [Test]
  39. public void GetOrCreateCluster_should_return_a_cluster_with_the_correct_settings()
  40. {
  41. var credentials = new[] { MongoCredential.CreateMongoCRCredential("source", "username", "password") };
  42. var servers = new[] { new MongoServerAddress("localhost") };
  43. var sslSettings = new SslSettings
  44. {
  45. CheckCertificateRevocation = true,
  46. EnabledSslProtocols = SslProtocols.Ssl3
  47. };
  48. var clientSettings = new MongoClientSettings
  49. {
  50. ConnectionMode = ConnectionMode.ReplicaSet,
  51. ConnectTimeout = TimeSpan.FromSeconds(1),
  52. Credentials = credentials,
  53. GuidRepresentation = GuidRepresentation.Standard,
  54. IPv6 = true,
  55. MaxConnectionIdleTime = TimeSpan.FromSeconds(2),
  56. MaxConnectionLifeTime = TimeSpan.FromSeconds(3),
  57. MaxConnectionPoolSize = 10,
  58. MinConnectionPoolSize = 5,
  59. ReplicaSetName = "rs",
  60. LocalThreshold = TimeSpan.FromMilliseconds(20),
  61. Servers = servers,
  62. ServerSelectionTimeout = TimeSpan.FromSeconds(5),
  63. SocketTimeout = TimeSpan.FromSeconds(4),
  64. SslSettings = sslSettings,
  65. UseSsl = true,
  66. VerifySslCertificate = true,
  67. WaitQueueSize = 20,
  68. WaitQueueTimeout = TimeSpan.FromSeconds(5)
  69. };
  70. var subject = new ClusterRegistry();
  71. using (var cluster = subject.GetOrCreateCluster(clientSettings.ToClusterKey()))
  72. {
  73. var address = clientSettings.Servers.Single();
  74. var endPoints = new[] { new DnsEndPoint(address.Host, address.Port) };
  75. cluster.Settings.ConnectionMode.Should().Be(ClusterConnectionMode.ReplicaSet);
  76. cluster.Settings.EndPoints.Equals(endPoints);
  77. cluster.Settings.ReplicaSetName.Should().Be("rs");
  78. cluster.Settings.ServerSelectionTimeout.Should().Be(clientSettings.ServerSelectionTimeout);
  79. cluster.Settings.PostServerSelector.Should().NotBeNull().And.Subject.Should().BeOfType<LatencyLimitingServerSelector>();
  80. cluster.Settings.MaxServerSelectionWaitQueueSize.Should().Be(20);
  81. var serverDescription = cluster.Description.Servers.Single(s => s.EndPoint.Equals(endPoints[0]));
  82. serverDescription.EndPoint.Should().Be(endPoints[0]);
  83. // TODO: don't know how to test the rest of the settings because they are all private to the cluster
  84. }
  85. }
  86. [Test]
  87. public void GetOrCreateCluster_should_return_a_different_cluster_if_client_settings_are_not_equal()
  88. {
  89. var clientSettings1 = new MongoClientSettings();
  90. var clientSettings2 = new MongoClientSettings() { IPv6 = true };
  91. var subject = new ClusterRegistry();
  92. using (var cluster1 = subject.GetOrCreateCluster(clientSettings1.ToClusterKey()))
  93. using (var cluster2 = subject.GetOrCreateCluster(clientSettings2.ToClusterKey()))
  94. {
  95. cluster2.Should().NotBeSameAs(cluster1);
  96. }
  97. }
  98. [Test]
  99. public void GetOrCreateCluster_should_return_the_same_cluster_if_client_settings_are_equal()
  100. {
  101. var clientSettings1 = new MongoClientSettings();
  102. var clientSettings2 = new MongoClientSettings();
  103. var subject = new ClusterRegistry();
  104. using (var cluster1 = subject.GetOrCreateCluster(clientSettings1.ToClusterKey()))
  105. using (var cluster2 = subject.GetOrCreateCluster(clientSettings2.ToClusterKey()))
  106. {
  107. cluster2.Should().BeSameAs(cluster1);
  108. }
  109. }
  110. }
  111. }