Skip to content

Commit 76877f4

Browse files
committed
Add connect timeout exception number in more places.
1 parent fbd2b52 commit 76877f4

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

docs/content/tutorials/migrating-from-connector-net.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,4 @@ The following bugs in Connector/NET are fixed by switching to MySqlConnector. (~
143143
* [#93374](https://bugs.mysql.com/bug.php?id=93374): `MySqlDataReader.GetStream` throws `IndexOutOfRangeException`
144144
* [#93825](https://bugs.mysql.com/bug.php?id=93825): `MySqlException` loses data when serialized
145145
* [#94075](https://bugs.mysql.com/bug.php?id=94075): `MySqlCommand.Cancel` throws exception
146+
* [#94760](https://bugs.mysql.com/bug.php?id=94760): `MySqlConnection.OpenAsync(CancellationToken)` doesn’t respect cancellation token

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ private async Task<bool> OpenTcpSocketAsync(ConnectionSettings cs, ILoadBalancer
782782
{
783783
SafeDispose(ref tcpClient);
784784
Log.Info("Session{0} connect timeout expired connecting to IpAddress {1} for HostName '{2}'", m_logArguments[0], ipAddress, hostName);
785-
throw new MySqlException("Connect Timeout expired.", ex);
785+
throw new MySqlException((int) MySqlErrorCode.UnableToConnectToHost, null, "Connect Timeout expired.", ex);
786786
}
787787
}
788788
}

tests/SideBySide/ConnectAsync.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.IO;
55
using System.Security.Authentication;
6+
using System.Threading;
67
using System.Threading.Tasks;
78
using MySql.Data.MySqlClient;
89
using Xunit;
@@ -129,15 +130,51 @@ public async Task ConnectTimeoutAsync()
129130
{
130131
Server = "www.mysql.com",
131132
Pooling = false,
132-
ConnectionTimeout = 3,
133+
ConnectionTimeout = 2,
133134
};
134135

135136
using (var connection = new MySqlConnection(csb.ConnectionString))
136137
{
137138
var stopwatch = Stopwatch.StartNew();
138-
await Assert.ThrowsAsync<MySqlException>(async () => await connection.OpenAsync());
139+
try
140+
{
141+
await connection.OpenAsync();
142+
Assert.True(false);
143+
}
144+
catch (MySqlException ex)
145+
{
146+
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Number);
147+
}
148+
stopwatch.Stop();
149+
TestUtilities.AssertDuration(stopwatch, 1900, 1500);
150+
}
151+
}
152+
153+
154+
[SkippableFact(ServerFeatures.Timeout, Baseline = "https://bugs.mysql.com/bug.php?id=94760")]
155+
public async Task ConnectTimeoutAsyncCancellationToken()
156+
{
157+
var csb = new MySqlConnectionStringBuilder
158+
{
159+
Server = "www.mysql.com",
160+
Pooling = false,
161+
};
162+
163+
using (var connection = new MySqlConnection(csb.ConnectionString))
164+
{
165+
var stopwatch = Stopwatch.StartNew();
166+
try
167+
{
168+
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2)))
169+
await connection.OpenAsync(cts.Token);
170+
Assert.True(false);
171+
}
172+
catch (MySqlException ex)
173+
{
174+
Assert.Equal((int) MySqlErrorCode.UnableToConnectToHost, ex.Number);
175+
}
139176
stopwatch.Stop();
140-
TestUtilities.AssertDuration(stopwatch, 2900, 1500);
177+
TestUtilities.AssertDuration(stopwatch, 1900, 1500);
141178
}
142179
}
143180

0 commit comments

Comments
 (0)