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

/tests/MongoDB.Driver.Core.Tests/ReadPreferenceTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 420 lines | 335 code | 70 blank | 15 comment | 4 complexity | 16a677ace471ac7c7137dac973db7492 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.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using FluentAssertions;
  21. using MongoDB.Bson.TestHelpers.XunitExtensions;
  22. using MongoDB.Driver.Core.Tests;
  23. using Xunit;
  24. namespace MongoDB.Driver
  25. {
  26. public class ReadPreferenceTests
  27. {
  28. [Fact]
  29. public void constructor_should_throw_when_tagSets_is_not_empty_and_mode_is_primary()
  30. {
  31. var tagSets = new[] { new TagSet(new[] { new Tag("name", "value") }) };
  32. var exception = Record.Exception(() => new ReadPreference(ReadPreferenceMode.Primary, tagSets: tagSets));
  33. var argumentException = exception.Should().BeOfType<ArgumentException>().Subject;
  34. argumentException.ParamName.Should().Be("tagSets");
  35. }
  36. [Fact]
  37. public void constructor_should_throw_when_maxStaleness_is_invalid()
  38. {
  39. var exception = Record.Exception(() => new ReadPreference(ReadPreferenceMode.Secondary, maxStaleness: TimeSpan.FromSeconds(-1)));
  40. var argumentOutOfRangeException = exception.Should().BeOfType<ArgumentOutOfRangeException>().Subject;
  41. argumentOutOfRangeException.ParamName.Should().Be("maxStaleness");
  42. }
  43. [Fact]
  44. public void constructor_should_throw_when_maxStaleness_is_not_null_and_mode_is_primary()
  45. {
  46. var exception = Record.Exception(() => new ReadPreference(ReadPreferenceMode.Primary, maxStaleness: TimeSpan.FromSeconds(1)));
  47. var argumentException = exception.Should().BeOfType<ArgumentException>().Subject;
  48. argumentException.ParamName.Should().Be("maxStaleness");
  49. }
  50. [Fact]
  51. public void constructor_should_throw_when_hedge_is_not_null_and_mode_is_primary()
  52. {
  53. var hedge = new ReadPreferenceHedge(true);
  54. var exception = Record.Exception(() => new ReadPreference(ReadPreferenceMode.Primary, hedge: hedge));
  55. var argumentException = exception.Should().BeOfType<ArgumentException>().Subject;
  56. argumentException.ParamName.Should().Be("hedge");
  57. }
  58. [Fact]
  59. public void constructor_with_mode_should_initialize_instance()
  60. {
  61. var mode = ReadPreferenceMode.Secondary; // use a value that is not the default
  62. var result = new ReadPreference(mode);
  63. result.ReadPreferenceMode.Should().Be(mode);
  64. result.TagSets.Should().BeEmpty();
  65. result.MaxStaleness.Should().NotHaveValue();
  66. result.Hedge.Should().BeNull();
  67. }
  68. [Fact]
  69. public void constructor_with_tagSets_should_initialize_instance()
  70. {
  71. var mode = ReadPreferenceMode.Secondary; // can't use tagSets with mode Primary
  72. var tagSets = new[] { new TagSet(new[] { new Tag("name", "value") }) };
  73. var result = new ReadPreference(mode, tagSets: tagSets);
  74. result.ReadPreferenceMode.Should().Be(mode);
  75. result.TagSets.Should().NotBeSameAs(tagSets);
  76. result.TagSets.Should().Equal(tagSets);
  77. result.MaxStaleness.Should().NotHaveValue();
  78. result.Hedge.Should().BeNull();
  79. }
  80. [Fact]
  81. public void constructor_with_tagSets_should_initialize_instance_when_tagSets_is_null()
  82. {
  83. var result = new ReadPreference(ReadPreferenceMode.Secondary, tagSets: null);
  84. result.ReadPreferenceMode.Should().Be(ReadPreferenceMode.Secondary);
  85. result.TagSets.Should().BeEmpty();
  86. result.MaxStaleness.Should().NotHaveValue();
  87. result.Hedge.Should().BeNull();
  88. }
  89. [Fact]
  90. public void constructor_with_maxStaleness_should_initialize_instance()
  91. {
  92. var maxStaleness = TimeSpan.FromSeconds(123);
  93. var result = new ReadPreference(ReadPreferenceMode.Secondary, maxStaleness: maxStaleness);
  94. result.ReadPreferenceMode.Should().Be(ReadPreferenceMode.Secondary);
  95. result.TagSets.Should().BeEmpty();
  96. result.MaxStaleness.Should().Be(maxStaleness);
  97. result.Hedge.Should().BeNull();
  98. }
  99. [Theory]
  100. [ParameterAttributeData]
  101. public void constructor_with_hedge_should_initialize_instance(
  102. [Values(null, false, true)]
  103. bool? isEnabled)
  104. {
  105. var hedge = isEnabled.HasValue ? new ReadPreferenceHedge(isEnabled.Value) : null;
  106. var result = new ReadPreference(ReadPreferenceMode.Secondary, hedge: hedge);
  107. result.ReadPreferenceMode.Should().Be(ReadPreferenceMode.Secondary);
  108. result.TagSets.Should().BeEmpty();
  109. result.MaxStaleness.Should().NotHaveValue();
  110. result.Hedge.Should().BeSameAs(hedge);
  111. }
  112. [Fact]
  113. public void constructor_with_maxStaleness_should_initialize_instance_when_value_is_null()
  114. {
  115. var result = new ReadPreference(ReadPreferenceMode.Secondary, maxStaleness: null);
  116. result.ReadPreferenceMode.Should().Be(ReadPreferenceMode.Secondary);
  117. result.TagSets.Should().BeEmpty();
  118. result.MaxStaleness.Should().NotHaveValue();
  119. result.Hedge.Should().BeNull();
  120. }
  121. [Theory]
  122. [ParameterAttributeData]
  123. public void Equals_should_compare_maxStaleness_fields(
  124. [Values(null, 1, 2)]
  125. int? lhsSeconds,
  126. [Values(null, 1, 2)]
  127. int? rhsSeconds)
  128. {
  129. var lhsMaxStaleness = lhsSeconds.HasValue ? TimeSpan.FromSeconds(lhsSeconds.Value) : (TimeSpan?)null;
  130. var rhsMaxStaleness = rhsSeconds.HasValue ? TimeSpan.FromSeconds(rhsSeconds.Value) : (TimeSpan?)null;
  131. var lhs = new ReadPreference(ReadPreferenceMode.Secondary, maxStaleness: lhsMaxStaleness);
  132. var rhs = new ReadPreference(ReadPreferenceMode.Secondary, maxStaleness: rhsMaxStaleness);
  133. Equals_Act_and_Assert(lhs, rhs, lhsSeconds.Equals(rhsSeconds));
  134. }
  135. [Theory]
  136. [InlineData(null, null, true)]
  137. [InlineData(null, true, false)]
  138. [InlineData(true, null, false)]
  139. [InlineData(false, false, true)]
  140. [InlineData(false, true, false)]
  141. [InlineData(true, false, false)]
  142. [InlineData(true, true, true)]
  143. public void Equals_should_compare_hedge_fields(bool? lhsEnabled, bool? rhsEnabled, bool expectedResult)
  144. {
  145. var lhsHedge = lhsEnabled.HasValue ? new ReadPreferenceHedge(lhsEnabled.Value) : null;
  146. var rhsHedge = rhsEnabled.HasValue ? new ReadPreferenceHedge(rhsEnabled.Value) : null;
  147. var lhs = new ReadPreference(ReadPreferenceMode.Secondary, hedge: lhsHedge);
  148. var rhs = new ReadPreference(ReadPreferenceMode.Secondary, hedge: rhsHedge);
  149. Equals_Act_and_Assert(lhs, rhs, expectedResult);
  150. }
  151. [Theory]
  152. [ParameterAttributeData]
  153. public void Equals_should_compare_mode_fields(
  154. [Values(ReadPreferenceMode.Primary, ReadPreferenceMode.Secondary)]
  155. ReadPreferenceMode lhsMode,
  156. [Values(ReadPreferenceMode.Primary, ReadPreferenceMode.Secondary)]
  157. ReadPreferenceMode rhsMode)
  158. {
  159. var lhs = new ReadPreference(lhsMode);
  160. var rhs = new ReadPreference(rhsMode);
  161. Equals_Act_and_Assert(lhs, rhs, lhsMode.Equals(rhsMode));
  162. }
  163. [Theory]
  164. [ParameterAttributeData]
  165. public void Equals_should_compare_tagSets_fields(
  166. [Values(null, "a", "b")]
  167. string lhsTagValue,
  168. [Values(null, "a", "b")]
  169. string rhsTagValue)
  170. {
  171. var lhsTagSets = lhsTagValue == null ? null : new[] { new TagSet(new[] { new Tag("x", lhsTagValue) }) };
  172. var rhsTagSets = rhsTagValue == null ? null : new[] { new TagSet(new[] { new Tag("x", rhsTagValue) }) };
  173. var lhs = new ReadPreference(ReadPreferenceMode.Secondary, tagSets: lhsTagSets);
  174. var rhs = new ReadPreference(ReadPreferenceMode.Secondary, tagSets: rhsTagSets);
  175. Equals_Act_and_Assert(lhs, rhs, object.Equals(lhsTagValue, rhsTagValue));
  176. }
  177. [Fact]
  178. public void Nearest_should_return_expected_result()
  179. {
  180. var result = ReadPreference.Nearest;
  181. result.ReadPreferenceMode.Should().Be(ReadPreferenceMode.Nearest);
  182. result.TagSets.Count.Should().Be(0);
  183. result.MaxStaleness.Should().NotHaveValue();
  184. result.Hedge.Should().BeNull();
  185. }
  186. [Fact]
  187. public void Primary_should_return_expected_result()
  188. {
  189. var result = ReadPreference.Primary;
  190. result.ReadPreferenceMode.Should().Be(ReadPreferenceMode.Primary);
  191. result.TagSets.Count.Should().Be(0);
  192. result.MaxStaleness.Should().NotHaveValue();
  193. result.Hedge.Should().BeNull();
  194. }
  195. [Fact]
  196. public void PrimaryPreferred_should_return_expected_result()
  197. {
  198. var result = ReadPreference.PrimaryPreferred;
  199. result.ReadPreferenceMode.Should().Be(ReadPreferenceMode.PrimaryPreferred);
  200. result.TagSets.Count.Should().Be(0);
  201. result.MaxStaleness.Should().NotHaveValue();
  202. result.Hedge.Should().BeNull();
  203. }
  204. [Fact]
  205. public void Secondary_should_return_expected_result()
  206. {
  207. var result = ReadPreference.Secondary;
  208. result.ReadPreferenceMode.Should().Be(ReadPreferenceMode.Secondary);
  209. result.TagSets.Count.Should().Be(0);
  210. result.MaxStaleness.Should().NotHaveValue();
  211. result.Hedge.Should().BeNull();
  212. }
  213. [Fact]
  214. public void SecondaryPreferred_should_return_expected_result()
  215. {
  216. var result = ReadPreference.SecondaryPreferred;
  217. result.ReadPreferenceMode.Should().Be(ReadPreferenceMode.SecondaryPreferred);
  218. result.TagSets.Count.Should().Be(0);
  219. result.MaxStaleness.Should().NotHaveValue();
  220. result.Hedge.Should().BeNull();
  221. }
  222. [Theory]
  223. [ParameterAttributeData]
  224. public void ToString_should_return_expected_result(
  225. [Values(ReadPreferenceMode.Primary, ReadPreferenceMode.Secondary)]
  226. ReadPreferenceMode mode)
  227. {
  228. var subject = new ReadPreference(mode);
  229. var result = subject.ToString();
  230. result.Should().Be($"{{ Mode : {mode} }}");
  231. }
  232. [Theory]
  233. [InlineData(new[] { 1 }, "[{ x : a }]")]
  234. [InlineData(new[] { 2 }, "[{ x : a, y : b }]")]
  235. [InlineData(new[] { 1, 1 }, "[{ x : a }, { x : a }]")]
  236. [InlineData(new[] { 2, 1 }, "[{ x : a, y : b }, { x : a }]")]
  237. [InlineData(new[] { 1, 2 }, "[{ x : a }, { x : a, y : b }]")]
  238. public void ToString_should_return_expected_result_when_tagSets_is_set(int[] tagSetSizes, string expectedTagSetsString)
  239. {
  240. var tagSets = new List<TagSet>();
  241. foreach (var size in tagSetSizes)
  242. {
  243. var tags = new List<Tag>();
  244. for (var i = 0; i < size; i++)
  245. {
  246. var name = new string((char)('x' + i), 1);
  247. var value = new string((char)('a' + i), 1);
  248. var tag = new Tag(name, value);
  249. tags.Add(tag);
  250. }
  251. var tagSet = new TagSet(tags);
  252. tagSets.Add(tagSet);
  253. }
  254. var subject = new ReadPreference(ReadPreferenceMode.Secondary, tagSets: tagSets);
  255. var result = subject.ToString();
  256. result.Should().Be($"{{ Mode : Secondary, TagSets : {expectedTagSetsString} }}");
  257. }
  258. [Theory]
  259. [ParameterAttributeData]
  260. public void ToString_should_return_expected_result_when_maxStaleness_is_set(
  261. [Values(1, 2)]
  262. int seconds)
  263. {
  264. var maxStaleness = TimeSpan.FromSeconds(seconds);
  265. var subject = new ReadPreference(ReadPreferenceMode.Secondary, maxStaleness: maxStaleness);
  266. var result = subject.ToString();
  267. result.Should().Be($"{{ Mode : Secondary, MaxStaleness : {seconds}s }}");
  268. }
  269. [Theory]
  270. [InlineData(null, "{ Mode : Secondary }")]
  271. [InlineData(false, "{ Mode : Secondary, Hedge : { \"enabled\" : false } }")]
  272. [InlineData(true, "{ Mode : Secondary, Hedge : { \"enabled\" : true } }")]
  273. public void ToString_should_return_expected_result_when_hedge_is_set(bool? isEnabled, string expectedResult)
  274. {
  275. var hedge = isEnabled.HasValue ? new ReadPreferenceHedge(isEnabled.Value) : null;
  276. var subject = new ReadPreference(ReadPreferenceMode.Secondary, hedge: hedge);
  277. var result = subject.ToString();
  278. result.Should().Be(expectedResult);
  279. }
  280. [Theory]
  281. [ParameterAttributeData]
  282. public void With_hedge_should_return_expected_result(
  283. [Values(false, true)]
  284. bool originalIsEnabled,
  285. [Values(false, true)]
  286. bool isEnabled)
  287. {
  288. var originalHedge = new ReadPreferenceHedge(isEnabled: originalIsEnabled);
  289. var hedge = new ReadPreferenceHedge(isEnabled: isEnabled);
  290. var subject = new ReadPreference(ReadPreferenceMode.Secondary, hedge: originalHedge);
  291. var result = subject.With(hedge);
  292. result.Hedge.Should().Be(hedge);
  293. result.With(originalHedge).Should().Be(subject);
  294. }
  295. [Theory]
  296. [ParameterAttributeData]
  297. public void With_maxStaleness_should_return_expected_result(
  298. [Values(1, 2)]
  299. int originalSeconds,
  300. [Values(1, 2)]
  301. int seconds)
  302. {
  303. var originalMaxStaleness = TimeSpan.FromSeconds(originalSeconds);
  304. var maxStaleness = TimeSpan.FromSeconds(seconds);
  305. var subject = new ReadPreference(ReadPreferenceMode.Secondary, maxStaleness: originalMaxStaleness);
  306. var result = subject.With(maxStaleness);
  307. result.MaxStaleness.Should().Be(maxStaleness);
  308. result.With(originalMaxStaleness).Should().Be(subject);
  309. }
  310. [Theory]
  311. [ParameterAttributeData]
  312. public void With_mode_should_return_expected_result(
  313. [Values(ReadPreferenceMode.Primary, ReadPreferenceMode.Secondary)]
  314. ReadPreferenceMode originalMode,
  315. [Values(ReadPreferenceMode.Primary, ReadPreferenceMode.Secondary)]
  316. ReadPreferenceMode mode)
  317. {
  318. var subject = new ReadPreference(originalMode);
  319. var result = subject.With(mode);
  320. result.ReadPreferenceMode.Should().Be(mode);
  321. result.With(originalMode).Should().Be(subject);
  322. }
  323. [Theory]
  324. [ParameterAttributeData]
  325. public void With_tagSets_should_return_expected_result(
  326. [Values("a", "b")]
  327. string originalTagValue,
  328. [Values("a", "b")]
  329. string tagValue)
  330. {
  331. var originalTagSets = new[] { new TagSet(new[] { new Tag("x", originalTagValue) }) };
  332. var tagSets = new[] { new TagSet(new[] { new Tag("x", tagValue) }) };
  333. var subject = new ReadPreference(ReadPreferenceMode.Secondary, tagSets: originalTagSets);
  334. var result = subject.With(tagSets);
  335. result.TagSets.Should().Equal(tagSets);
  336. result.With(originalTagSets).Should().Be(subject);
  337. }
  338. // private methods
  339. private void Equals_Act_and_Assert(ReadPreference lhs, ReadPreference rhs, bool expectedResult)
  340. {
  341. var result1 = lhs.Equals(rhs);
  342. var result2 = lhs.Equals((object)rhs);
  343. var hashCode1 = lhs.GetHashCode();
  344. var hashCode2 = rhs.GetHashCode();
  345. result1.Should().Be(expectedResult);
  346. result2.Should().Be(expectedResult);
  347. (hashCode1 == hashCode2).Should().Be(expectedResult);
  348. }
  349. }
  350. }