PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/Core/Bindings/WritableServerBindingTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 220 lines | 168 code | 38 blank | 14 comment | 4 complexity | 324825216ac17e7fa67d8f81adbb1e36 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.ComponentModel;
  17. using System.Net;
  18. using System.Reflection;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using FluentAssertions;
  22. using MongoDB.Bson.TestHelpers.XunitExtensions;
  23. using MongoDB.Driver.Core.Bindings;
  24. using MongoDB.Driver.Core.Clusters;
  25. using MongoDB.Driver.Core.Clusters.ServerSelectors;
  26. using MongoDB.Driver.Core.Servers;
  27. using Moq;
  28. using Xunit;
  29. namespace MongoDB.Driver.Core.Bindings
  30. {
  31. public class WritableServerBindingTests
  32. {
  33. private Mock<ICluster> _mockCluster;
  34. public WritableServerBindingTests()
  35. {
  36. _mockCluster = new Mock<ICluster>();
  37. }
  38. [Fact]
  39. public void Constructor_should_throw_if_cluster_is_null()
  40. {
  41. Action act = () => new WritableServerBinding(null, NoCoreSession.NewHandle());
  42. act.ShouldThrow<ArgumentNullException>();
  43. }
  44. [Fact]
  45. public void Constructor_should_throw_if_session_is_null()
  46. {
  47. var cluster = new Mock<ICluster>().Object;
  48. Action act = () => new WritableServerBinding(cluster, null);
  49. act.ShouldThrow<ArgumentNullException>();
  50. }
  51. [Fact]
  52. public void ReadPreference_should_be_primary()
  53. {
  54. var subject = new WritableServerBinding(_mockCluster.Object, NoCoreSession.NewHandle());
  55. subject.ReadPreference.Should().Be(ReadPreference.Primary);
  56. }
  57. [Fact]
  58. public void Session_should_return_expected_result()
  59. {
  60. var cluster = new Mock<ICluster>().Object;
  61. var session = new Mock<ICoreSessionHandle>().Object;
  62. var subject = new WritableServerBinding(cluster, session);
  63. var result = subject.Session;
  64. result.Should().BeSameAs(session);
  65. }
  66. [Theory]
  67. [ParameterAttributeData]
  68. public void GetReadChannelSource_should_throw_if_disposed(
  69. [Values(false, true)]
  70. bool async)
  71. {
  72. var subject = new WritableServerBinding(_mockCluster.Object, NoCoreSession.NewHandle());
  73. subject.Dispose();
  74. Action act;
  75. if (async)
  76. {
  77. act = () => subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
  78. }
  79. else
  80. {
  81. act = () => subject.GetReadChannelSource(CancellationToken.None);
  82. }
  83. act.ShouldThrow<ObjectDisposedException>();
  84. }
  85. [Theory]
  86. [ParameterAttributeData]
  87. public void GetReadChannelSource_should_use_a_writable_server_selector_to_select_the_server_from_the_cluster(
  88. [Values(false, true)]
  89. bool async)
  90. {
  91. var subject = new WritableServerBinding(_mockCluster.Object, NoCoreSession.NewHandle());
  92. var selectedServer = new Mock<IServer>().Object;
  93. var clusterId = new ClusterId();
  94. var endPoint = new DnsEndPoint("localhost", 27017);
  95. var initialClusterDescription = new ClusterDescription(
  96. clusterId,
  97. ClusterConnectionMode.Automatic,
  98. ClusterType.Unknown,
  99. new[] { new ServerDescription(new ServerId(clusterId, endPoint), endPoint) });
  100. var finalClusterDescription = initialClusterDescription.WithType(ClusterType.Standalone);
  101. _mockCluster.SetupSequence(c => c.Description).Returns(initialClusterDescription).Returns(finalClusterDescription);
  102. if (async)
  103. {
  104. _mockCluster.Setup(c => c.SelectServerAsync(It.IsAny<WritableServerSelector>(), CancellationToken.None)).Returns(Task.FromResult(selectedServer));
  105. subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
  106. _mockCluster.Verify(c => c.SelectServerAsync(It.IsAny<WritableServerSelector>(), CancellationToken.None), Times.Once);
  107. }
  108. else
  109. {
  110. _mockCluster.Setup(c => c.SelectServer(It.IsAny<WritableServerSelector>(), CancellationToken.None)).Returns(selectedServer);
  111. subject.GetReadChannelSource(CancellationToken.None);
  112. _mockCluster.Verify(c => c.SelectServer(It.IsAny<WritableServerSelector>(), CancellationToken.None), Times.Once);
  113. }
  114. }
  115. [Theory]
  116. [ParameterAttributeData]
  117. public void GetWriteChannelSource_should_throw_if_disposed(
  118. [Values(false, true)]
  119. bool async)
  120. {
  121. var subject = new WritableServerBinding(_mockCluster.Object, NoCoreSession.NewHandle());
  122. subject.Dispose();
  123. Action act;
  124. if (async)
  125. {
  126. act = () => subject.GetWriteChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
  127. }
  128. else
  129. {
  130. act = () => subject.GetWriteChannelSource(CancellationToken.None);
  131. }
  132. act.ShouldThrow<ObjectDisposedException>();
  133. }
  134. [Theory]
  135. [ParameterAttributeData]
  136. public void GetWriteChannelSourceAsync_should_use_a_writable_server_selector_to_select_the_server_from_the_cluster(
  137. [Values(false, true)]
  138. bool async)
  139. {
  140. var subject = new WritableServerBinding(_mockCluster.Object, NoCoreSession.NewHandle());
  141. var selectedServer = new Mock<IServer>().Object;
  142. var clusterId = new ClusterId();
  143. var endPoint = new DnsEndPoint("localhost", 27017);
  144. var initialClusterDescription = new ClusterDescription(
  145. clusterId,
  146. ClusterConnectionMode.Automatic,
  147. ClusterType.Unknown,
  148. new[] { new ServerDescription(new ServerId(clusterId, endPoint), endPoint) });
  149. var finalClusterDescription = initialClusterDescription.WithType(ClusterType.Standalone);
  150. _mockCluster.SetupSequence(c => c.Description).Returns(initialClusterDescription).Returns(finalClusterDescription);
  151. if (async)
  152. {
  153. _mockCluster.Setup(c => c.SelectServerAsync(It.IsAny<WritableServerSelector>(), CancellationToken.None)).Returns(Task.FromResult(selectedServer));
  154. subject.GetWriteChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
  155. _mockCluster.Verify(c => c.SelectServerAsync(It.IsAny<WritableServerSelector>(), CancellationToken.None), Times.Once);
  156. }
  157. else
  158. {
  159. _mockCluster.Setup(c => c.SelectServer(It.IsAny<WritableServerSelector>(), CancellationToken.None)).Returns(selectedServer);
  160. subject.GetWriteChannelSource(CancellationToken.None);
  161. _mockCluster.Verify(c => c.SelectServer(It.IsAny<WritableServerSelector>(), CancellationToken.None), Times.Once);
  162. }
  163. }
  164. [Fact]
  165. public void Dispose_should_call_dispose_on_owned_resources()
  166. {
  167. var mockSession = new Mock<ICoreSessionHandle>();
  168. var subject = new WritableServerBinding(_mockCluster.Object, mockSession.Object);
  169. subject.Dispose();
  170. _mockCluster.Verify(c => c.Dispose(), Times.Never);
  171. mockSession.Verify(m => m.Dispose(), Times.Once);
  172. }
  173. }
  174. public static class WritableServerBindingReflector
  175. {
  176. public static ICluster _cluster(this WritableServerBinding obj)
  177. {
  178. var fieldInfo = typeof(WritableServerBinding).GetField("_cluster", BindingFlags.NonPublic | BindingFlags.Instance);
  179. return (ICluster)fieldInfo.GetValue(obj);
  180. }
  181. }
  182. }