-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes ReadAsync() behavior to register Cancellation token action befo…
…re streaming results (#1781)
- Loading branch information
1 parent
81055cf
commit 871c0d2
Showing
4 changed files
with
98 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...crosoft.Data.SqlClient/tests/ManualTests/SQL/DataReaderTest/DataReaderCancellationTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Microsoft.Data.SqlClient.ManualTesting.Tests | ||
{ | ||
public class DataReaderCancellationTest | ||
{ | ||
/// <summary> | ||
/// Test ensures cancellation token is registered before ReadAsync starts processing results from TDS Stream, | ||
/// such that when Cancel is triggered, the token is capable of canceling reading further results. | ||
/// </summary> | ||
/// <returns>Async Task</returns> | ||
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))] | ||
public static async Task CancellationTokenIsRespected_ReadAsync() | ||
{ | ||
const string longRunningQuery = @" | ||
with TenRows as (select Value from (values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) as TenRows (Value)), | ||
ThousandRows as (select A.Value as A, B.Value as B, C.Value as C from TenRows as A, TenRows as B, TenRows as C) | ||
select * | ||
from ThousandRows as A, ThousandRows as B, ThousandRows as C;"; | ||
|
||
using (var source = new CancellationTokenSource()) | ||
using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString)) | ||
{ | ||
await connection.OpenAsync(source.Token); | ||
|
||
Stopwatch stopwatch = Stopwatch.StartNew(); | ||
await Assert.ThrowsAsync<TaskCanceledException>(async () => | ||
{ | ||
using (var command = new SqlCommand(longRunningQuery, connection)) | ||
using (var reader = await command.ExecuteReaderAsync(source.Token)) | ||
{ | ||
while (await reader.ReadAsync(source.Token)) | ||
{ | ||
source.Cancel(); | ||
} | ||
} | ||
}); | ||
Assert.True(stopwatch.ElapsedMilliseconds < 10000, "Cancellation did not trigger on time."); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Test ensures cancellation token is registered before ReadAsync starts processing results from TDS Stream, | ||
/// such that when Cancel is triggered, the token is capable of canceling reading further results. | ||
/// </summary> | ||
/// <returns>Async Task</returns> | ||
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))] | ||
public static async Task CancelledCancellationTokenIsRespected_ReadAsync() | ||
{ | ||
const string longRunningQuery = @" | ||
with TenRows as (select Value from (values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) as TenRows (Value)), | ||
ThousandRows as (select A.Value as A, B.Value as B, C.Value as C from TenRows as A, TenRows as B, TenRows as C) | ||
select * | ||
from ThousandRows as A, ThousandRows as B, ThousandRows as C;"; | ||
|
||
using (var source = new CancellationTokenSource()) | ||
using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString)) | ||
{ | ||
await connection.OpenAsync(source.Token); | ||
|
||
Stopwatch stopwatch = Stopwatch.StartNew(); | ||
await Assert.ThrowsAsync<TaskCanceledException>(async () => | ||
{ | ||
using (var command = new SqlCommand(longRunningQuery, connection)) | ||
using (var reader = await command.ExecuteReaderAsync(source.Token)) | ||
{ | ||
source.Cancel(); | ||
while (await reader.ReadAsync(source.Token)) | ||
{ } | ||
} | ||
}); | ||
Assert.True(stopwatch.ElapsedMilliseconds < 10000, "Cancellation did not trigger on time."); | ||
} | ||
} | ||
} | ||
} |