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

/tests/MongoDB.Bson.Tests/IO/InputBufferChunkSourceTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 309 lines | 232 code | 62 blank | 15 comment | 0 complexity | 7503ab0e8a188967a9c33b77fc5f505f MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2010-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 FluentAssertions;
  18. using MongoDB.Bson.IO;
  19. using MongoDB.Bson.TestHelpers.XunitExtensions;
  20. using Moq;
  21. using Xunit;
  22. namespace MongoDB.Bson.Tests.IO
  23. {
  24. public class InputBufferChunkSourceTests
  25. {
  26. [Fact]
  27. public void BaseSource_get_should_return_expected_result()
  28. {
  29. var mockBaseSource = new Mock<IBsonChunkSource>();
  30. var subject = new InputBufferChunkSource(mockBaseSource.Object);
  31. var result = subject.BaseSource;
  32. result.Should().BeSameAs(mockBaseSource.Object);
  33. }
  34. [Fact]
  35. public void BaseSource_get_should_throw_when_subject_is_disposed()
  36. {
  37. var mockBaseSource = new Mock<IBsonChunkSource>();
  38. var subject = new InputBufferChunkSource(mockBaseSource.Object);
  39. subject.Dispose();
  40. Action action = () => { var _ = subject.BaseSource; };
  41. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("InputBufferChunkSource");
  42. }
  43. [Fact]
  44. public void constructor_should_initialize_subject()
  45. {
  46. var mockBaseSource = new Mock<IBsonChunkSource>();
  47. var maxUnpooledChunkSize = 2;
  48. var minChunkSize = 4;
  49. var maxChunkSize = 8;
  50. var subject = new InputBufferChunkSource(mockBaseSource.Object, maxUnpooledChunkSize, minChunkSize, maxChunkSize);
  51. var reflector = new Reflector(subject);
  52. subject.BaseSource.Should().BeSameAs(mockBaseSource.Object);
  53. subject.MaxChunkSize.Should().Be(maxChunkSize);
  54. subject.MaxUnpooledChunkSize.Should().Be(maxUnpooledChunkSize);
  55. subject.MinChunkSize.Should().Be(minChunkSize);
  56. reflector._disposed.Should().BeFalse();
  57. }
  58. [Fact]
  59. public void constructor_should_throw_when_maxChunkSize_is_less_than_minChunkSize()
  60. {
  61. Action action = () => new InputBufferChunkSource(BsonChunkPool.Default, minChunkSize: 2, maxChunkSize: 1);
  62. action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("maxChunkSize");
  63. }
  64. [Fact]
  65. public void constructor_should_throw_when_maxChunkSize_is_negative()
  66. {
  67. Action action = () => new InputBufferChunkSource(BsonChunkPool.Default, maxChunkSize: -1);
  68. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("maxChunkSize");
  69. }
  70. [Theory]
  71. [ParameterAttributeData]
  72. public void constructor_should_throw_when_maxChunkSize_is_not_a_power_of_2(
  73. [Values(3, 5, 6, 7, 9, 15, 17)]
  74. int maxChunkSize)
  75. {
  76. Action action = () => new InputBufferChunkSource(BsonChunkPool.Default, maxChunkSize: maxChunkSize);
  77. action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("maxChunkSize");
  78. }
  79. [Fact]
  80. public void constructor_should_throw_when_baseSource_is_null()
  81. {
  82. Action action = () => new InputBufferChunkSource(null);
  83. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("baseSource");
  84. }
  85. [Fact]
  86. public void constructor_should_throw_when_maxUnpooledChunkSize_is_negative()
  87. {
  88. Action action = () => new InputBufferChunkSource(BsonChunkPool.Default, maxUnpooledChunkSize: -1);
  89. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("maxUnpooledChunkSize");
  90. }
  91. [Fact]
  92. public void constructor_should_throw_when_minChunkSize_is_negative()
  93. {
  94. Action action = () => new InputBufferChunkSource(BsonChunkPool.Default, minChunkSize: -1);
  95. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("minChunkSize");
  96. }
  97. [Theory]
  98. [ParameterAttributeData]
  99. public void constructor_should_throw_when_minChunkSize_is_not_a_power_of_2(
  100. [Values(3, 5, 6, 7, 9, 15, 17)]
  101. int minChunkSize)
  102. {
  103. Action action = () => new InputBufferChunkSource(BsonChunkPool.Default, minChunkSize: minChunkSize);
  104. action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("minChunkSize");
  105. }
  106. [Fact]
  107. public void constructor_should_use_default_value_for_maxChunkSize()
  108. {
  109. var mockBaseSource = new Mock<IBsonChunkSource>();
  110. var subject = new InputBufferChunkSource(mockBaseSource.Object);
  111. subject.MaxChunkSize.Should().Be(1 * 1024 * 1024);
  112. }
  113. [Fact]
  114. public void constructor_should_use_default_value_for_maxUnpooledChunkSize()
  115. {
  116. var mockBaseSource = new Mock<IBsonChunkSource>();
  117. var subject = new InputBufferChunkSource(mockBaseSource.Object);
  118. subject.MaxUnpooledChunkSize.Should().Be(4 * 1024);
  119. }
  120. [Fact]
  121. public void constructor_should_use_default_value_for_minChunkSize()
  122. {
  123. var mockBaseSource = new Mock<IBsonChunkSource>();
  124. var subject = new InputBufferChunkSource(mockBaseSource.Object);
  125. subject.MinChunkSize.Should().Be(16 * 1024);
  126. }
  127. [Fact]
  128. public void Dispose_can_be_called_more_than_once()
  129. {
  130. var mockBaseSource = new Mock<IBsonChunkSource>();
  131. var subject = new InputBufferChunkSource(mockBaseSource.Object);
  132. subject.Dispose();
  133. subject.Dispose();
  134. }
  135. [Fact]
  136. public void Dispose_should_dispose_subject()
  137. {
  138. var mockBaseSource = new Mock<IBsonChunkSource>();
  139. var subject = new InputBufferChunkSource(mockBaseSource.Object);
  140. subject.Dispose();
  141. var reflector = new Reflector(subject);
  142. reflector._disposed.Should().BeTrue();
  143. }
  144. [Theory]
  145. [ParameterAttributeData]
  146. public void GetChunk_should_return_unpooled_chunk_when_requestedSize_is_less_than_or_equal_to_maxUnpooledChunkSize(
  147. [Values(1, 2, 4 * 1024 - 1, 4 * 1024)]
  148. int requestedSize)
  149. {
  150. var mockBaseSource = new Mock<IBsonChunkSource>();
  151. var subject = new InputBufferChunkSource(mockBaseSource.Object);
  152. var result = subject.GetChunk(requestedSize);
  153. result.Should().BeOfType<ByteArrayChunk>();
  154. result.Bytes.Count.Should().Be(requestedSize);
  155. mockBaseSource.Verify(s => s.GetChunk(It.IsAny<int>()), Times.Never);
  156. }
  157. [Theory]
  158. [InlineData(1, 4)]
  159. [InlineData(2, 4)]
  160. [InlineData(3, 4)]
  161. [InlineData(4, 4)]
  162. [InlineData(5, 8)]
  163. [InlineData(6, 8)]
  164. [InlineData(7, 8)]
  165. [InlineData(8, 8)]
  166. [InlineData(9, 8)]
  167. [InlineData(10, 8)]
  168. [InlineData(11, 8)]
  169. [InlineData(12, 16)]
  170. [InlineData(13, 16)]
  171. [InlineData(14, 16)]
  172. [InlineData(15, 16)]
  173. [InlineData(16, 16)]
  174. public void GetChunk_should_round_requestedSize_to_power_of_2_without_wasting_too_much_space(int requestedSize, int roundedSize)
  175. {
  176. var mockBaseSource = new Mock<IBsonChunkSource>();
  177. var subject = new InputBufferChunkSource(mockBaseSource.Object, maxUnpooledChunkSize: 0, minChunkSize: 4, maxChunkSize: 16);
  178. mockBaseSource.Setup(s => s.GetChunk(It.IsAny<int>())).Returns(() => new Mock<IBsonChunk>().Object);
  179. subject.GetChunk(requestedSize);
  180. mockBaseSource.Verify(s => s.GetChunk(roundedSize), Times.Once);
  181. }
  182. [Theory]
  183. [ParameterAttributeData]
  184. public void GetChunk_should_throw_when_requestedSize_is_less_than_or_equal_to_zero(
  185. [Values(-1, 0)]
  186. int requestedSize)
  187. {
  188. var mockBaseSource = new Mock<IBsonChunkSource>();
  189. var subject = new InputBufferChunkSource(mockBaseSource.Object);
  190. Action action = () => subject.GetChunk(requestedSize);
  191. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("requestedSize");
  192. }
  193. [Fact]
  194. public void GetChunk_should_throw_when_subject_is_disposed()
  195. {
  196. var mockBaseSource = new Mock<IBsonChunkSource>();
  197. var subject = new InputBufferChunkSource(mockBaseSource.Object);
  198. subject.Dispose();
  199. Action action = () => subject.GetChunk(1);
  200. action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("InputBufferChunkSource");
  201. }
  202. [Fact]
  203. public void MaxChunkSize_get_should_return_expected_result()
  204. {
  205. var mockBaseSource = new Mock<IBsonChunkSource>();
  206. var maxChunkSize = 32 * 1024 * 1024;
  207. var subject = new InputBufferChunkSource(mockBaseSource.Object, maxChunkSize: maxChunkSize);
  208. var result = subject.MaxChunkSize;
  209. result.Should().Be(maxChunkSize);
  210. }
  211. [Fact]
  212. public void MaxChunkUnpooledSize_get_should_return_expected_result()
  213. {
  214. var mockBaseSource = new Mock<IBsonChunkSource>();
  215. var maxUnpooledChunkSize = 8 * 1024;
  216. var subject = new InputBufferChunkSource(mockBaseSource.Object, maxUnpooledChunkSize: maxUnpooledChunkSize);
  217. var result = subject.MaxUnpooledChunkSize;
  218. result.Should().Be(maxUnpooledChunkSize);
  219. }
  220. [Fact]
  221. public void MinChunkSize_get_should_return_expected_result()
  222. {
  223. var mockBaseSource = new Mock<IBsonChunkSource>();
  224. var minChunkSize = 8 * 1024;
  225. var subject = new InputBufferChunkSource(mockBaseSource.Object, minChunkSize: minChunkSize);
  226. var result = subject.MinChunkSize;
  227. result.Should().Be(minChunkSize);
  228. }
  229. // nested types
  230. private class Reflector
  231. {
  232. private readonly InputBufferChunkSource _instance;
  233. public Reflector(InputBufferChunkSource instance)
  234. {
  235. _instance = instance;
  236. }
  237. public bool _disposed
  238. {
  239. get
  240. {
  241. var field = typeof(InputBufferChunkSource).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance);
  242. return (bool)field.GetValue(_instance);
  243. }
  244. }
  245. }
  246. }
  247. }