Skip to content

Commit d8f660a

Browse files
YdbCommand.Execute* throws OperationCanceledException, if the CancellationToken has already been cancelled before the method is called. (#502)
1 parent ae74c09 commit d8f660a

File tree

4 files changed

+16
-45
lines changed

4 files changed

+16
-45
lines changed

src/Ydb.Sdk/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
- ADO.NET: `YdbConnection.OpenAsync` throws OperationCancelException when the `CancellationToken` is cancelled.
1+
- ADO.NET: Now `YdbConnection.OpenAsync` and `YdbCommand.Execute*` throw `OperationCanceledException`,
2+
if the CancellationToken has already been cancelled before the method is called.
23
- Feat ADO.NET: decimal type with arbitrary precision/scale ([#498](https://github.com/ydb-platform/ydb-dotnet-sdk/issues/498)).
34
- Fixed bug: interval value parsing in microseconds and double instead of ticks ([#497](https://github.com/ydb-platform/ydb-dotnet-sdk/issues/497)).
45
- ADO.NET: Changed `IBulkUpsertImporter.AddRowAsync` signature: `object?[] row``params object[]`.

src/Ydb.Sdk/src/Ado/YdbCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ protected override YdbDataReader ExecuteDbDataReader(CommandBehavior behavior) =
169169
protected override async Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehavior behavior,
170170
CancellationToken cancellationToken)
171171
{
172+
cancellationToken.ThrowIfCancellationRequested();
173+
172174
if (YdbConnection.IsBusy)
173175
{
174176
throw new YdbOperationInProgressException(YdbConnection);

src/Ydb.Sdk/test/Ydb.Sdk.Ado.Specification.Tests/YdbCommandTests.cs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,4 @@ public override void ExecuteReader_throws_when_transaction_required()
5353
});
5454
}
5555
}
56-
57-
public override async Task ExecuteReaderAsync_is_canceled()
58-
{
59-
await using var connection = CreateOpenConnection();
60-
await using var command = connection.CreateCommand();
61-
command.CommandText = "SELECT 1;";
62-
var task = command.ExecuteReaderAsync(CanceledToken);
63-
Assert.Equal(StatusCode.ClientTransportTimeout,
64-
(await Assert.ThrowsAnyAsync<YdbException>(() => task)).Code);
65-
}
66-
67-
public override async Task ExecuteNonQueryAsync_is_canceled()
68-
{
69-
await using var connection = CreateOpenConnection();
70-
await using var command = connection.CreateCommand();
71-
command.CommandText = "SELECT 1;";
72-
var task = command.ExecuteNonQueryAsync(CanceledToken);
73-
Assert.Equal(StatusCode.ClientTransportTimeout,
74-
(await Assert.ThrowsAnyAsync<YdbException>(() => task)).Code);
75-
}
76-
77-
public override async Task ExecuteScalarAsync_is_canceled()
78-
{
79-
await using var connection = CreateOpenConnection();
80-
await using var command = connection.CreateCommand();
81-
command.CommandText = "SELECT 1;";
82-
var task = command.ExecuteScalarAsync(CanceledToken);
83-
Assert.Equal(StatusCode.ClientTransportTimeout,
84-
(await Assert.ThrowsAnyAsync<YdbException>(() => task)).Code);
85-
}
8656
}

src/Ydb.Sdk/test/Ydb.Sdk.Ado.Tests/YdbConnectionTests.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -246,21 +246,19 @@ public async Task ExecuteMethods_WhenCancelTokenIsCanceled_ConnectionIsBroken()
246246
await using var connection = await CreateOpenConnectionAsync();
247247
var command = new YdbCommand(connection) { CommandText = "SELECT 1; SELECT 1; SELECT 1;" };
248248
using var cts = new CancellationTokenSource();
249-
cts.Cancel();
249+
await cts.CancelAsync();
250250

251-
Assert.Equal(StatusCode.ClientTransportTimeout,
252-
(await Assert.ThrowsAsync<YdbException>(async () => await command.ExecuteReaderAsync(cts.Token))).Code);
253-
Assert.Equal(ConnectionState.Broken, connection.State);
254-
// ReSharper disable once MethodSupportsCancellation
255-
await connection.OpenAsync();
256-
Assert.Equal(StatusCode.ClientTransportTimeout,
257-
(await Assert.ThrowsAsync<YdbException>(async () => await command.ExecuteScalarAsync(cts.Token))).Code);
258-
Assert.Equal(ConnectionState.Broken, connection.State);
259-
// ReSharper disable once MethodSupportsCancellation
260-
await connection.OpenAsync();
261-
Assert.Equal(StatusCode.ClientTransportTimeout,
262-
(await Assert.ThrowsAsync<YdbException>(async () => await command.ExecuteNonQueryAsync(cts.Token))).Code);
263-
Assert.Equal(ConnectionState.Broken, connection.State);
251+
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () =>
252+
await command.ExecuteReaderAsync(cts.Token));
253+
Assert.Equal(ConnectionState.Open, connection.State); // state is not changed
254+
255+
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () =>
256+
await command.ExecuteScalarAsync(cts.Token));
257+
Assert.Equal(ConnectionState.Open, connection.State); // state is not changed
258+
259+
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () =>
260+
await command.ExecuteNonQueryAsync(cts.Token));
261+
Assert.Equal(ConnectionState.Open, connection.State);
264262
}
265263

266264
[Fact]

0 commit comments

Comments
 (0)