Skip to content

Commit 24ff078

Browse files
committed
CSHARP-1117: Added tests for InputBufferChunkSource.
1 parent 5d963f0 commit 24ff078

File tree

3 files changed

+384
-18
lines changed

3 files changed

+384
-18
lines changed
Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
/* Copyright 2010-2014 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+
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using System.Reflection;
20+
using System.Text;
21+
using System.Threading.Tasks;
22+
using FluentAssertions;
23+
using MongoDB.Bson.IO;
24+
using NSubstitute;
25+
using NUnit.Framework;
26+
27+
namespace MongoDB.Bson.Tests.IO
28+
{
29+
[TestFixture]
30+
public class InputBufferChunkSourceTests
31+
{
32+
[Test]
33+
public void BaseSource_get_should_return_expected_result()
34+
{
35+
var baseSource = Substitute.For<IBsonChunkSource>();
36+
var subject = new InputBufferChunkSource(baseSource);
37+
38+
var result = subject.BaseSource;
39+
40+
result.Should().BeSameAs(baseSource);
41+
}
42+
43+
[Test]
44+
public void BaseSource_get_should_throw_when_subject_is_disposed()
45+
{
46+
var baseSource = Substitute.For<IBsonChunkSource>();
47+
var subject = new InputBufferChunkSource(baseSource);
48+
subject.Dispose();
49+
50+
Action action = () => { var _ = subject.BaseSource; };
51+
52+
action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("InputBufferChunkSource");
53+
}
54+
55+
[Test]
56+
public void constructor_should_initialize_subject()
57+
{
58+
var baseSource = Substitute.For<IBsonChunkSource>();
59+
var maxUnpooledChunkSize = 2;
60+
var minChunkSize = 4;
61+
var maxChunkSize = 8;
62+
63+
var subject = new InputBufferChunkSource(baseSource, maxUnpooledChunkSize, minChunkSize, maxChunkSize);
64+
65+
var reflector = new Reflector(subject);
66+
subject.BaseSource.Should().BeSameAs(baseSource);
67+
subject.MaxChunkSize.Should().Be(maxChunkSize);
68+
subject.MaxUnpooledChunkSize.Should().Be(maxUnpooledChunkSize);
69+
subject.MinChunkSize.Should().Be(minChunkSize);
70+
reflector._disposed.Should().BeFalse();
71+
}
72+
73+
[Test]
74+
public void constructor_should_throw_when_maxChunkSize_is_less_than_minChunkSize()
75+
{
76+
Action action = () => new InputBufferChunkSource(BsonChunkPool.Default, minChunkSize: 2, maxChunkSize: 1);
77+
78+
action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("maxChunkSize");
79+
}
80+
81+
[Test]
82+
public void constructor_should_throw_when_maxChunkSize_is_negative()
83+
{
84+
Action action = () => new InputBufferChunkSource(BsonChunkPool.Default, maxChunkSize: -1);
85+
86+
action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("maxChunkSize");
87+
}
88+
89+
[Test]
90+
public void constructor_should_throw_when_maxChunkSize_is_not_a_power_of_2(
91+
[Values(3, 5, 6, 7, 9, 15, 17)]
92+
int maxChunkSize)
93+
{
94+
Action action = () => new InputBufferChunkSource(BsonChunkPool.Default, maxChunkSize: maxChunkSize);
95+
96+
action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("maxChunkSize");
97+
}
98+
99+
[Test]
100+
public void constructor_should_throw_when_baseSource_is_null()
101+
{
102+
Action action = () => new InputBufferChunkSource(null);
103+
104+
action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("baseSource");
105+
}
106+
107+
[Test]
108+
public void constructor_should_throw_when_maxUnpooledChunkSize_is_negative()
109+
{
110+
Action action = () => new InputBufferChunkSource(BsonChunkPool.Default, maxUnpooledChunkSize: -1);
111+
112+
action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("maxUnpooledChunkSize");
113+
}
114+
115+
[Test]
116+
public void constructor_should_throw_when_minChunkSize_is_negative()
117+
{
118+
Action action = () => new InputBufferChunkSource(BsonChunkPool.Default, minChunkSize: -1);
119+
120+
action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("minChunkSize");
121+
}
122+
123+
[Test]
124+
public void constructor_should_throw_when_minChunkSize_is_not_a_power_of_2(
125+
[Values(3, 5, 6, 7, 9, 15, 17)]
126+
int minChunkSize)
127+
{
128+
Action action = () => new InputBufferChunkSource(BsonChunkPool.Default, minChunkSize: minChunkSize);
129+
130+
action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("minChunkSize");
131+
}
132+
133+
[Test]
134+
public void constructor_should_use_default_value_for_maxChunkSize()
135+
{
136+
var baseSource = Substitute.For<IBsonChunkSource>();
137+
138+
var subject = new InputBufferChunkSource(baseSource);
139+
140+
subject.MaxChunkSize.Should().Be(1 * 1024 * 1024);
141+
}
142+
143+
[Test]
144+
public void constructor_should_use_default_value_for_maxUnpooledChunkSize()
145+
{
146+
var baseSource = Substitute.For<IBsonChunkSource>();
147+
148+
var subject = new InputBufferChunkSource(baseSource);
149+
150+
subject.MaxUnpooledChunkSize.Should().Be(4 * 1024);
151+
}
152+
153+
[Test]
154+
public void constructor_should_use_default_value_for_minChunkSize()
155+
{
156+
var baseSource = Substitute.For<IBsonChunkSource>();
157+
158+
var subject = new InputBufferChunkSource(baseSource);
159+
160+
subject.MinChunkSize.Should().Be(16 * 1024);
161+
}
162+
163+
[Test]
164+
public void Dispose_can_be_called_more_than_once()
165+
{
166+
var baseSource = Substitute.For<IBsonChunkSource>();
167+
var subject = new InputBufferChunkSource(baseSource);
168+
169+
subject.Dispose();
170+
subject.Dispose();
171+
}
172+
173+
[Test]
174+
public void Dispose_should_dispose_subject()
175+
{
176+
var baseSource = Substitute.For<IBsonChunkSource>();
177+
var subject = new InputBufferChunkSource(baseSource);
178+
179+
subject.Dispose();
180+
181+
var reflector = new Reflector(subject);
182+
reflector._disposed.Should().BeTrue();
183+
}
184+
185+
[Test]
186+
public void GetChunk_should_return_unpooled_chunk_when_requestedSize_is_less_than_or_equal_to_maxUnpooledChunkSize(
187+
[Values(1, 2, 4 * 1024 - 1, 4 * 1024)]
188+
int requestedSize)
189+
{
190+
var baseSource = Substitute.For<IBsonChunkSource>();
191+
var subject = new InputBufferChunkSource(baseSource);
192+
193+
var result = subject.GetChunk(requestedSize);
194+
195+
result.Should().BeOfType<ByteArrayChunk>();
196+
result.Bytes.Count.Should().Be(requestedSize);
197+
baseSource.DidNotReceive().GetChunk(Arg.Any<int>());
198+
}
199+
200+
[TestCase(1, 4)]
201+
[TestCase(2, 4)]
202+
[TestCase(3, 4)]
203+
[TestCase(4, 4)]
204+
[TestCase(5, 8)]
205+
[TestCase(6, 8)]
206+
[TestCase(7, 8)]
207+
[TestCase(8, 8)]
208+
[TestCase(9, 8)]
209+
[TestCase(10, 8)]
210+
[TestCase(11, 8)]
211+
[TestCase(12, 16)]
212+
[TestCase(13, 16)]
213+
[TestCase(14, 16)]
214+
[TestCase(15, 16)]
215+
[TestCase(16, 16)]
216+
public void GetChunk_should_round_requestedSize_to_power_of_2_without_wasting_too_much_space(int requestedSize, int roundedSize)
217+
{
218+
var baseSource = Substitute.For<IBsonChunkSource>();
219+
var subject = new InputBufferChunkSource(baseSource, maxUnpooledChunkSize: 0, minChunkSize: 4, maxChunkSize: 16);
220+
baseSource.GetChunk(Arg.Any<int>()).Returns(Substitute.For<IBsonChunk>());
221+
222+
var chunk = subject.GetChunk(requestedSize);
223+
224+
baseSource.Received(1).GetChunk(roundedSize);
225+
}
226+
227+
[Test]
228+
public void GetChunk_should_throw_when_requestedSize_is_less_than_or_equal_to_zero(
229+
[Values(-1, 0)]
230+
int requestedSize)
231+
{
232+
var baseSource = Substitute.For<IBsonChunkSource>();
233+
var subject = new InputBufferChunkSource(baseSource);
234+
235+
Action action = () => subject.GetChunk(requestedSize);
236+
237+
action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("requestedSize");
238+
}
239+
240+
[Test]
241+
public void GetChunk_should_throw_when_subject_is_disposed()
242+
{
243+
var baseSource = Substitute.For<IBsonChunkSource>();
244+
var subject = new InputBufferChunkSource(baseSource);
245+
subject.Dispose();
246+
247+
Action action = () => subject.GetChunk(1);
248+
249+
action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("InputBufferChunkSource");
250+
}
251+
252+
[Test]
253+
public void MaxChunkSize_get_should_return_expected_result()
254+
{
255+
var baseSource = Substitute.For<IBsonChunkSource>();
256+
var maxChunkSize = 32 * 1024 * 1024;
257+
var subject = new InputBufferChunkSource(baseSource, maxChunkSize: maxChunkSize);
258+
259+
var result = subject.MaxChunkSize;
260+
261+
result.Should().Be(maxChunkSize);
262+
}
263+
264+
[Test]
265+
public void MaxChunkUnpooledSize_get_should_return_expected_result()
266+
{
267+
var baseSource = Substitute.For<IBsonChunkSource>();
268+
var maxUnpooledChunkSize = 8 * 1024;
269+
var subject = new InputBufferChunkSource(baseSource, maxUnpooledChunkSize: maxUnpooledChunkSize);
270+
271+
var result = subject.MaxUnpooledChunkSize;
272+
273+
result.Should().Be(maxUnpooledChunkSize);
274+
}
275+
276+
[Test]
277+
public void MinChunkSize_get_should_return_expected_result()
278+
{
279+
var baseSource = Substitute.For<IBsonChunkSource>();
280+
var minChunkSize = 8 * 1024;
281+
var subject = new InputBufferChunkSource(baseSource, minChunkSize: minChunkSize);
282+
283+
var result = subject.MinChunkSize;
284+
285+
result.Should().Be(minChunkSize);
286+
}
287+
288+
// nested types
289+
private class Reflector
290+
{
291+
private readonly InputBufferChunkSource _instance;
292+
293+
public Reflector(InputBufferChunkSource instance)
294+
{
295+
_instance = instance;
296+
}
297+
298+
public bool _disposed
299+
{
300+
get
301+
{
302+
var field = typeof(InputBufferChunkSource).GetField("_disposed", BindingFlags.NonPublic | BindingFlags.Instance);
303+
return (bool)field.GetValue(_instance);
304+
}
305+
}
306+
}
307+
}
308+
}

src/MongoDB.Bson.Tests/MongoDB.Bson.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<Compile Include="IO\ByteArrayBufferTests.cs" />
9090
<Compile Include="IO\ByteArrayChunkTests.cs" />
9191
<Compile Include="IO\ByteBufferSliceTests.cs" />
92+
<Compile Include="IO\InputBufferChunkSourceTests.cs" />
9293
<Compile Include="IO\MultiChunkBufferTests.cs" />
9394
<Compile Include="Jira\CSharp728Tests.cs" />
9495
<Compile Include="Jira\CSharp708Tests.cs" />

0 commit comments

Comments
 (0)