PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/Core/Servers/ServerDescriptionTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 386 lines | 345 code | 25 blank | 16 comment | 3 complexity | 9f241aeebe0614ddf9db022ff02589df 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.Bson;
  19. using MongoDB.Driver.Core.Clusters;
  20. using MongoDB.Driver.Core.Misc;
  21. using Xunit;
  22. namespace MongoDB.Driver.Core.Servers
  23. {
  24. public class ServerDescriptionTests
  25. {
  26. #region static
  27. // static fields
  28. private static readonly ClusterId __clusterId;
  29. private static readonly DnsEndPoint __endPoint;
  30. private static readonly ServerId __serverId;
  31. // static constructor
  32. static ServerDescriptionTests()
  33. {
  34. __clusterId = new ClusterId();
  35. __endPoint = new DnsEndPoint("localhost", 27017);
  36. __serverId = new ServerId(__clusterId, __endPoint);
  37. }
  38. #endregion
  39. [Fact]
  40. public void Constructor_with_serverId_and_endPoint_only_should_return_disconnected_instance()
  41. {
  42. var subject = new ServerDescription(__serverId, __endPoint);
  43. subject.AverageRoundTripTime.Should().Be(TimeSpan.Zero);
  44. subject.CanonicalEndPoint.Should().BeNull();
  45. subject.ElectionId.Should().BeNull();
  46. subject.EndPoint.Should().Be(__endPoint);
  47. subject.LogicalSessionTimeout.Should().NotHaveValue();
  48. subject.ReplicaSetConfig.Should().BeNull();
  49. subject.ServerId.Should().Be(__serverId);
  50. subject.State.Should().Be(ServerState.Disconnected);
  51. subject.Tags.Should().BeNull();
  52. subject.Type.Should().Be(ServerType.Unknown);
  53. subject.Version.Should().BeNull();
  54. subject.WireVersionRange.Should().BeNull();
  55. }
  56. [Fact]
  57. public void Constructor_with_multiple_parameters_should_return_properly_initialized_instance()
  58. {
  59. var averageRoundTripTime = TimeSpan.FromSeconds(1);
  60. var canonicalEndPoint = new DnsEndPoint("localhost", 27017);
  61. var electionId = new ElectionId(ObjectId.GenerateNewId());
  62. var logicalSessionTimeout = TimeSpan.FromMinutes(1);
  63. var replicaSetConfig = new ReplicaSetConfig(
  64. new[] { new DnsEndPoint("localhost", 27017), new DnsEndPoint("localhost", 27018) },
  65. "name",
  66. new DnsEndPoint("localhost", 27017),
  67. 1);
  68. var state = ServerState.Connected;
  69. var tags = new TagSet(new[] { new Tag("x", "a") });
  70. var type = ServerType.ReplicaSetPrimary;
  71. var version = new SemanticVersion(2, 6, 3);
  72. var wireVersionRange = new Range<int>(2, 3);
  73. var subject = new ServerDescription(
  74. __serverId,
  75. __endPoint,
  76. state: state,
  77. type: type,
  78. averageRoundTripTime: averageRoundTripTime,
  79. canonicalEndPoint: canonicalEndPoint,
  80. electionId: electionId,
  81. logicalSessionTimeout: logicalSessionTimeout,
  82. replicaSetConfig: replicaSetConfig,
  83. tags: tags,
  84. version: version,
  85. wireVersionRange: wireVersionRange);
  86. subject.AverageRoundTripTime.Should().Be(TimeSpan.FromSeconds(1));
  87. subject.CanonicalEndPoint.Should().Be(canonicalEndPoint);
  88. subject.ElectionId.Should().Be(electionId);
  89. subject.EndPoint.Should().Be(__endPoint);
  90. subject.LogicalSessionTimeout.Should().Be(logicalSessionTimeout);
  91. subject.ReplicaSetConfig.Should().Be(replicaSetConfig);
  92. subject.ServerId.Should().Be(__serverId);
  93. subject.State.Should().Be(state);
  94. subject.Tags.Should().Be(tags);
  95. subject.Type.Should().Be(type);
  96. }
  97. [Theory]
  98. [InlineData("AverageRoundTripTime")]
  99. [InlineData("CanonicalEndPoint")]
  100. [InlineData("ElectionId")]
  101. [InlineData("EndPoint")]
  102. [InlineData("LogicalSessionTimeout")]
  103. [InlineData("ReplicaSetConfig")]
  104. [InlineData("ServerId")]
  105. [InlineData("State")]
  106. [InlineData("Tags")]
  107. [InlineData("Type")]
  108. [InlineData("Version")]
  109. [InlineData("WireVersionRange")]
  110. public void Equals_should_return_false_when_any_field_is_not_equal(string notEqualField)
  111. {
  112. var averageRoundTripTime = TimeSpan.FromSeconds(1);
  113. var canonicalEndPoint = new DnsEndPoint("localhost", 27017);
  114. var electionId = new ElectionId(ObjectId.GenerateNewId());
  115. var endPoint = new DnsEndPoint("localhost", 27017);
  116. var logicalSessionTimeout = TimeSpan.FromMinutes(1);
  117. var replicaSetConfig = new ReplicaSetConfig(
  118. new[] { new DnsEndPoint("localhost", 27017), new DnsEndPoint("localhost", 27018) },
  119. "name",
  120. new DnsEndPoint("localhost", 27017),
  121. 1);
  122. var serverId = new ServerId(__clusterId, endPoint);
  123. var state = ServerState.Connected;
  124. var tags = new TagSet(new[] { new Tag("x", "a") });
  125. var type = ServerType.ReplicaSetPrimary;
  126. var version = new SemanticVersion(2, 6, 3);
  127. var wireVersionRange = new Range<int>(2, 3);
  128. var subject = new ServerDescription(
  129. serverId,
  130. endPoint,
  131. state: state,
  132. type: type,
  133. averageRoundTripTime: averageRoundTripTime,
  134. canonicalEndPoint: canonicalEndPoint,
  135. logicalSessionTimeout: logicalSessionTimeout,
  136. replicaSetConfig: replicaSetConfig,
  137. tags: tags,
  138. version: version,
  139. wireVersionRange: wireVersionRange);
  140. switch (notEqualField)
  141. {
  142. case "AverageRoundTripTime": averageRoundTripTime = averageRoundTripTime.Add(TimeSpan.FromSeconds(1)); break;
  143. case "CanonicalEndPoint": canonicalEndPoint = new DnsEndPoint("localhost", 27018); break;
  144. case "ElectionId": electionId = new ElectionId(ObjectId.Empty); break;
  145. case "EndPoint": endPoint = new DnsEndPoint(endPoint.Host, endPoint.Port + 1); serverId = new ServerId(__clusterId, endPoint); break;
  146. case "LogicalSessionTimeout": logicalSessionTimeout = TimeSpan.FromMinutes(2); break;
  147. case "ReplicaSetConfig": replicaSetConfig = new ReplicaSetConfig(replicaSetConfig.Members, "newname", replicaSetConfig.Primary, replicaSetConfig.Version); break;
  148. case "State": state = ServerState.Disconnected; break;
  149. case "ServerId": serverId = new ServerId(new ClusterId(), endPoint); break;
  150. case "Tags": tags = new TagSet(new[] { new Tag("x", "b") }); break;
  151. case "Type": type = ServerType.ReplicaSetSecondary; break;
  152. case "Version": version = new SemanticVersion(version.Major, version.Minor, version.Patch + 1); break;
  153. case "WireVersionRange": wireVersionRange = new Range<int>(0, 0); break;
  154. }
  155. var serverDescription2 = new ServerDescription(
  156. serverId,
  157. endPoint,
  158. state: state,
  159. type: type,
  160. averageRoundTripTime: averageRoundTripTime,
  161. canonicalEndPoint: canonicalEndPoint,
  162. electionId: electionId,
  163. logicalSessionTimeout: logicalSessionTimeout,
  164. replicaSetConfig: replicaSetConfig,
  165. tags: tags,
  166. version: version,
  167. wireVersionRange: wireVersionRange);
  168. subject.Equals(serverDescription2).Should().BeFalse();
  169. subject.Equals((object)serverDescription2).Should().BeFalse();
  170. subject.GetHashCode().Should().NotBe(serverDescription2.GetHashCode());
  171. }
  172. [Fact]
  173. public void Equals_should_return_true_when_all_fields_are_equal()
  174. {
  175. var lastUpdateTimestamp = DateTime.UtcNow;
  176. ServerDescription subject = new ServerDescription(
  177. __serverId,
  178. __endPoint,
  179. type: ServerType.Standalone,
  180. lastUpdateTimestamp: lastUpdateTimestamp);
  181. ServerDescription serverDescription2 = new ServerDescription(
  182. __serverId,
  183. __endPoint,
  184. type: ServerType.Standalone,
  185. lastUpdateTimestamp: lastUpdateTimestamp);
  186. subject.Equals(serverDescription2).Should().BeTrue();
  187. subject.Equals((object)serverDescription2).Should().BeTrue();
  188. subject.GetHashCode().Should().Be(serverDescription2.GetHashCode());
  189. }
  190. [Theory]
  191. [InlineData(null, true)]
  192. [InlineData(new[] { 0, 0 }, false)]
  193. [InlineData(new[] { 0, 1 }, false)]
  194. [InlineData(new[] { 0, 2 }, true)]
  195. [InlineData(new[] { 0, 6 }, true)]
  196. [InlineData(new[] { 0, 7 }, true)]
  197. [InlineData(new[] { 2, 2 }, true)]
  198. [InlineData(new[] { 2, 6 }, true)]
  199. [InlineData(new[] { 2, 7 }, true)]
  200. [InlineData(new[] { 6, 6 }, true)]
  201. [InlineData(new[] { 6, 7 }, true)]
  202. [InlineData(new[] { 7, 7 }, true)]
  203. [InlineData(new[] { 7, 8 }, true)]
  204. public void IsCompatibleWithDriver_should_return_expected_result(int[] minMaxWireVersions, bool expectedResult)
  205. {
  206. var clusterId = new ClusterId(1);
  207. var endPoint = new DnsEndPoint("localhost", 27017);
  208. var serverId = new ServerId(clusterId, endPoint);
  209. var wireVersionRange = minMaxWireVersions == null ? null : new Range<int>(minMaxWireVersions[0], minMaxWireVersions[1]);
  210. var subject = new ServerDescription(serverId, endPoint, wireVersionRange: wireVersionRange, type: ServerType.Standalone);
  211. var result = subject.IsCompatibleWithDriver;
  212. result.Should().Be(expectedResult);
  213. }
  214. [Theory]
  215. [InlineData("AverageRoundTripTime")]
  216. [InlineData("CanonicalEndPoint")]
  217. [InlineData("ElectionId")]
  218. [InlineData("HeartbeatException")]
  219. [InlineData("HeartbeatInterval")]
  220. [InlineData("LastUpdateTimestamp")]
  221. [InlineData("LastWriteTimestamp")]
  222. [InlineData("LogicalSessionTimeout")]
  223. [InlineData("MaxBatchCount")]
  224. [InlineData("MaxDocumentSize")]
  225. [InlineData("MaxMessageSize")]
  226. [InlineData("MaxWireDocumentSize")]
  227. [InlineData("ReplicaSetConfig")]
  228. [InlineData("State")]
  229. [InlineData("Tags")]
  230. [InlineData("Type")]
  231. [InlineData("Version")]
  232. [InlineData("WireVersionRange")]
  233. public void With_should_return_new_instance_when_a_field_is_not_equal(string notEqualField)
  234. {
  235. var averageRoundTripTime = TimeSpan.FromSeconds(1);
  236. var canonicalEndPoint = new DnsEndPoint("localhost", 27017);
  237. var electionId = new ElectionId(ObjectId.GenerateNewId());
  238. var heartbeatException = new Exception();
  239. var heartbeatInterval = TimeSpan.FromSeconds(10);
  240. var lastUpdateTimestamp = DateTime.UtcNow;
  241. var lastWriteTimestamp = DateTime.UtcNow;
  242. var logicalSessionTimeout = TimeSpan.FromMinutes(1);
  243. var maxBatchCount = 1000;
  244. var maxDocumentSize = 16000000;
  245. var maxMessageSize = 48000000;
  246. var maxWireDocumentSize = 16000000;
  247. var replicaSetConfig = new ReplicaSetConfig(
  248. new[] { new DnsEndPoint("localhost", 27017), new DnsEndPoint("localhost", 27018) },
  249. "name",
  250. new DnsEndPoint("localhost", 27017),
  251. 1);
  252. var state = ServerState.Connected;
  253. var tags = new TagSet(new[] { new Tag("x", "a") });
  254. var type = ServerType.ReplicaSetPrimary;
  255. var version = new SemanticVersion(2, 6, 3);
  256. var wireVersionRange = new Range<int>(2, 3);
  257. var subject = new ServerDescription(
  258. __serverId,
  259. __endPoint,
  260. averageRoundTripTime: averageRoundTripTime,
  261. canonicalEndPoint: canonicalEndPoint,
  262. electionId: electionId,
  263. heartbeatException: heartbeatException,
  264. heartbeatInterval: heartbeatInterval,
  265. lastUpdateTimestamp: lastUpdateTimestamp,
  266. lastWriteTimestamp: lastWriteTimestamp,
  267. logicalSessionTimeout: logicalSessionTimeout,
  268. maxBatchCount: maxBatchCount,
  269. maxDocumentSize: maxDocumentSize,
  270. maxMessageSize: maxMessageSize,
  271. maxWireDocumentSize: maxWireDocumentSize,
  272. replicaSetConfig: replicaSetConfig,
  273. state: state,
  274. tags: tags,
  275. type: type,
  276. version: version,
  277. wireVersionRange: wireVersionRange);
  278. switch (notEqualField)
  279. {
  280. case "AverageRoundTripTime": averageRoundTripTime = averageRoundTripTime.Add(TimeSpan.FromSeconds(1)); break;
  281. case "CanonicalEndPoint": canonicalEndPoint = new DnsEndPoint("localhost", 27018); break;
  282. case "ElectionId": electionId = new ElectionId(ObjectId.Empty); break;
  283. case "HeartbeatException": heartbeatException = new Exception(); break;
  284. case "HeartbeatInterval": heartbeatInterval = TimeSpan.FromSeconds(11); break;
  285. case "LastUpdateTimestamp": lastUpdateTimestamp = lastUpdateTimestamp.Add(TimeSpan.FromSeconds(1)); break;
  286. case "LastWriteTimestamp": lastWriteTimestamp = lastWriteTimestamp.Add(TimeSpan.FromSeconds(1)); break;
  287. case "LogicalSessionTimeout": logicalSessionTimeout = TimeSpan.FromMinutes(2); break;
  288. case "MaxBatchCount": maxBatchCount += 1; break;
  289. case "MaxDocumentSize": maxDocumentSize += 1; break;
  290. case "MaxMessageSize": maxMessageSize += 1; break;
  291. case "MaxWireDocumentSize": maxWireDocumentSize += 1; break;
  292. case "ReplicaSetConfig": replicaSetConfig = new ReplicaSetConfig(replicaSetConfig.Members, "newname", replicaSetConfig.Primary, replicaSetConfig.Version); break;
  293. case "State": state = ServerState.Disconnected; break;
  294. case "Tags": tags = new TagSet(new[] { new Tag("x", "b") }); break;
  295. case "Type": type = ServerType.ReplicaSetSecondary; break;
  296. case "Version": version = new SemanticVersion(version.Major, version.Minor, version.Patch + 1); break;
  297. case "WireVersionRange": wireVersionRange = new Range<int>(0, 0); break;
  298. }
  299. var result = subject.With(
  300. averageRoundTripTime: averageRoundTripTime,
  301. canonicalEndPoint: canonicalEndPoint,
  302. electionId: electionId,
  303. heartbeatException: heartbeatException,
  304. heartbeatInterval: heartbeatInterval,
  305. lastUpdateTimestamp: lastUpdateTimestamp,
  306. lastWriteTimestamp: lastWriteTimestamp,
  307. logicalSessionTimeout: logicalSessionTimeout,
  308. maxBatchCount: maxBatchCount,
  309. maxDocumentSize: maxDocumentSize,
  310. maxMessageSize: maxMessageSize,
  311. maxWireDocumentSize: maxWireDocumentSize,
  312. replicaSetConfig: replicaSetConfig,
  313. state: state,
  314. tags: tags,
  315. type: type,
  316. version: version,
  317. wireVersionRange: wireVersionRange);
  318. result.Should().NotBeSameAs(subject);
  319. result.Equals(subject).Should().BeFalse();
  320. result.Equals((object)subject).Should().BeFalse();
  321. result.GetHashCode().Should().NotBe(subject.GetHashCode());
  322. }
  323. [Fact]
  324. public void With_should_return_same_instance_when_all_fields_are_equal()
  325. {
  326. var averageRoundTripTime = TimeSpan.FromSeconds(1);
  327. var lastUpdateTimestamp = DateTime.UtcNow;
  328. var replicaSetConfig = new ReplicaSetConfig(
  329. new[] { new DnsEndPoint("localhost", 27017), new DnsEndPoint("localhost", 27018) },
  330. "name",
  331. new DnsEndPoint("localhost", 27017),
  332. 1);
  333. var state = ServerState.Connected;
  334. var tags = new TagSet(new[] { new Tag("x", "a") });
  335. var type = ServerType.ReplicaSetPrimary;
  336. var version = new SemanticVersion(2, 6, 3);
  337. var wireVersionRange = new Range<int>(0, 2);
  338. var subject = new ServerDescription(
  339. __serverId,
  340. __endPoint,
  341. averageRoundTripTime: averageRoundTripTime,
  342. lastUpdateTimestamp: lastUpdateTimestamp,
  343. replicaSetConfig: replicaSetConfig,
  344. state: state,
  345. tags: tags,
  346. type: type,
  347. version: version,
  348. wireVersionRange: wireVersionRange);
  349. var result = subject.With(
  350. averageRoundTripTime: averageRoundTripTime,
  351. lastUpdateTimestamp: lastUpdateTimestamp,
  352. replicaSetConfig: replicaSetConfig,
  353. state: ServerState.Connected,
  354. tags: tags,
  355. type: type,
  356. version: version,
  357. wireVersionRange: wireVersionRange);
  358. result.ShouldBeEquivalentTo(subject);
  359. }
  360. }
  361. }