PageRenderTime 59ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/BatchTransformingAsyncCursorTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 208 lines | 161 code | 32 blank | 15 comment | 7 complexity | ac172da9a98f096947da3fa9b49c573c 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.Collections.Generic;
  17. using System.Linq;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using FluentAssertions;
  21. using MongoDB.Bson;
  22. using MongoDB.Bson.TestHelpers.XunitExtensions;
  23. using Xunit;
  24. namespace MongoDB.Driver
  25. {
  26. public class BatchTransformingAsyncCursorTests
  27. {
  28. [Theory]
  29. [ParameterAttributeData]
  30. public void Should_provide_back_all_results(
  31. [Values(false, true)]
  32. bool async)
  33. {
  34. var source = Enumerable.Range(0, 15);
  35. var cursor = new ListBasedAsyncCursor<int>(source, 5);
  36. var subject = new BatchTransformingAsyncCursor<int, string>(cursor, x => x.Select(y => y.ToString()));
  37. var result = MoveNext(subject, async);
  38. result.Should().BeTrue();
  39. var batch = subject.Current.ToList();
  40. batch.Should().Equal("0", "1", "2", "3", "4");
  41. result = MoveNext(subject, async);
  42. result.Should().BeTrue();
  43. batch = subject.Current.ToList();
  44. batch.Should().Equal("5", "6", "7", "8", "9");
  45. result = MoveNext(subject, async);
  46. result.Should().BeTrue();
  47. batch = subject.Current.ToList();
  48. batch.Should().Equal("10", "11", "12", "13", "14");
  49. result = MoveNext(subject, async);
  50. result.Should().BeFalse();
  51. }
  52. [Theory]
  53. [ParameterAttributeData]
  54. public void Should_provide_back_a_filtered_list(
  55. [Values(false, true)]
  56. bool async)
  57. {
  58. var source = Enumerable.Range(0, 15);
  59. var cursor = new ListBasedAsyncCursor<int>(source, 5);
  60. var subject = new BatchTransformingAsyncCursor<int, int>(cursor, x => x.Where(y => y % 2 == 0));
  61. var result = MoveNext(subject, async);
  62. result.Should().BeTrue();
  63. var batch = subject.Current.ToList();
  64. batch.Should().Equal(0, 2, 4);
  65. result = MoveNext(subject, async);
  66. result.Should().BeTrue();
  67. batch = subject.Current.ToList();
  68. batch.Should().Equal(6, 8);
  69. result = MoveNext(subject, async);
  70. result.Should().BeTrue();
  71. batch = subject.Current.ToList();
  72. batch.Should().Equal(10, 12, 14);
  73. result = MoveNext(subject, async);
  74. result.Should().BeFalse();
  75. }
  76. [Theory]
  77. [ParameterAttributeData]
  78. public void Should_skip_empty_batches(
  79. [Values(false, true)]
  80. bool async)
  81. {
  82. var source = Enumerable.Range(0, 15);
  83. var cursor = new ListBasedAsyncCursor<int>(source, 5);
  84. // skip the second batch
  85. var subject = new BatchTransformingAsyncCursor<int, int>(cursor, x => x.Where(y => y < 5 || y > 9));
  86. var result = MoveNext(subject, async);
  87. result.Should().BeTrue();
  88. var batch = subject.Current.ToList();
  89. batch.Should().Equal(0, 1, 2, 3, 4);
  90. result = MoveNext(subject, async);
  91. result.Should().BeTrue();
  92. batch = subject.Current.ToList();
  93. batch.Should().Equal(10, 11, 12, 13, 14);
  94. result = MoveNext(subject, async);
  95. result.Should().BeFalse();
  96. }
  97. [Theory]
  98. [ParameterAttributeData]
  99. public void Should_return_false_when_all_remaining_batches_are_empty(
  100. [Values(false, true)]
  101. bool async)
  102. {
  103. var source = Enumerable.Range(0, 15);
  104. var cursor = new ListBasedAsyncCursor<int>(source, 5);
  105. var subject = new BatchTransformingAsyncCursor<int, int>(cursor, x => x.Where(y => y < 8));
  106. var result = MoveNext(subject, async);
  107. result.Should().BeTrue();
  108. var batch = subject.Current.ToList();
  109. batch.Should().Equal(0, 1, 2, 3, 4);
  110. result = MoveNext(subject, async);
  111. result.Should().BeTrue();
  112. batch = subject.Current.ToList();
  113. batch.Should().Equal(5, 6, 7);
  114. result = MoveNext(subject, async);
  115. result.Should().BeFalse();
  116. }
  117. private bool MoveNext<T>(IAsyncCursor<T> cursor, bool async)
  118. {
  119. if (async)
  120. {
  121. return cursor.MoveNextAsync().GetAwaiter().GetResult();
  122. }
  123. else
  124. {
  125. return cursor.MoveNext();
  126. }
  127. }
  128. private class ListBasedAsyncCursor<T> : IAsyncCursor<T>
  129. {
  130. private readonly List<T> _full;
  131. private readonly int _batchSize;
  132. private int _index;
  133. private List<T> _current;
  134. public ListBasedAsyncCursor(IEnumerable<T> full, int batchSize)
  135. {
  136. _full = full.ToList();
  137. _batchSize = batchSize;
  138. }
  139. public System.Collections.Generic.IEnumerable<T> Current
  140. {
  141. get
  142. {
  143. if (_current == null)
  144. {
  145. throw new Exception("AAAHHHHH");
  146. }
  147. return _current;
  148. }
  149. }
  150. public bool MoveNext(CancellationToken cancellationToken)
  151. {
  152. if (_index >= _full.Count)
  153. {
  154. _current = null;
  155. return false;
  156. }
  157. var count = _batchSize;
  158. if (_index + count > _full.Count)
  159. {
  160. count = _full.Count - _index;
  161. }
  162. _current = _full.GetRange(_index, count);
  163. _index += count;
  164. return true;
  165. }
  166. public Task<bool> MoveNextAsync(CancellationToken cancellationToken)
  167. {
  168. return Task.FromResult(MoveNext(cancellationToken));
  169. }
  170. public void Dispose()
  171. {
  172. _current = null;
  173. }
  174. }
  175. }
  176. }