Skip to content

Commit 1ed3411

Browse files
committed
Add failover and prelogin count capabilities to test server. Update test case.
1 parent 46bbba8 commit 1ed3411

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionBasicTests.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Configuration;
67
using System.Data;
78
using System.Data.Common;
89
using System.Reflection;
@@ -16,6 +17,13 @@ namespace Microsoft.Data.SqlClient.Tests
1617
{
1718
public class SqlConnectionBasicTests
1819
{
20+
// Reflection
21+
public static Assembly systemData = Assembly.GetAssembly(typeof(SqlConnection));
22+
public static Type sqlConnection = systemData.GetType("Microsoft.Data.SqlClient.SqlConnection");
23+
public static PropertyInfo innerConnectionProperty = sqlConnection.GetProperty("InnerConnection", BindingFlags.NonPublic | BindingFlags.Instance);
24+
public static Type sqlInternalConnectionTds = systemData.GetType("Microsoft.Data.SqlClient.SqlInternalConnectionTds");
25+
public static PropertyInfo serverProvidedFailoverPartnerProperty = sqlInternalConnectionTds.GetProperty("ServerProvidedFailoverPartner", BindingFlags.NonPublic | BindingFlags.Instance);
26+
1927
[Fact]
2028
public void ConnectionTest()
2129
{
@@ -213,18 +221,18 @@ public void ConnectionTestValidCredentialCombination()
213221
public void TransientFault_IgnoreServerProvidedFailoverPartner_ShouldConnectToUserProvidedPartner()
214222
{
215223
// Arrange
216-
AppContext.SetSwitch("Microsoft.Data.SqlClient.IgnoreServerProvidedFailoverPartner", true);
224+
AppContext.SetSwitch("Switch.Microsoft.Data.SqlClient.IgnoreServerProvidedFailoverPartner", true);
217225

218226
using TestTdsServer failoverServer = TestTdsServer.StartTestServer();
219227
// Doesn't need to point to a real endpoint, just needs a value specified
220-
//TODO: FailoverPartner = "localhost,1234",
228+
failoverServer.Arguments.FailoverPartner = "localhost,1234";
221229

222230
var failoverBuilder = new SqlConnectionStringBuilder(failoverServer.ConnectionString);
223231

224232
using TestTdsServer server = TestTdsServer.StartTestServer();
225-
// Set an invalid failover partner to ensure that the connection fails if the
226-
// server provided failover partner is used.
227-
// TODO: FailoverPartner = $"invalidhost",
233+
// Set an invalid failover partner to ensure that the connection fails if the
234+
// server provided failover partner is used.
235+
server.Arguments.FailoverPartner = $"invalidhost";
228236

229237
SqlConnectionStringBuilder builder = new(server.ConnectionString)
230238
{
@@ -234,18 +242,20 @@ public void TransientFault_IgnoreServerProvidedFailoverPartner_ShouldConnectToUs
234242
// Ensure pooling is enabled so that the failover partner information
235243
// is persisted in the pool group. If pooling is disabled, the server
236244
// provided failover partner will never be used.
237-
Pooling = true
245+
Pooling = true,
238246
};
239247
SqlConnection connection = new(builder.ConnectionString);
240248

241249
// Connect once to the primary to trigger it to send the failover partner
242250
connection.Open();
243-
Assert.Equal("invalidhost", (connection.InnerConnection as SqlInternalConnectionTds)!.ServerProvidedFailoverPartner);
251+
252+
var innerConnection = innerConnectionProperty.GetValue(connection);
253+
var serverProvidedFailoverPartner = serverProvidedFailoverPartnerProperty.GetValue(innerConnection);
254+
Assert.Equal("invalidhost", serverProvidedFailoverPartner);
244255

245256
// Close the connection to return it to the pool
246257
connection.Close();
247258

248-
249259
// Act
250260
// Dispose of the server to trigger a failover
251261
server.Dispose();

src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/GenericTDSServer.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,26 @@ public class GenericTDSServer : ITDSServer
3232
/// </summary>
3333
private int _sessionCount = 0;
3434

35+
/// <summary>
36+
/// Counts pre-login requests to the server.
37+
/// </summary>
38+
private int _preLoginCount = 0;
39+
3540
/// <summary>
3641
/// Server configuration
3742
/// </summary>
38-
protected TDSServerArguments Arguments { get; set; }
43+
public TDSServerArguments Arguments { get; set; }
3944

4045
/// <summary>
4146
/// Query engine instance
4247
/// </summary>
4348
protected QueryEngine Engine { get; set; }
4449

50+
/// <summary>
51+
/// Counts pre-login requests to the server.
52+
/// </summary>
53+
public int PreLoginCount => _preLoginCount;
54+
4555
/// <summary>
4656
/// Default constructor
4757
/// </summary>
@@ -104,6 +114,8 @@ public virtual void CloseSession(ITDSServerSession session)
104114
/// </summary>
105115
public virtual TDSMessageCollection OnPreLoginRequest(ITDSServerSession session, TDSMessage request)
106116
{
117+
Interlocked.Increment(ref _preLoginCount);
118+
107119
// Inflate pre-login request from the message
108120
TDSPreLoginToken preLoginRequest = request[0] as TDSPreLoginToken;
109121

@@ -544,6 +556,16 @@ protected virtual TDSMessageCollection OnAuthenticationCompleted(ITDSServerSessi
544556
responseMessage.Add(featureExtActToken);
545557
}
546558

559+
if (!string.IsNullOrEmpty(Arguments.FailoverPartner))
560+
{
561+
envChange = new TDSEnvChangeToken(TDSEnvChangeTokenType.RealTimeLogShipping, Arguments.FailoverPartner);
562+
563+
// Log response
564+
TDSUtilities.Log(Arguments.Log, "Response", envChange);
565+
566+
responseMessage.Add(envChange);
567+
}
568+
547569
// Create DONE token
548570
TDSDoneToken doneToken = new TDSDoneToken(TDSDoneTokenStatusType.Final);
549571

src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS.Servers/TDSServerArguments.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public class TDSServerArguments
6969
/// </summary>
7070
public X509Certificate EncryptionCertificate { get; set; }
7171

72+
/// <summary>
73+
/// Specifies the failover partner server name and port
74+
/// </summary>
75+
public string FailoverPartner { get; set; } = string.Empty;
76+
7277
/// <summary>
7378
/// Initialization constructor
7479
/// </summary>

0 commit comments

Comments
 (0)