-
Notifications
You must be signed in to change notification settings - Fork 330
[7.0] Test | Fix Transient Fault handling and other flaky unit tests #4114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,7 +11,6 @@ | |||||
|
|
||||||
| namespace Microsoft.Data.SqlClient.UnitTests.SimulatedServerTests | ||||||
| { | ||||||
| [Trait("Category", "flaky")] | ||||||
| [Collection("SimulatedServerTests")] | ||||||
| public class ConnectionFailoverTests | ||||||
| { | ||||||
|
|
@@ -70,7 +69,7 @@ public void TransientFault_NoFailover_DoesNotClearPool(uint errorCode) | |||||
| Assert.Equal($"localhost,{initialServer.EndPoint.Port}", secondConnection.DataSource); | ||||||
|
|
||||||
| // 1 for the initial connection, 2 for the second connection | ||||||
| Assert.Equal(3, initialServer.PreLoginCount); | ||||||
| Assert.Equal(3, initialServer.PreLoginCount - initialServer.AbandonedPreLoginCount); | ||||||
| // A failover should not be triggered, so prelogin count to the failover server should be 0 | ||||||
| Assert.Equal(0, failoverServer.PreLoginCount); | ||||||
| } | ||||||
|
|
@@ -219,6 +218,7 @@ public void NetworkDelay_ShouldConnectToPrimary() | |||||
| InitialCatalog = "master", // Required for failover partner to work | ||||||
| ConnectTimeout = 5, | ||||||
| Encrypt = false, | ||||||
| Pooling = false, // Disable pooling to ensure a fresh connection attempt is made | ||||||
| MultiSubnetFailover = false, | ||||||
| #if NETFRAMEWORK | ||||||
| TransparentNetworkIPResolution = false, | ||||||
|
|
@@ -268,6 +268,7 @@ public void NetworkError_WithUserProvidedPartner_RetryDisabled_ShouldConnectToFa | |||||
| ConnectRetryCount = 0, // Disable retry | ||||||
| FailoverPartner = $"localhost,{failoverServer.EndPoint.Port}", // User provided failover partner | ||||||
| Encrypt = false, | ||||||
| Pooling = false, // Disable pooling to ensure a fresh connection attempt is made on failover | ||||||
| }; | ||||||
| using SqlConnection connection = new(builder.ConnectionString); | ||||||
|
|
||||||
|
|
@@ -313,6 +314,9 @@ public void NetworkError_WithUserProvidedPartner_RetryEnabled_ShouldConnectToFai | |||||
| ConnectRetryInterval = 1, | ||||||
| FailoverPartner = $"localhost,{failoverServer.EndPoint.Port}", // User provided failover partner | ||||||
| Encrypt = false, | ||||||
| #if NETFRAMEWORK | ||||||
| TransparentNetworkIPResolution = false, | ||||||
| #endif | ||||||
| }; | ||||||
| using SqlConnection connection = new(builder.ConnectionString); | ||||||
| // Act | ||||||
|
|
@@ -324,7 +328,8 @@ public void NetworkError_WithUserProvidedPartner_RetryEnabled_ShouldConnectToFai | |||||
| Assert.Equal(ConnectionState.Open, connection.State); | ||||||
| Assert.Equal($"localhost,{failoverServer.EndPoint.Port}", connection.DataSource); | ||||||
| Assert.Equal(1, server.PreLoginCount); | ||||||
| Assert.Equal(1, failoverServer.PreLoginCount); | ||||||
| Assert.Equal(0, server.Login7Count); | ||||||
| Assert.Equal(1, failoverServer.PreLoginCount - failoverServer.AbandonedPreLoginCount); | ||||||
| } | ||||||
|
|
||||||
| [Theory] | ||||||
|
|
@@ -357,7 +362,8 @@ public void TransientFault_ShouldConnectToPrimary(uint errorCode) | |||||
| InitialCatalog = "master", | ||||||
| ConnectTimeout = 30, | ||||||
| ConnectRetryInterval = 1, | ||||||
| Encrypt = false | ||||||
| Encrypt = false, | ||||||
| Pooling = false, // Disable pooling to ensure a fresh connection attempt is made | ||||||
| }; | ||||||
| using SqlConnection connection = new(builder.ConnectionString); | ||||||
|
|
||||||
|
|
@@ -369,7 +375,7 @@ public void TransientFault_ShouldConnectToPrimary(uint errorCode) | |||||
| Assert.Equal($"localhost,{server.EndPoint.Port}", connection.DataSource); | ||||||
|
|
||||||
| // Failures should prompt the client to return to the original server, resulting in a login count of 2 | ||||||
| Assert.Equal(2, server.PreLoginCount); | ||||||
| Assert.Equal(2, server.PreLoginCount - server.AbandonedPreLoginCount); | ||||||
| } | ||||||
|
|
||||||
| [Theory] | ||||||
|
|
@@ -454,7 +460,7 @@ public void TransientFault_WithUserProvidedPartner_ShouldConnectToPrimary(uint e | |||||
| FailoverPartner = $"localhost:{failoverServer.EndPoint.Port}", // User provided failover partner | ||||||
| }; | ||||||
| using SqlConnection connection = new(builder.ConnectionString); | ||||||
|
|
||||||
| // Act | ||||||
| connection.Open(); | ||||||
|
|
||||||
|
|
@@ -463,7 +469,7 @@ public void TransientFault_WithUserProvidedPartner_ShouldConnectToPrimary(uint e | |||||
| Assert.Equal($"localhost,{server.EndPoint.Port}", connection.DataSource); | ||||||
|
|
||||||
| // Failures should prompt the client to return to the original server, resulting in a login count of 2 | ||||||
| Assert.Equal(2, server.PreLoginCount); | ||||||
| Assert.Equal(2, server.PreLoginCount - server.AbandonedPreLoginCount); | ||||||
| } | ||||||
|
|
||||||
| [Theory] | ||||||
|
|
@@ -559,11 +565,14 @@ public void TransientFault_IgnoreServerProvidedFailoverPartner_ShouldConnectToUs | |||||
| // Close the connection to return it to the pool | ||||||
| connection.Close(); | ||||||
|
|
||||||
|
|
||||||
| // Act | ||||||
| // Dispose of the server to trigger a failover | ||||||
| server.Dispose(); | ||||||
|
|
||||||
| // Clear the pool to ensure the next connection attempt doesn't reuse | ||||||
| // the pooled connection to the now-disposed primary server. | ||||||
| SqlConnection.ClearAllPools(); | ||||||
|
||||||
| SqlConnection.ClearAllPools(); | |
| SqlConnection.ClearPool(connection); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test still asserts an exact
server.PreLoginCount == 1, but the flakiness being addressed elsewhere in this PR comes from extra (abandoned) PreLogin attempts caused by interval-timer/TNIR behavior. To keep this deterministic, assert usingAbandonedPreLoginCount/Login7Count(or relax the PreLogin assertion) instead of requiring an exact PreLoginCount.