PageRenderTime 56ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 406 lines | 342 code | 46 blank | 18 comment | 14 complexity | 2c0acdbabec517681555e1ac34a88546 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.Net;
  17. using FluentAssertions;
  18. using MongoDB.Driver.Core.Helpers;
  19. using MongoDB.Driver.Core.Misc;
  20. using MongoDB.Driver.Core.Servers;
  21. using Xunit;
  22. namespace MongoDB.Driver.Core.Clusters
  23. {
  24. public class ClusterDescriptionTests
  25. {
  26. #region static
  27. // static fields
  28. private static readonly ClusterId __clusterId;
  29. private static readonly DnsEndPoint __endPoint1;
  30. private static readonly DnsEndPoint __endPoint2;
  31. private static readonly ServerDescription __serverDescription1;
  32. private static readonly ServerDescription __serverDescription2;
  33. private static readonly ServerId __serverId1;
  34. private static readonly ServerId __serverId2;
  35. // static constructor
  36. static ClusterDescriptionTests()
  37. {
  38. __clusterId = new ClusterId();
  39. __endPoint1 = new DnsEndPoint("localhost", 27017);
  40. __endPoint2 = new DnsEndPoint("localhost", 27018);
  41. __serverId1 = new ServerId(__clusterId, __endPoint1);
  42. __serverId2 = new ServerId(__clusterId, __endPoint2);
  43. __serverDescription1 = new ServerDescription(__serverId1, __endPoint1);
  44. __serverDescription2 = new ServerDescription(__serverId2, __endPoint2);
  45. }
  46. #endregion
  47. // static member tests
  48. [Fact]
  49. public void CreateInitial_should_return_initial_description()
  50. {
  51. var subject = ClusterDescription.CreateInitial(__clusterId, ClusterConnectionMode.Standalone);
  52. subject.ClusterId.Should().Be(__clusterId);
  53. subject.ConnectionMode.Should().Be(ClusterConnectionMode.Standalone);
  54. subject.DnsMonitorException.Should().BeNull();
  55. subject.LogicalSessionTimeout.Should().NotHaveValue();
  56. subject.Servers.Should().BeEmpty();
  57. subject.State.Should().Be(ClusterState.Disconnected);
  58. subject.Type.Should().Be(ClusterType.Unknown);
  59. }
  60. // instance member tests
  61. [Fact]
  62. public void Constructor_should_initialize_instance()
  63. {
  64. var dnsMonitorException = new Exception();
  65. var subject = new ClusterDescription(
  66. __clusterId,
  67. ClusterConnectionMode.ReplicaSet,
  68. dnsMonitorException,
  69. ClusterType.ReplicaSet,
  70. new[] { __serverDescription1, __serverDescription2 });
  71. subject.ClusterId.Should().Be(__clusterId);
  72. subject.ConnectionMode.Should().Be(ClusterConnectionMode.ReplicaSet);
  73. subject.DnsMonitorException.Should().BeSameAs(dnsMonitorException);
  74. subject.LogicalSessionTimeout.Should().NotHaveValue();
  75. subject.Servers.Should().ContainInOrder(new[] { __serverDescription1, __serverDescription2 });
  76. subject.State.Should().Be(ClusterState.Disconnected);
  77. subject.Type.Should().Be(ClusterType.ReplicaSet);
  78. }
  79. [Theory]
  80. [InlineData("ClusterId")]
  81. [InlineData("ConnectionMode")]
  82. [InlineData("DnsMonitorException")]
  83. [InlineData("Servers")]
  84. [InlineData("Type")]
  85. public void Equals_should_return_false_if_any_field_is_not_equal(string notEqualField)
  86. {
  87. var subject1 = CreateSubject();
  88. var subject2 = CreateSubject(notEqualField);
  89. subject1.Equals(subject2).Should().BeFalse();
  90. subject1.Equals((object)subject2).Should().BeFalse();
  91. subject1.GetHashCode().Should().NotBe(subject2.GetHashCode());
  92. }
  93. [Fact]
  94. public void Equals_should_return_true_if_all_fields_are_equal()
  95. {
  96. var subject1 = CreateSubject();
  97. var subject2 = CreateSubject();
  98. subject1.Equals(subject2).Should().BeTrue();
  99. subject1.Equals((object)subject2).Should().BeTrue();
  100. subject1.GetHashCode().Should().Be(subject2.GetHashCode());
  101. }
  102. [Theory]
  103. [InlineData(new int[0], true)]
  104. [InlineData(new int[] { 0 }, false)]
  105. [InlineData(new int[] { 1 }, true)]
  106. [InlineData(new int[] { 2 }, true)]
  107. [InlineData(new int[] { 0, 0 }, false)]
  108. [InlineData(new int[] { 0, 1 }, false)]
  109. [InlineData(new int[] { 0, 2 }, false)]
  110. [InlineData(new int[] { 1, 0 }, false)]
  111. [InlineData(new int[] { 1, 1 }, true)]
  112. [InlineData(new int[] { 1, 2 }, true)]
  113. [InlineData(new int[] { 2, 0 }, false)]
  114. [InlineData(new int[] { 2, 1 }, true)]
  115. [InlineData(new int[] { 2, 2 }, true)]
  116. public void IsCompatibleWithDriver_should_return_expected_result(int[] wireRanges, bool expectedResult)
  117. {
  118. var clusterId = new ClusterId(1);
  119. var connectionMode = ClusterConnectionMode.Automatic;
  120. var subject = ClusterDescription.CreateInitial(clusterId, connectionMode);
  121. for (var i = 0; i < wireRanges.Length; i++)
  122. {
  123. var endPoint = new DnsEndPoint("localhost", i);
  124. var serverId = new ServerId(clusterId, endPoint);
  125. var wireRange = wireRanges[i];
  126. var wireVersionRange = wireRange == 0 ? new Range<int>(0, 0) : wireRange == 1 ? new Range<int>(2, 6) : null;
  127. var server = new ServerDescription(serverId, endPoint, wireVersionRange: wireVersionRange, type: ServerType.Standalone);
  128. subject = subject.WithServerDescription(server);
  129. }
  130. var result = subject.IsCompatibleWithDriver;
  131. result.Should().Be(expectedResult);
  132. }
  133. [Theory]
  134. [InlineData(new int[0])]
  135. [InlineData(new int[] { 0 })]
  136. [InlineData(new int[] { 1 })]
  137. [InlineData(new int[] { 2 })]
  138. [InlineData(new int[] { 0, 0 })]
  139. [InlineData(new int[] { 0, 1 })]
  140. [InlineData(new int[] { 0, 2 })]
  141. [InlineData(new int[] { 1, 0 })]
  142. [InlineData(new int[] { 1, 1 })]
  143. [InlineData(new int[] { 1, 2 })]
  144. [InlineData(new int[] { 2, 0 })]
  145. [InlineData(new int[] { 2, 1 })]
  146. [InlineData(new int[] { 2, 2 })]
  147. public void IsCompatibleWithDriver_should_return_true_if_server_unknown(int[] wireRanges)
  148. {
  149. var clusterId = new ClusterId(1);
  150. var connectionMode = ClusterConnectionMode.Automatic;
  151. var subject = ClusterDescription.CreateInitial(clusterId, connectionMode);
  152. for (var i = 0; i < wireRanges.Length; i++)
  153. {
  154. var endPoint = new DnsEndPoint("localhost", i);
  155. var serverId = new ServerId(clusterId, endPoint);
  156. var wireRange = wireRanges[i];
  157. var wireVersionRange = wireRange == 0 ? new Range<int>(0, 0) : wireRange == 1 ? new Range<int>(2, 6) : null;
  158. var server = new ServerDescription(serverId, endPoint, wireVersionRange: wireVersionRange, type: ServerType.Unknown);
  159. subject = subject.WithServerDescription(server);
  160. }
  161. var result = subject.IsCompatibleWithDriver;
  162. result.Should().BeTrue();
  163. }
  164. [Fact]
  165. public void LogicalSessionTimeout_should_return_expected_result_with_0_servers()
  166. {
  167. var clusterId = new ClusterId(1);
  168. var connectionMode = ClusterConnectionMode.Automatic;
  169. var type = ClusterType.ReplicaSet;
  170. var servers = new ServerDescription[0];
  171. var subject = new ClusterDescription(clusterId, connectionMode, type, servers);
  172. var result = subject.LogicalSessionTimeout;
  173. result.Should().NotHaveValue();
  174. }
  175. [Theory]
  176. [InlineData(null, null)]
  177. [InlineData(1, 1)]
  178. public void LogicalSessionTimeout_should_return_expected_result_with_1_server(int? timeout1, int? expectedResultMinutes)
  179. {
  180. var clusterId = new ClusterId(1);
  181. var connectionMode = ClusterConnectionMode.Automatic;
  182. var type = ClusterType.ReplicaSet;
  183. var endPoint1 = new DnsEndPoint("localhost", 27017);
  184. var serverId1 = new ServerId(clusterId, endPoint1);
  185. var server1 = new ServerDescription(
  186. serverId1,
  187. endPoint1,
  188. state: ServerState.Connected,
  189. type: ServerType.ReplicaSetPrimary,
  190. logicalSessionTimeout: timeout1 == null ? (TimeSpan?)null : TimeSpan.FromMinutes(timeout1.Value));
  191. var servers = new[] { server1 };
  192. var subject = new ClusterDescription(clusterId, connectionMode, type, servers);
  193. var expectedResult = expectedResultMinutes == null ? (TimeSpan?)null : TimeSpan.FromMinutes(expectedResultMinutes.Value);
  194. var result = subject.LogicalSessionTimeout;
  195. result.Should().Be(expectedResult);
  196. }
  197. [Theory]
  198. [InlineData(null, null, null)]
  199. [InlineData(1, null, null)]
  200. [InlineData(null, 2, null)]
  201. [InlineData(1, 2, 1)]
  202. [InlineData(2, 1, 1)]
  203. public void LogicalSessionTimeout_should_return_expected_result_with_2_servers(int? timeout1, int? timeout2, int? expectedResultMinutes)
  204. {
  205. var clusterId = new ClusterId(1);
  206. var connectionMode = ClusterConnectionMode.Automatic;
  207. var type = ClusterType.ReplicaSet;
  208. var endPoint1 = new DnsEndPoint("localhost", 27017);
  209. var serverId1 = new ServerId(clusterId, endPoint1);
  210. var server1 = new ServerDescription(
  211. serverId1,
  212. endPoint1,
  213. state: ServerState.Connected,
  214. type: ServerType.ReplicaSetPrimary,
  215. logicalSessionTimeout: timeout1 == null ? (TimeSpan?)null : TimeSpan.FromMinutes(timeout1.Value));
  216. var endPoint2 = new DnsEndPoint("localhost", 27018);
  217. var serverId2 = new ServerId(clusterId, endPoint2);
  218. var server2 = new ServerDescription(
  219. serverId2,
  220. endPoint2,
  221. state: ServerState.Connected,
  222. type: ServerType.ReplicaSetSecondary,
  223. logicalSessionTimeout: timeout2 == null ? (TimeSpan?)null : TimeSpan.FromMinutes(timeout2.Value));
  224. var servers = new[] { server1, server2 };
  225. var subject = new ClusterDescription(clusterId, connectionMode, type, servers);
  226. var expectedResult = expectedResultMinutes == null ? (TimeSpan?)null : TimeSpan.FromMinutes(expectedResultMinutes.Value);
  227. var result = subject.LogicalSessionTimeout;
  228. result.Should().Be(expectedResult);
  229. }
  230. [Fact]
  231. public void State_should_be_connected_if_any_server_is_connected()
  232. {
  233. var connected = ServerDescriptionHelper.Connected(new ClusterId(1));
  234. var subject = new ClusterDescription(new ClusterId(1), ClusterConnectionMode.Standalone, ClusterType.Standalone, new[] { __serverDescription1, connected });
  235. subject.State.Should().Be(ClusterState.Connected);
  236. }
  237. [Fact]
  238. public void ToString_should_return_string_representation()
  239. {
  240. var subject = new ClusterDescription(new ClusterId(1), ClusterConnectionMode.Standalone, ClusterType.Standalone, new[] { __serverDescription1 });
  241. var expected = string.Format("{{ ClusterId : \"1\", ConnectionMode : \"Standalone\", Type : \"Standalone\", State : \"Disconnected\", Servers : [{0}] }}",
  242. __serverDescription1.ToString());
  243. subject.ToString().Should().Be(expected);
  244. }
  245. [Fact]
  246. public void ToString_should_return_string_representation_when_dnsMonitorException_is_not_null()
  247. {
  248. var dnsMonitorException = new Exception("DNS");
  249. var subject = new ClusterDescription(new ClusterId(1), ClusterConnectionMode.Standalone, dnsMonitorException, ClusterType.Standalone, new[] { __serverDescription1 });
  250. var expected = string.Format(
  251. "{{ ClusterId : \"1\", ConnectionMode : \"Standalone\", Type : \"Standalone\", State : \"Disconnected\", Servers : [{0}], DnsMonitorException : \"{1}\" }}",
  252. __serverDescription1,
  253. dnsMonitorException);
  254. subject.ToString().Should().Be(expected);
  255. }
  256. [Theory]
  257. [InlineData(false, false)]
  258. [InlineData(false, true)]
  259. [InlineData(true, false)]
  260. public void WithDnsMonitorException_should_return_expected_result(bool useNullValue1, bool useNullValue2)
  261. {
  262. var value1 = useNullValue1 ? null : new Exception();
  263. var value2 = useNullValue2 ? null : new Exception();
  264. var subject = CreateSubject(dnsMonitorException: value1);
  265. var result = subject.WithDnsMonitorException(value2);
  266. result.Should().NotBeSameAs(subject);
  267. result.DnsMonitorException.Should().Be(value2);
  268. }
  269. [Theory]
  270. [InlineData(false)]
  271. [InlineData(true)]
  272. public void WithDnsMonitorException_should_return_same_instance_when_value_did_not_change(bool useNullValue)
  273. {
  274. var value = useNullValue ? null : new Exception();
  275. var subject = CreateSubject(dnsMonitorException: value);
  276. var result = subject.WithDnsMonitorException(value);
  277. result.Should().BeSameAs(subject);
  278. }
  279. [Fact]
  280. public void WithServerDescription_should_add_server_if_server_does_not_exist()
  281. {
  282. var subject1 = CreateSubject();
  283. var newServerDescription = new ServerDescription(new ServerId(__clusterId, new DnsEndPoint("127.0.0.1", 27018)), new DnsEndPoint("127.0.0.1", 27018));
  284. var subject2 = subject1.WithServerDescription(newServerDescription);
  285. subject2.Should().NotBeSameAs(subject1);
  286. subject2.Should().NotBe(subject1);
  287. subject2.Servers.Count.Should().Be(3);
  288. }
  289. [Fact]
  290. public void WithServerDescription_should_return_new_instance_if_value_is_not_equal()
  291. {
  292. var subject1 = CreateSubject();
  293. var oldServerDescription = subject1.Servers[0];
  294. var newServerDescription = oldServerDescription.With(
  295. averageRoundTripTime: oldServerDescription.AverageRoundTripTime.Add(TimeSpan.FromSeconds(1)));
  296. var subject2 = subject1.WithServerDescription(newServerDescription);
  297. subject2.Should().NotBeSameAs(subject1);
  298. subject2.Should().NotBe(subject1);
  299. }
  300. [Fact]
  301. public void WithServerDescription_should_return_same_instance_if_value_is_equal()
  302. {
  303. var subject1 = CreateSubject();
  304. var subject2 = subject1.WithServerDescription(subject1.Servers[0]);
  305. subject2.Should().BeSameAs(subject1);
  306. }
  307. [Fact]
  308. public void WithoutServerDescription_should_remove_server_if_it_exists()
  309. {
  310. var subject1 = CreateSubject();
  311. var subject2 = subject1.WithoutServerDescription(__endPoint1);
  312. subject2.Should().NotBeSameAs(subject1);
  313. subject2.Servers.Count.Should().Be(1);
  314. }
  315. [Fact]
  316. public void WithType_should_return_new_instance_if_value_is_not_equal()
  317. {
  318. var subject1 = CreateSubject();
  319. var subject2 = subject1.WithType(ClusterType.Unknown);
  320. subject2.Should().NotBeSameAs(subject1);
  321. subject2.Should().NotBe(subject1);
  322. }
  323. [Fact]
  324. public void WithType_should_return_same_instance_if_value_is_equal()
  325. {
  326. var subject1 = CreateSubject();
  327. var subject2 = subject1.WithType(subject1.Type);
  328. subject2.Should().BeSameAs(subject1);
  329. }
  330. private ClusterDescription CreateSubject(Exception dnsMonitorException)
  331. {
  332. var clusterId = new ClusterId(1);
  333. var connectionMode = ClusterConnectionMode.ReplicaSet;
  334. var type = ClusterType.ReplicaSet;
  335. var servers = new[] { __serverDescription1, __serverDescription2 };
  336. return new ClusterDescription(clusterId, connectionMode, dnsMonitorException, type, servers);
  337. }
  338. private ClusterDescription CreateSubject(string notEqualField = null)
  339. {
  340. var clusterId = new ClusterId(1);
  341. var connectionMode = ClusterConnectionMode.ReplicaSet;
  342. Exception dnsMonitorException = null;
  343. var type = ClusterType.ReplicaSet;
  344. var servers = new[] { __serverDescription1, __serverDescription2 };
  345. if (notEqualField != null)
  346. {
  347. switch (notEqualField)
  348. {
  349. case "ClusterId": clusterId = new ClusterId(2); break;
  350. case "ConnectionMode": connectionMode = ClusterConnectionMode.Standalone; break;
  351. case "DnsMonitorException": dnsMonitorException = new Exception(); break;
  352. case "Type": type = ClusterType.Unknown; break;
  353. case "Servers": servers = new[] { __serverDescription1 }; break;
  354. default: throw new ArgumentException($"Unknown field name: \"{notEqualField}\".", nameof(notEqualField));
  355. }
  356. }
  357. return new ClusterDescription(clusterId, connectionMode, dnsMonitorException, type, servers);
  358. }
  359. }
  360. }