|
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. |
@@ -201,6 +201,49 @@ await Record.ExceptionAsync(() => stream.ReadBytesAsync(OperationContext.NoTimeo |
201 | 201 | .ParamName.Should().Be("stream"); |
202 | 202 | } |
203 | 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 | + |
204 | 247 | [Theory] |
205 | 248 | [InlineData(true, 0, new byte[] { 0, 0 })] |
206 | 249 | [InlineData(true, 1, new byte[] { 1, 0 })] |
@@ -529,5 +572,18 @@ private Mock<IByteBuffer> CreateMockByteBuffer(int length) |
529 | 572 | mockBuffer.SetupGet(b => b.Length).Returns(length); |
530 | 573 | return mockBuffer; |
531 | 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 | + } |
532 | 588 | } |
533 | 589 | } |
0 commit comments