Skip to content

Commit 1ccb09e

Browse files
committed
pr
1 parent 88b4c6c commit 1ccb09e

File tree

2 files changed

+64
-12
lines changed

2 files changed

+64
-12
lines changed

src/MongoDB.Driver/Core/Misc/StreamExtensionMethods.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,15 @@ public static void EfficientCopyTo(this Stream input, Stream output)
3636
}
3737
}
3838

39-
public static int Read(this Stream stream, byte[] buffer, int offset, int count, TimeSpan timeout, CancellationToken cancellationToken)
40-
{
41-
return ExecuteOperationWithTimeout(
39+
public static int Read(this Stream stream, byte[] buffer, int offset, int count, TimeSpan timeout, CancellationToken cancellationToken) =>
40+
ExecuteOperationWithTimeout(
4241
stream,
4342
(str, state) => str.Read(state.Buffer, state.Offset, state.Count),
4443
buffer,
4544
offset,
4645
count,
4746
timeout,
4847
cancellationToken);
49-
}
5048

5149
public static async Task<int> ReadAsync(this Stream stream, byte[] buffer, int offset, int count, TimeSpan timeout, CancellationToken cancellationToken)
5250
{
@@ -190,8 +188,7 @@ public static async Task ReadBytesAsync(this Stream stream, byte[] destination,
190188
}
191189
}
192190

193-
public static void Write(this Stream stream, byte[] buffer, int offset, int count, TimeSpan timeout, CancellationToken cancellationToken)
194-
{
191+
public static void Write(this Stream stream, byte[] buffer, int offset, int count, TimeSpan timeout, CancellationToken cancellationToken) =>
195192
ExecuteOperationWithTimeout(
196193
stream,
197194
(str, state) =>
@@ -204,7 +201,6 @@ public static void Write(this Stream stream, byte[] buffer, int offset, int coun
204201
count,
205202
timeout,
206203
cancellationToken);
207-
}
208204

209205
public static async Task WriteAsync(this Stream stream, byte[] buffer, int offset, int count, TimeSpan timeout, CancellationToken cancellationToken)
210206
{
@@ -304,7 +300,7 @@ private static TResult ExecuteOperationWithTimeout<TResult>(Stream stream, Func<
304300
}
305301
catch (IOException)
306302
{
307-
if (callbackState?.OperationState == OperationState.Cancelled)
303+
if (callbackState?.OperationState == OperationState.Interrupted)
308304
{
309305
cancellationToken.ThrowIfCancellationRequested();
310306
throw new TimeoutException();
@@ -321,9 +317,9 @@ private static TResult ExecuteOperationWithTimeout<TResult>(Stream stream, Func<
321317
static void DisposeStreamCallback(object state)
322318
{
323319
var disposeCallbackState = (StreamDisposeCallbackState)state;
324-
if (!disposeCallbackState.TryChangeStateFromInProgress(OperationState.Cancelled))
320+
if (!disposeCallbackState.TryChangeStateFromInProgress(OperationState.Interrupted))
325321
{
326-
// if cannot change the state - then I/O was already succeeded
322+
// If the state can't be changed - then I/O had already succeeded
327323
return;
328324
}
329325

@@ -352,7 +348,7 @@ private enum OperationState
352348
{
353349
InProgress = 0,
354350
Done,
355-
Cancelled,
351+
Interrupted,
356352
}
357353
}
358354
}

tests/MongoDB.Driver.Tests/Core/Misc/StreamExtensionMethodsTests.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-present MongoDB Inc.
1+
/* Copyright 2010-present MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -201,6 +201,49 @@ await Record.ExceptionAsync(() => stream.ReadBytesAsync(OperationContext.NoTimeo
201201
.ParamName.Should().Be("stream");
202202
}
203203

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+
204247
[Theory]
205248
[InlineData(true, 0, new byte[] { 0, 0 })]
206249
[InlineData(true, 1, new byte[] { 1, 0 })]
@@ -529,5 +572,18 @@ private Mock<IByteBuffer> CreateMockByteBuffer(int length)
529572
mockBuffer.SetupGet(b => b.Length).Returns(length);
530573
return mockBuffer;
531574
}
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+
}
532588
}
533589
}

0 commit comments

Comments
 (0)