PageRenderTime 619ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 197 lines | 142 code | 41 blank | 14 comment | 4 complexity | 72c7b57a4848e15f0c5dceb403c91a0d 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 Moq;
  21. using Xunit;
  22. namespace MongoDB.Driver.Core.Bindings
  23. {
  24. public class ReadWriteBindingHandleTests
  25. {
  26. private Mock<IReadWriteBinding> _mockReadWriteBinding;
  27. public ReadWriteBindingHandleTests()
  28. {
  29. _mockReadWriteBinding = new Mock<IReadWriteBinding>();
  30. }
  31. [Fact]
  32. public void Constructor_should_throw_if_readWriteBinding_is_null()
  33. {
  34. Action act = () => new ReadWriteBindingHandle(null);
  35. act.ShouldThrow<ArgumentNullException>();
  36. }
  37. [Fact]
  38. public void Session_should_delegate_to_reference()
  39. {
  40. var subject = new ReadWriteBindingHandle(_mockReadWriteBinding.Object);
  41. var result = subject.Session;
  42. _mockReadWriteBinding.Verify(m => m.Session, Times.Once);
  43. }
  44. [Theory]
  45. [ParameterAttributeData]
  46. public void GetReadChannelSource_should_throw_if_disposed(
  47. [Values(false, true)]
  48. bool async)
  49. {
  50. var subject = new ReadWriteBindingHandle(_mockReadWriteBinding.Object);
  51. subject.Dispose();
  52. Action act;
  53. if (async)
  54. {
  55. act = () => subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
  56. }
  57. else
  58. {
  59. act = () => subject.GetReadChannelSource(CancellationToken.None);
  60. }
  61. act.ShouldThrow<ObjectDisposedException>();
  62. }
  63. [Theory]
  64. [ParameterAttributeData]
  65. public void GetReadChannelSource_should_delegate_to_reference(
  66. [Values(false, true)]
  67. bool async)
  68. {
  69. var subject = new ReadWriteBindingHandle(_mockReadWriteBinding.Object);
  70. if (async)
  71. {
  72. subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
  73. _mockReadWriteBinding.Verify(b => b.GetReadChannelSourceAsync(CancellationToken.None), Times.Once);
  74. }
  75. else
  76. {
  77. subject.GetReadChannelSource(CancellationToken.None);
  78. _mockReadWriteBinding.Verify(b => b.GetReadChannelSource(CancellationToken.None), Times.Once);
  79. }
  80. }
  81. [Theory]
  82. [ParameterAttributeData]
  83. public void GetWriteChannelSource_should_throw_if_disposed(
  84. [Values(false, true)]
  85. bool async)
  86. {
  87. var subject = new ReadWriteBindingHandle(_mockReadWriteBinding.Object);
  88. subject.Dispose();
  89. Action act;
  90. if (async)
  91. {
  92. act = () => subject.GetWriteChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
  93. }
  94. else
  95. {
  96. act = () => subject.GetWriteChannelSource(CancellationToken.None);
  97. }
  98. act.ShouldThrow<ObjectDisposedException>();
  99. }
  100. [Theory]
  101. [ParameterAttributeData]
  102. public void GetWriteChannelSource_should_delegate_to_reference(
  103. [Values(false, true)]
  104. bool async)
  105. {
  106. var subject = new ReadWriteBindingHandle(_mockReadWriteBinding.Object);
  107. if (async)
  108. {
  109. subject.GetWriteChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
  110. _mockReadWriteBinding.Verify(b => b.GetWriteChannelSourceAsync(CancellationToken.None), Times.Once);
  111. }
  112. else
  113. {
  114. subject.GetWriteChannelSource(CancellationToken.None);
  115. _mockReadWriteBinding.Verify(b => b.GetWriteChannelSource(CancellationToken.None), Times.Once);
  116. }
  117. }
  118. [Fact]
  119. public void Fork_should_throw_if_disposed()
  120. {
  121. var subject = new ReadWriteBindingHandle(_mockReadWriteBinding.Object);
  122. subject.Dispose();
  123. Action act = () => subject.Fork();
  124. act.ShouldThrow<ObjectDisposedException>();
  125. }
  126. [Fact]
  127. public void Disposing_of_handle_after_fork_should_not_dispose_of_channelSource()
  128. {
  129. var subject = new ReadWriteBindingHandle(_mockReadWriteBinding.Object);
  130. var forked = subject.Fork();
  131. subject.Dispose();
  132. _mockReadWriteBinding.Verify(b => b.Dispose(), Times.Never);
  133. forked.Dispose();
  134. _mockReadWriteBinding.Verify(b => b.Dispose(), Times.Once);
  135. }
  136. [Fact]
  137. public void Disposing_of_fork_before_disposing_of_subject_hould_not_dispose_of_channelSource()
  138. {
  139. var subject = new ReadWriteBindingHandle(_mockReadWriteBinding.Object);
  140. var forked = subject.Fork();
  141. forked.Dispose();
  142. _mockReadWriteBinding.Verify(b => b.Dispose(), Times.Never);
  143. subject.Dispose();
  144. _mockReadWriteBinding.Verify(b => b.Dispose(), Times.Once);
  145. }
  146. [Fact]
  147. public void Disposing_of_last_handle_should_dispose_of_connectioSource()
  148. {
  149. var subject = new ReadWriteBindingHandle(_mockReadWriteBinding.Object);
  150. var forked = subject.Fork();
  151. subject.Dispose();
  152. forked.Dispose();
  153. _mockReadWriteBinding.Verify(b => b.Dispose(), Times.Once);
  154. }
  155. }
  156. }