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

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

http://github.com/mongodb/mongo-csharp-driver
C# | 184 lines | 138 code | 32 blank | 14 comment | 4 complexity | 376c4da2096e576148bde0192743d23d 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.Threading;
  17. using FluentAssertions;
  18. using MongoDB.Bson.TestHelpers.XunitExtensions;
  19. using MongoDB.Driver.Core.Bindings;
  20. using MongoDB.Driver.Core.Clusters;
  21. using Moq;
  22. using Xunit;
  23. namespace MongoDB.Driver.Core.Bindings
  24. {
  25. public class ChannelSourceReadWriteBindingTests
  26. {
  27. private Mock<IChannelSourceHandle> _mockChannelSource;
  28. public ChannelSourceReadWriteBindingTests()
  29. {
  30. _mockChannelSource = new Mock<IChannelSourceHandle>();
  31. }
  32. [Fact]
  33. public void Constructor_should_throw_if_channelSource_is_null()
  34. {
  35. Action act = () => new ChannelSourceReadWriteBinding(null, ReadPreference.Primary, NoCoreSession.NewHandle());
  36. act.ShouldThrow<ArgumentNullException>();
  37. }
  38. [Fact]
  39. public void Constructor_should_throw_if_readPreference_is_null()
  40. {
  41. Action act = () => new ChannelSourceReadWriteBinding(_mockChannelSource.Object, null, NoCoreSession.NewHandle());
  42. act.ShouldThrow<ArgumentNullException>();
  43. }
  44. [Fact]
  45. public void Constructor_should_throw_if_session_is_null()
  46. {
  47. Action act = () => new ChannelSourceReadWriteBinding(_mockChannelSource.Object, ReadPreference.Primary, null);
  48. act.ShouldThrow<ArgumentNullException>();
  49. }
  50. [Fact]
  51. public void Constructor_should_not_fork_channelSource()
  52. {
  53. new ChannelSourceReadWriteBinding(_mockChannelSource.Object, ReadPreference.Primary, NoCoreSession.NewHandle());
  54. _mockChannelSource.Verify(s => s.Fork(), Times.Never);
  55. }
  56. [Fact]
  57. public void Session_should_return_expected_result()
  58. {
  59. var session = new Mock<ICoreSessionHandle>().Object;
  60. var subject = new ChannelSourceReadWriteBinding(_mockChannelSource.Object, ReadPreference.Primary, session);
  61. var result = subject.Session;
  62. result.Should().BeSameAs(session);
  63. }
  64. [Theory]
  65. [ParameterAttributeData]
  66. public void GetReadChannelSourceAsync_should_throw_if_disposed(
  67. [Values(false, true)]
  68. bool async)
  69. {
  70. var subject = new ChannelSourceReadWriteBinding(_mockChannelSource.Object, ReadPreference.Primary, NoCoreSession.NewHandle());
  71. subject.Dispose();
  72. Action act;
  73. if (async)
  74. {
  75. act = () => subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
  76. }
  77. else
  78. {
  79. act = () => subject.GetReadChannelSource(CancellationToken.None);
  80. }
  81. act.ShouldThrow<ObjectDisposedException>();
  82. }
  83. [Theory]
  84. [ParameterAttributeData]
  85. public void GetReadChannelSource_should_fork_the_channelSource(
  86. [Values(false, true)]
  87. bool async)
  88. {
  89. var subject = new ChannelSourceReadWriteBinding(_mockChannelSource.Object, ReadPreference.Primary, NoCoreSession.NewHandle());
  90. if (async)
  91. {
  92. subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
  93. }
  94. else
  95. {
  96. subject.GetReadChannelSource(CancellationToken.None);
  97. }
  98. _mockChannelSource.Verify(f => f.Fork(), Times.Once);
  99. }
  100. [Theory]
  101. [ParameterAttributeData]
  102. public void GetWriteChannelSource_should_throw_if_disposed(
  103. [Values(false, true)]
  104. bool async)
  105. {
  106. var subject = new ChannelSourceReadWriteBinding(_mockChannelSource.Object, ReadPreference.Primary, NoCoreSession.NewHandle());
  107. subject.Dispose();
  108. Action act;
  109. if (async)
  110. {
  111. act = () => subject.GetWriteChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
  112. }
  113. else
  114. {
  115. act = () => subject.GetWriteChannelSource(CancellationToken.None);
  116. }
  117. act.ShouldThrow<ObjectDisposedException>();
  118. }
  119. [Theory]
  120. [ParameterAttributeData]
  121. public void GetWriteChannelSource_should_fork_the_channelSource(
  122. [Values(false, true)]
  123. bool async)
  124. {
  125. var subject = new ChannelSourceReadWriteBinding(_mockChannelSource.Object, ReadPreference.Primary, NoCoreSession.NewHandle());
  126. if (async)
  127. {
  128. subject.GetWriteChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
  129. }
  130. else
  131. {
  132. subject.GetWriteChannelSource(CancellationToken.None);
  133. }
  134. _mockChannelSource.Verify(f => f.Fork(), Times.Once);
  135. }
  136. [Fact]
  137. public void Dispose_should_call_dispose_on_connection_source()
  138. {
  139. var subject = new ChannelSourceReadWriteBinding(_mockChannelSource.Object, ReadPreference.Primary, NoCoreSession.NewHandle());
  140. subject.Dispose();
  141. _mockChannelSource.Verify(f => f.Dispose(), Times.Once);
  142. }
  143. [Fact]
  144. public void Dispose_should_call_dispose_on_session()
  145. {
  146. var mockSession = new Mock<ICoreSessionHandle>();
  147. var subject = new ChannelSourceReadWriteBinding(_mockChannelSource.Object, ReadPreference.Primary, mockSession.Object);
  148. subject.Dispose();
  149. mockSession.Verify(f => f.Dispose(), Times.Once);
  150. }
  151. }
  152. }