-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathDataReaderCancellationTest.cs
49 lines (45 loc) · 2.13 KB
/
DataReaderCancellationTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 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 < 1000, "Cancellation did not trigger on time.");
}
}
}
}