Skip to content

Commit ff1a5fe

Browse files
committed
Throw OperationCanceledException. Fixes #931
1 parent 837891f commit ff1a5fe

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/MySqlConnector/MySqlConnection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ internal async Task OpenAsync(IOBehavior? ioBehavior, CancellationToken cancella
419419
catch (MySqlException)
420420
{
421421
SetState(ConnectionState.Closed);
422+
cancellationToken.ThrowIfCancellationRequested();
422423
throw;
423424
}
424425
catch (SocketException ex)

tests/SideBySide/ConnectAsync.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,11 @@ public async Task ConnectTimeoutAsyncCancellationToken()
145145
using var connection = new MySqlConnection(csb.ConnectionString);
146146
var stopwatch = Stopwatch.StartNew();
147147
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));
148-
var ex = await Assert.ThrowsAsync<MySqlException>(async () => await connection.OpenAsync(cts.Token));
148+
var task = connection.OpenAsync(cts.Token);
149+
var ex = await Assert.ThrowsAsync<OperationCanceledException>(async () => await task);
149150
stopwatch.Stop();
150-
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Number);
151+
Assert.Equal(TaskStatus.Canceled, task.Status);
152+
Assert.Equal(cts.Token, ex.CancellationToken);
151153
TestUtilities.AssertDuration(stopwatch, 1900, 1500);
152154
}
153155

0 commit comments

Comments
 (0)