PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 152 lines | 104 code | 34 blank | 14 comment | 2 complexity | 1c6c10050cdb09299aab513f93e2cd26 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 Moq;
  20. using Xunit;
  21. namespace MongoDB.Driver.Core.Bindings
  22. {
  23. public class ReadBindingHandleTests
  24. {
  25. private Mock<IReadBinding> _mockReadBinding;
  26. public ReadBindingHandleTests()
  27. {
  28. _mockReadBinding = new Mock<IReadBinding>();
  29. }
  30. [Fact]
  31. public void Constructor_should_throw_if_readWriteBinding_is_null()
  32. {
  33. Action act = () => new ReadBindingHandle(null);
  34. act.ShouldThrow<ArgumentNullException>();
  35. }
  36. [Fact]
  37. public void Session_should_delegate_to_reference()
  38. {
  39. var subject = new ReadBindingHandle(_mockReadBinding.Object);
  40. var result = subject.Session;
  41. _mockReadBinding.Verify(m => m.Session, Times.Once);
  42. }
  43. [Theory]
  44. [ParameterAttributeData]
  45. public void GetReadChannelSource_should_throw_if_disposed(
  46. [Values(false, true)]
  47. bool async)
  48. {
  49. var subject = new ReadBindingHandle(_mockReadBinding.Object);
  50. subject.Dispose();
  51. Action act;
  52. if (async)
  53. {
  54. act = () => subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
  55. }
  56. else
  57. {
  58. act = () => subject.GetReadChannelSource(CancellationToken.None);
  59. }
  60. act.ShouldThrow<ObjectDisposedException>();
  61. }
  62. [Theory]
  63. [ParameterAttributeData]
  64. public void GetReadChannelSource_should_delegate_to_reference(
  65. [Values(false, true)]
  66. bool async)
  67. {
  68. var subject = new ReadBindingHandle(_mockReadBinding.Object);
  69. if (async)
  70. {
  71. subject.GetReadChannelSourceAsync(CancellationToken.None).GetAwaiter().GetResult();
  72. _mockReadBinding.Verify(b => b.GetReadChannelSourceAsync(CancellationToken.None), Times.Once);
  73. }
  74. else
  75. {
  76. subject.GetReadChannelSource(CancellationToken.None);
  77. _mockReadBinding.Verify(b => b.GetReadChannelSource(CancellationToken.None), Times.Once);
  78. }
  79. }
  80. [Fact]
  81. public void Fork_should_throw_if_disposed()
  82. {
  83. var subject = new ReadBindingHandle(_mockReadBinding.Object);
  84. subject.Dispose();
  85. Action act = () => subject.Fork();
  86. act.ShouldThrow<ObjectDisposedException>();
  87. }
  88. [Fact]
  89. public void Disposing_of_handle_after_fork_should_not_dispose_of_channelSource()
  90. {
  91. var subject = new ReadBindingHandle(_mockReadBinding.Object);
  92. var forked = subject.Fork();
  93. subject.Dispose();
  94. _mockReadBinding.Verify(b => b.Dispose(), Times.Never);
  95. forked.Dispose();
  96. _mockReadBinding.Verify(b => b.Dispose(), Times.Once);
  97. }
  98. [Fact]
  99. public void Disposing_of_fork_before_disposing_of_subject_hould_not_dispose_of_channelSource()
  100. {
  101. var subject = new ReadBindingHandle(_mockReadBinding.Object);
  102. var forked = subject.Fork();
  103. forked.Dispose();
  104. _mockReadBinding.Verify(b => b.Dispose(), Times.Never);
  105. subject.Dispose();
  106. _mockReadBinding.Verify(b => b.Dispose(), Times.Once);
  107. }
  108. [Fact]
  109. public void Disposing_of_last_handle_should_dispose_of_connectioSource()
  110. {
  111. var subject = new ReadBindingHandle(_mockReadBinding.Object);
  112. var forked = subject.Fork();
  113. subject.Dispose();
  114. forked.Dispose();
  115. _mockReadBinding.Verify(b => b.Dispose(), Times.Once);
  116. }
  117. }
  118. }