Skip to content

Commit 09e91c5

Browse files
Copilotbgrainger
andcommitted
Improve server identification handling and add integration tests
Co-authored-by: bgrainger <[email protected]>
1 parent 3e141d3 commit 09e91c5

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,9 +2015,9 @@ private async Task GetServerIdentificationAsync(IOBehavior ioBehavior, Cancellat
20152015

20162016
var reader = new ByteArrayReader(payload.Span);
20172017
var length = reader.ReadLengthEncodedIntegerOrNull();
2018-
var serverUuid = length != -1 ? Encoding.UTF8.GetString(reader.ReadByteString(length)) : null;
2018+
var serverUuid = length > 0 ? Encoding.UTF8.GetString(reader.ReadByteString(length)) : null;
20192019
length = reader.ReadLengthEncodedIntegerOrNull();
2020-
var serverId = (length != -1 && Utf8Parser.TryParse(reader.ReadByteString(length), out long id, out _)) ? id : default(long?);
2020+
var serverId = (length > 0 && Utf8Parser.TryParse(reader.ReadByteString(length), out long id, out _)) ? id : default(long?);
20212021

20222022
ServerUuid = serverUuid;
20232023
ServerId = serverId;
@@ -2047,7 +2047,7 @@ private async Task GetServerIdentificationAsync(IOBehavior ioBehavior, Cancellat
20472047

20482048
var reader = new ByteArrayReader(payload.Span);
20492049
var length = reader.ReadLengthEncodedIntegerOrNull();
2050-
var serverId = (length != -1 && Utf8Parser.TryParse(reader.ReadByteString(length), out long id, out _)) ? id : default(long?);
2050+
var serverId = (length > 0 && Utf8Parser.TryParse(reader.ReadByteString(length), out long id, out _)) ? id : default(long?);
20512051

20522052
ServerUuid = null;
20532053
ServerId = serverId;
@@ -2065,6 +2065,9 @@ private async Task GetServerIdentificationAsync(IOBehavior ioBehavior, Cancellat
20652065
catch (MySqlException ex)
20662066
{
20672067
Log.FailedToGetServerIdentification(m_logger, ex, Id);
2068+
// Set fallback values to ensure operation can continue
2069+
ServerUuid = null;
2070+
ServerId = null;
20682071
}
20692072
}
20702073

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Diagnostics;
2+
3+
namespace IntegrationTests;
4+
5+
public class ServerIdentificationTests : IClassFixture<DatabaseFixture>, IDisposable
6+
{
7+
public ServerIdentificationTests(DatabaseFixture database)
8+
{
9+
m_database = database;
10+
}
11+
12+
public void Dispose()
13+
{
14+
}
15+
16+
[SkippableFact(ServerFeatures.Timeout)]
17+
public void CancelCommand_WithServerVerification()
18+
{
19+
// This test verifies that cancellation still works with server verification
20+
using var connection = new MySqlConnection(AppConfig.ConnectionString);
21+
connection.Open();
22+
23+
using var cmd = new MySqlCommand("SELECT SLEEP(5)", connection);
24+
var task = Task.Run(async () =>
25+
{
26+
await Task.Delay(TimeSpan.FromSeconds(0.5));
27+
cmd.Cancel();
28+
});
29+
30+
var stopwatch = Stopwatch.StartNew();
31+
TestUtilities.AssertExecuteScalarReturnsOneOrIsCanceled(cmd);
32+
Assert.InRange(stopwatch.ElapsedMilliseconds, 250, 2500);
33+
34+
#pragma warning disable xUnit1031 // Do not use blocking task operations in test method
35+
task.Wait(); // shouldn't throw
36+
#pragma warning restore xUnit1031 // Do not use blocking task operations in test method
37+
38+
TestUtilities.LogInfo("Cancellation with server verification completed successfully");
39+
}
40+
41+
[SkippableFact(ServerFeatures.KnownCertificateAuthority)]
42+
public void ServerHasServerIdentification()
43+
{
44+
using var connection = new MySqlConnection(AppConfig.ConnectionString);
45+
connection.Open();
46+
47+
// Test that we can query server identification manually
48+
using var cmd = new MySqlCommand("SELECT @@server_id", connection);
49+
var serverId = cmd.ExecuteScalar();
50+
Assert.NotNull(serverId);
51+
TestUtilities.LogInfo($"Server ID: {serverId}");
52+
53+
// Test server UUID if available (MySQL 5.6+)
54+
if (connection.ServerVersion.Version.Major > 5 ||
55+
(connection.ServerVersion.Version.Major == 5 && connection.ServerVersion.Version.Minor >= 6))
56+
{
57+
try
58+
{
59+
using var uuidCmd = new MySqlCommand("SELECT @@server_uuid", connection);
60+
var serverUuid = uuidCmd.ExecuteScalar();
61+
Assert.NotNull(serverUuid);
62+
TestUtilities.LogInfo($"Server UUID: {serverUuid}");
63+
}
64+
catch (MySqlException ex) when (ex.ErrorCode == MySqlErrorCode.UnknownSystemVariable)
65+
{
66+
// Some MySQL-compatible servers might not support server_uuid
67+
TestUtilities.LogInfo("Server UUID not supported on this server");
68+
}
69+
}
70+
}
71+
72+
private readonly DatabaseFixture m_database;
73+
}

0 commit comments

Comments
 (0)