PageRenderTime 31ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/Core/Clusters/SingleServerClusterTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 194 lines | 145 code | 34 blank | 15 comment | 0 complexity | 10cd176fe4e7bfe9f1bc2e1c933aaaa6 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-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.Linq;
  17. using System.Net;
  18. using FluentAssertions;
  19. using MongoDB.Bson.TestHelpers.XunitExtensions;
  20. using MongoDB.Driver.Core.Configuration;
  21. using MongoDB.Driver.Core.Events;
  22. using MongoDB.Driver.Core.Servers;
  23. using MongoDB.Driver.Core.Helpers;
  24. using Moq;
  25. using Xunit;
  26. namespace MongoDB.Driver.Core.Clusters
  27. {
  28. public class SingleServerClusterTests
  29. {
  30. private EventCapturer _capturedEvents;
  31. private MockClusterableServerFactory _mockServerFactory;
  32. private ClusterSettings _settings;
  33. private EndPoint _endPoint = new DnsEndPoint("localhost", 27017);
  34. public SingleServerClusterTests()
  35. {
  36. _settings = new ClusterSettings();
  37. _mockServerFactory = new MockClusterableServerFactory();
  38. _capturedEvents = new EventCapturer();
  39. }
  40. [Fact]
  41. public void Constructor_should_throw_if_more_than_one_endpoint_is_specified()
  42. {
  43. _settings = _settings.With(endPoints: new[] { _endPoint, new DnsEndPoint("localhost", 27018) });
  44. Action act = () => new SingleServerCluster(_settings, _mockServerFactory, _capturedEvents);
  45. act.ShouldThrow<ArgumentException>();
  46. }
  47. [Fact]
  48. public void Initialize_should_throw_if_disposed()
  49. {
  50. var subject = CreateSubject();
  51. subject.Dispose();
  52. Action act = () => subject.Initialize();
  53. act.ShouldThrow<ObjectDisposedException>();
  54. }
  55. [Fact]
  56. public void Initialize_should_create_and_initialize_the_server()
  57. {
  58. var subject = CreateSubject();
  59. subject.Initialize();
  60. var mockServer = Mock.Get(_mockServerFactory.GetServer(_endPoint));
  61. mockServer.Verify(s => s.Initialize(), Times.Once);
  62. _capturedEvents.Next().Should().BeOfType<ClusterOpeningEvent>();
  63. _capturedEvents.Next().Should().BeOfType<ClusterAddingServerEvent>();
  64. _capturedEvents.Next().Should().BeOfType<ClusterAddedServerEvent>();
  65. _capturedEvents.Next().Should().BeOfType<ClusterDescriptionChangedEvent>();
  66. _capturedEvents.Next().Should().BeOfType<ClusterOpenedEvent>();
  67. _capturedEvents.Any().Should().BeFalse();
  68. }
  69. [Theory]
  70. [InlineData(ClusterConnectionMode.ReplicaSet, ServerType.ShardRouter)]
  71. [InlineData(ClusterConnectionMode.ReplicaSet, ServerType.Standalone)]
  72. [InlineData(ClusterConnectionMode.Standalone, ServerType.ReplicaSetArbiter)]
  73. [InlineData(ClusterConnectionMode.Standalone, ServerType.ReplicaSetGhost)]
  74. [InlineData(ClusterConnectionMode.Standalone, ServerType.ReplicaSetOther)]
  75. [InlineData(ClusterConnectionMode.Standalone, ServerType.ReplicaSetPrimary)]
  76. [InlineData(ClusterConnectionMode.Standalone, ServerType.ReplicaSetSecondary)]
  77. [InlineData(ClusterConnectionMode.Standalone, ServerType.ShardRouter)]
  78. [InlineData(ClusterConnectionMode.Sharded, ServerType.ReplicaSetArbiter)]
  79. [InlineData(ClusterConnectionMode.Sharded, ServerType.ReplicaSetGhost)]
  80. [InlineData(ClusterConnectionMode.Sharded, ServerType.ReplicaSetOther)]
  81. [InlineData(ClusterConnectionMode.Sharded, ServerType.ReplicaSetPrimary)]
  82. [InlineData(ClusterConnectionMode.Sharded, ServerType.ReplicaSetSecondary)]
  83. [InlineData(ClusterConnectionMode.Sharded, ServerType.Standalone)]
  84. public void Description_should_not_contain_any_servers_if_the_provided_server_is_not_of_the_required_type(ClusterConnectionMode connectionMode, ServerType serverType)
  85. {
  86. _settings = _settings.With(connectionMode: connectionMode);
  87. var subject = CreateSubject();
  88. subject.Initialize();
  89. _capturedEvents.Clear();
  90. PublishDescription(_endPoint, serverType);
  91. subject.Description.Servers.Should().BeEmpty();
  92. _capturedEvents.Next().Should().BeOfType<ClusterDescriptionChangedEvent>();
  93. _capturedEvents.Any().Should().BeFalse();
  94. }
  95. [Fact]
  96. public void Description_should_regain_a_server_if_the_provided_server_is_rebooted_to_its_expected_type()
  97. {
  98. _settings = _settings.With(connectionMode: ClusterConnectionMode.Standalone);
  99. var subject = CreateSubject();
  100. subject.Initialize();
  101. _capturedEvents.Clear();
  102. PublishDescription(_endPoint, ServerType.ReplicaSetGhost);
  103. PublishDescription(_endPoint, ServerType.Standalone);
  104. subject.Description.Servers.Count.Should().Be(1);
  105. _capturedEvents.Next().Should().BeOfType<ClusterDescriptionChangedEvent>();
  106. _capturedEvents.Next().Should().BeOfType<ClusterDescriptionChangedEvent>();
  107. _capturedEvents.Any().Should().BeFalse();
  108. }
  109. [Fact]
  110. public void Dispose_should_dispose_of_the_server()
  111. {
  112. var subject = CreateSubject();
  113. subject.Initialize();
  114. _capturedEvents.Clear();
  115. subject.Dispose();
  116. var mockServer = Mock.Get(_mockServerFactory.GetServer(_endPoint));
  117. mockServer.Verify(s => s.Dispose(), Times.Once);
  118. _capturedEvents.Next().Should().BeOfType<ClusterClosingEvent>();
  119. _capturedEvents.Next().Should().BeOfType<ClusterRemovingServerEvent>();
  120. _capturedEvents.Next().Should().BeOfType<ClusterRemovedServerEvent>();
  121. _capturedEvents.Next().Should().BeOfType<ClusterDescriptionChangedEvent>();
  122. _capturedEvents.Next().Should().BeOfType<ClusterClosedEvent>();
  123. _capturedEvents.Any().Should().BeFalse();
  124. }
  125. [Theory]
  126. [ParameterAttributeData]
  127. public void ServerDescription_type_should_be_replaced_with_Unknown_when_isMaster_setName_is_different(
  128. [Values(null, "wrong")] string isMasterSetName)
  129. {
  130. _settings = _settings.With(
  131. connectionMode: ClusterConnectionMode.Direct,
  132. replicaSetName: "rs");
  133. var subject = CreateSubject();
  134. subject.Initialize();
  135. _capturedEvents.Clear();
  136. var replicaSetConfig = new ReplicaSetConfig(new[] { _endPoint }, name: isMasterSetName, _endPoint, 1);
  137. PublishDescription(_endPoint, ServerType.Standalone, replicaSetConfig);
  138. subject.Description.Type.Should().Be(ClusterType.Unknown);
  139. var resultServers = subject.Description.Servers;
  140. resultServers.Count.Should().Be(1);
  141. resultServers.First().Type.Should().Be(ServerType.Unknown);
  142. _capturedEvents.Next().Should().BeOfType<ClusterDescriptionChangedEvent>();
  143. _capturedEvents.Any().Should().BeFalse();
  144. }
  145. // private methods
  146. private SingleServerCluster CreateSubject()
  147. {
  148. return new SingleServerCluster(_settings, _mockServerFactory, _capturedEvents);
  149. }
  150. private void PublishDescription(EndPoint endPoint, ServerType serverType, ReplicaSetConfig replicaSetConfig = null)
  151. {
  152. var current = _mockServerFactory.GetServerDescription(endPoint);
  153. var description = current.With(
  154. state: ServerState.Connected,
  155. type: serverType,
  156. replicaSetConfig: replicaSetConfig);
  157. _mockServerFactory.PublishDescription(description);
  158. }
  159. }
  160. }