|
1 | | -/* Copyright 2013-present MongoDB Inc. |
| 1 | +/* Copyright 2010-present MongoDB Inc. |
2 | 2 | * |
3 | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | * you may not use this file except in compliance with the License. |
@@ -90,20 +90,18 @@ public async Task ReadBytes_with_byte_array_should_have_expected_effect_for_part |
90 | 90 | var bytes = new byte[] { 1, 2, 3 }; |
91 | 91 | var n = 0; |
92 | 92 | var position = 0; |
93 | | - Task<int> ReadPartial (byte[] buffer, int offset, int count) |
| 93 | + int ReadPartial (byte[] buffer, int offset, int count) |
94 | 94 | { |
95 | 95 | var length = partition[n++]; |
96 | 96 | Buffer.BlockCopy(bytes, position, buffer, offset, length); |
97 | 97 | position += length; |
98 | | - return Task.FromResult(length); |
| 98 | + return length; |
99 | 99 | } |
100 | 100 |
|
101 | 101 | mockStream.Setup(s => s.ReadAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>())) |
102 | | - .Returns((byte[] buffer, int offset, int count, CancellationToken cancellationToken) => ReadPartial(buffer, offset, count)); |
103 | | - mockStream.Setup(s => s.BeginRead(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<AsyncCallback>(), It.IsAny<object>())) |
104 | | - .Returns((byte[] buffer, int offset, int count, AsyncCallback callback, object state) => ReadPartial(buffer, offset, count)); |
105 | | - mockStream.Setup(s => s.EndRead(It.IsAny<IAsyncResult>())) |
106 | | - .Returns<IAsyncResult>(x => ((Task<int>)x).GetAwaiter().GetResult()); |
| 102 | + .Returns((byte[] buffer, int offset, int count, CancellationToken cancellationToken) => Task.FromResult(ReadPartial(buffer, offset, count))); |
| 103 | + mockStream.Setup(s => s.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>())) |
| 104 | + .Returns((byte[] buffer, int offset, int count) => ReadPartial(buffer, offset, count)); |
107 | 105 | var destination = new byte[3]; |
108 | 106 |
|
109 | 107 | if (async) |
@@ -203,6 +201,49 @@ await Record.ExceptionAsync(() => stream.ReadBytesAsync(OperationContext.NoTimeo |
203 | 201 | .ParamName.Should().Be("stream"); |
204 | 202 | } |
205 | 203 |
|
| 204 | + [Theory] |
| 205 | + [ParameterAttributeData] |
| 206 | + public async Task ReadBytes_with_byte_array_throws_on_timeout([Values(true, false)]bool async) |
| 207 | + { |
| 208 | + var streamMock = new Mock<Stream>(); |
| 209 | + SetupStreamRead(streamMock); |
| 210 | + var stream = streamMock.Object; |
| 211 | + |
| 212 | + var destination = new byte[2]; |
| 213 | + var timeout = TimeSpan.FromMilliseconds(10); |
| 214 | + |
| 215 | + var exception = async ? |
| 216 | + await Record.ExceptionAsync(() => stream.ReadAsync(destination, 0, 2, timeout, CancellationToken.None)) : |
| 217 | + Record.Exception(() => stream.Read(destination, 0, 2, timeout, CancellationToken.None)); |
| 218 | + |
| 219 | + exception.Should().BeOfType<TimeoutException>(); |
| 220 | + } |
| 221 | + |
| 222 | + [Theory] |
| 223 | + [ParameterAttributeData] |
| 224 | + public async Task ReadBytes_with_byte_array_throws_on_cancellation([Values(true, false)]bool async) |
| 225 | + { |
| 226 | + var streamMock = new Mock<Stream>(); |
| 227 | + SetupStreamRead(streamMock); |
| 228 | + var stream = streamMock.Object; |
| 229 | + |
| 230 | + var destination = new byte[2]; |
| 231 | + using var cancellationTokenSource = new CancellationTokenSource(10); |
| 232 | + |
| 233 | + var exception = async ? |
| 234 | + await Record.ExceptionAsync(() => stream.ReadAsync(destination, 0, 2, Timeout.InfiniteTimeSpan, cancellationTokenSource.Token)) : |
| 235 | + Record.Exception(() => stream.Read(destination, 0, 2, Timeout.InfiniteTimeSpan, cancellationTokenSource.Token)); |
| 236 | + |
| 237 | + if (async) |
| 238 | + { |
| 239 | + exception.Should().BeOfType<TaskCanceledException>(); |
| 240 | + } |
| 241 | + else |
| 242 | + { |
| 243 | + exception.Should().BeOfType<OperationCanceledException>(); |
| 244 | + } |
| 245 | + } |
| 246 | + |
206 | 247 | [Theory] |
207 | 248 | [InlineData(true, 0, new byte[] { 0, 0 })] |
208 | 249 | [InlineData(true, 1, new byte[] { 1, 0 })] |
@@ -267,20 +308,18 @@ public async Task ReadBytes_with_byte_buffer_should_have_expected_effect_for_par |
267 | 308 | var destination = new ByteArrayBuffer(new byte[3], 3); |
268 | 309 | var n = 0; |
269 | 310 | var position = 0; |
270 | | - Task<int> ReadPartial (byte[] buffer, int offset, int count) |
| 311 | + int ReadPartial (byte[] buffer, int offset, int count) |
271 | 312 | { |
272 | 313 | var length = partition[n++]; |
273 | 314 | Buffer.BlockCopy(bytes, position, buffer, offset, length); |
274 | 315 | position += length; |
275 | | - return Task.FromResult(length); |
| 316 | + return length; |
276 | 317 | } |
277 | 318 |
|
278 | 319 | mockStream.Setup(s => s.ReadAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>())) |
279 | | - .Returns((byte[] buffer, int offset, int count, CancellationToken cancellationToken) => ReadPartial(buffer, offset, count)); |
280 | | - mockStream.Setup(s => s.BeginRead(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<AsyncCallback>(), It.IsAny<object>())) |
281 | | - .Returns((byte[] buffer, int offset, int count, AsyncCallback callback, object state) => ReadPartial(buffer, offset, count)); |
282 | | - mockStream.Setup(s => s.EndRead(It.IsAny<IAsyncResult>())) |
283 | | - .Returns<IAsyncResult>(x => ((Task<int>)x).GetAwaiter().GetResult()); |
| 320 | + .Returns((byte[] buffer, int offset, int count, CancellationToken cancellationToken) => Task.FromResult(ReadPartial(buffer, offset, count))); |
| 321 | + mockStream.Setup(s => s.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>())) |
| 322 | + .Returns((byte[] buffer, int offset, int count) => ReadPartial(buffer, offset, count)); |
284 | 323 |
|
285 | 324 | if (async) |
286 | 325 | { |
@@ -533,5 +572,18 @@ private Mock<IByteBuffer> CreateMockByteBuffer(int length) |
533 | 572 | mockBuffer.SetupGet(b => b.Length).Returns(length); |
534 | 573 | return mockBuffer; |
535 | 574 | } |
| 575 | + |
| 576 | + private void SetupStreamRead(Mock<Stream> streamMock, TaskCompletionSource<int> readTaskCompletionSource = null) |
| 577 | + { |
| 578 | + readTaskCompletionSource ??= new TaskCompletionSource<int>(); |
| 579 | + streamMock.Setup(s => s.Close()).Callback(() => |
| 580 | + { |
| 581 | + readTaskCompletionSource.SetException(new IOException()); |
| 582 | + }); |
| 583 | + streamMock.Setup(s => s.Read(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>())).Returns(() => |
| 584 | + readTaskCompletionSource.Task.GetAwaiter().GetResult()); |
| 585 | + streamMock.Setup(s => s.ReadAsync(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<CancellationToken>())).Returns(() => |
| 586 | + readTaskCompletionSource.Task); |
| 587 | + } |
536 | 588 | } |
537 | 589 | } |
0 commit comments