PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 163 lines | 113 code | 35 blank | 15 comment | 2 complexity | cb4f873e3b94f57452fbe0fb6885a251 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.Reflection;
  17. using System.Threading;
  18. using FluentAssertions;
  19. using MongoDB.Bson.TestHelpers;
  20. using MongoDB.Bson.TestHelpers.XunitExtensions;
  21. using MongoDB.Driver.Core.Bindings;
  22. using MongoDB.Driver.Core.Misc;
  23. using Moq;
  24. using Xunit;
  25. namespace MongoDB.Driver.Core.Bindings
  26. {
  27. public class ChannelSourceHandleTests
  28. {
  29. private Mock<IChannelSource> _mockChannelSource;
  30. public ChannelSourceHandleTests()
  31. {
  32. _mockChannelSource = new Mock<IChannelSource>();
  33. }
  34. [Fact]
  35. public void Constructor_should_throw_if_channelSource_is_null()
  36. {
  37. Action act = () => new ChannelSourceHandle(null);
  38. act.ShouldThrow<ArgumentNullException>();
  39. }
  40. [Fact]
  41. public void Session_should_delegate_to_reference()
  42. {
  43. var subject = new ChannelSourceHandle(_mockChannelSource.Object);
  44. var result = subject.Session;
  45. _mockChannelSource.Verify(m => m.Session, Times.Once);
  46. }
  47. [Theory]
  48. [ParameterAttributeData]
  49. public void GetChannel_should_throw_if_disposed(
  50. [Values(false, true)]
  51. bool async)
  52. {
  53. var subject = new ChannelSourceHandle(_mockChannelSource.Object);
  54. subject.Dispose();
  55. Action act;
  56. if (async)
  57. {
  58. act = () => subject.GetChannelAsync(CancellationToken.None).GetAwaiter().GetResult();
  59. }
  60. else
  61. {
  62. act = () => subject.GetChannel(CancellationToken.None);
  63. }
  64. act.ShouldThrow<ObjectDisposedException>();
  65. }
  66. [Theory]
  67. [ParameterAttributeData]
  68. public void GetChannel_should_delegate_to_reference(
  69. [Values(false, true)]
  70. bool async)
  71. {
  72. var subject = new ChannelSourceHandle(_mockChannelSource.Object);
  73. if (async)
  74. {
  75. subject.GetChannelAsync(CancellationToken.None).GetAwaiter().GetResult();
  76. _mockChannelSource.Verify(s => s.GetChannelAsync(CancellationToken.None), Times.Once);
  77. }
  78. else
  79. {
  80. subject.GetChannel(CancellationToken.None);
  81. _mockChannelSource.Verify(s => s.GetChannel(CancellationToken.None), Times.Once);
  82. }
  83. }
  84. [Fact]
  85. public void Fork_should_throw_if_disposed()
  86. {
  87. var subject = new ChannelSourceHandle(_mockChannelSource.Object);
  88. subject.Dispose();
  89. Action act = () => subject.Fork();
  90. act.ShouldThrow<ObjectDisposedException>();
  91. }
  92. [Fact]
  93. public void Disposing_of_handle_after_fork_should_not_dispose_of_channelSource()
  94. {
  95. var subject = new ChannelSourceHandle(_mockChannelSource.Object);
  96. var forked = subject.Fork();
  97. subject.Dispose();
  98. _mockChannelSource.Verify(s => s.Dispose(), Times.Never);
  99. forked.Dispose();
  100. _mockChannelSource.Verify(s => s.Dispose(), Times.Once);
  101. }
  102. [Fact]
  103. public void Disposing_of_fork_before_disposing_of_subject_hould_not_dispose_of_channelSource()
  104. {
  105. var subject = new ChannelSourceHandle(_mockChannelSource.Object);
  106. var forked = subject.Fork();
  107. forked.Dispose();
  108. _mockChannelSource.Verify(s => s.Dispose(), Times.Never);
  109. subject.Dispose();
  110. _mockChannelSource.Verify(s => s.Dispose(), Times.Once);
  111. }
  112. [Fact]
  113. public void Disposing_of_last_handle_should_dispose_of_connectioSource()
  114. {
  115. var subject = new ChannelSourceHandle(_mockChannelSource.Object);
  116. var forked = subject.Fork();
  117. subject.Dispose();
  118. forked.Dispose();
  119. _mockChannelSource.Verify(s => s.Dispose(), Times.Once);
  120. }
  121. }
  122. internal static class ChannelSourceHandleReflector
  123. {
  124. // private fields
  125. public static bool _disposed(this ChannelSourceHandle obj) => (bool)Reflector.GetFieldValue(obj, nameof(_disposed));
  126. public static ReferenceCounted<IChannelSource> _reference(this ChannelSourceHandle obj) => (ReferenceCounted<IChannelSource>)Reflector.GetFieldValue(obj, nameof(_reference));
  127. }
  128. }