|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Microsoft.Data.SqlClient.ManualTesting.Tests |
| 11 | +{ |
| 12 | + public class DataReaderCancellationTest |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Test ensures cancellation token is registered before ReadAsync starts processing results from TDS Stream, |
| 16 | + /// such that when Cancel is triggered, the token is capable of canceling reading further results. |
| 17 | + /// </summary> |
| 18 | + /// <returns>Async Task</returns> |
| 19 | + [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))] |
| 20 | + public static async Task CancellationTokenIsRespected_ReadAsync() |
| 21 | + { |
| 22 | + const string longRunningQuery = @" |
| 23 | +with TenRows as (select Value from (values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) as TenRows (Value)), |
| 24 | + 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) |
| 25 | +select * |
| 26 | +from ThousandRows as A, ThousandRows as B, ThousandRows as C;"; |
| 27 | + |
| 28 | + using (var source = new CancellationTokenSource()) |
| 29 | + using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString)) |
| 30 | + { |
| 31 | + await connection.OpenAsync(source.Token); |
| 32 | + |
| 33 | + Stopwatch stopwatch = Stopwatch.StartNew(); |
| 34 | + await Assert.ThrowsAsync<TaskCanceledException>(async () => |
| 35 | + { |
| 36 | + using (var command = new SqlCommand(longRunningQuery, connection)) |
| 37 | + using (var reader = await command.ExecuteReaderAsync(source.Token)) |
| 38 | + { |
| 39 | + while (await reader.ReadAsync(source.Token)) |
| 40 | + { |
| 41 | + source.Cancel(); |
| 42 | + } |
| 43 | + } |
| 44 | + }); |
| 45 | + Assert.True(stopwatch.ElapsedMilliseconds < 10000, "Cancellation did not trigger on time."); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Test ensures cancellation token is registered before ReadAsync starts processing results from TDS Stream, |
| 51 | + /// such that when Cancel is triggered, the token is capable of canceling reading further results. |
| 52 | + /// </summary> |
| 53 | + /// <returns>Async Task</returns> |
| 54 | + [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))] |
| 55 | + public static async Task CancelledCancellationTokenIsRespected_ReadAsync() |
| 56 | + { |
| 57 | + const string longRunningQuery = @" |
| 58 | +with TenRows as (select Value from (values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) as TenRows (Value)), |
| 59 | + 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) |
| 60 | +select * |
| 61 | +from ThousandRows as A, ThousandRows as B, ThousandRows as C;"; |
| 62 | + |
| 63 | + using (var source = new CancellationTokenSource()) |
| 64 | + using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString)) |
| 65 | + { |
| 66 | + await connection.OpenAsync(source.Token); |
| 67 | + |
| 68 | + Stopwatch stopwatch = Stopwatch.StartNew(); |
| 69 | + await Assert.ThrowsAsync<TaskCanceledException>(async () => |
| 70 | + { |
| 71 | + using (var command = new SqlCommand(longRunningQuery, connection)) |
| 72 | + using (var reader = await command.ExecuteReaderAsync(source.Token)) |
| 73 | + { |
| 74 | + source.Cancel(); |
| 75 | + while (await reader.ReadAsync(source.Token)) |
| 76 | + { } |
| 77 | + } |
| 78 | + }); |
| 79 | + Assert.True(stopwatch.ElapsedMilliseconds < 10000, "Cancellation did not trigger on time."); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments