1212using System . Threading . Tasks ;
1313using Xunit ;
1414
15+ #nullable enable
16+
1517namespace Microsoft . Data . SqlClient . ManualTesting . Tests
1618{
1719 /// <summary>
@@ -24,7 +26,7 @@ public class ConnectionPoolStressTest
2426 /// <summary>
2527 /// Connection string
2628 /// </summary>
27- internal string ConnectionString { get ; set ; }
29+ internal string ? ConnectionString { get ; set ; }
2830
2931 /// <summary>
3032 /// Maximum number of connections in the pool
@@ -51,9 +53,9 @@ public class ConnectionPoolStressTest
5153 #region Connection Dooming
5254
5355 // Reflection fields for accessing internal connection properties
54- private static readonly FieldInfo _internalConnectionField ;
56+ private readonly FieldInfo ? _internalConnectionField ;
5557
56- static ConnectionPoolStressTest ( )
58+ internal ConnectionPoolStressTest ( )
5759 {
5860 try
5961 {
@@ -70,13 +72,19 @@ static ConnectionPoolStressTest()
7072 /// <summary>
7173 /// Dooms a Microsoft.Data.SqlClient connection by calling DoomThisConnection on its internal connection
7274 /// </summary>
73- private static bool DoomMicrosoftDataConnection ( SqlConnection connection )
75+ private bool DoomMicrosoftDataConnection ( SqlConnection connection )
7476 {
7577 try
7678 {
77- if ( _internalConnectionField ? . GetValue ( connection ) is object internalConnection )
79+ if ( _internalConnectionField == null )
80+ {
81+ // Fail the test if reflection setup failed
82+ return false ;
83+ }
84+
85+ if ( _internalConnectionField . GetValue ( connection ) is object internalConnection )
7886 {
79- MethodInfo doomMethod = internalConnection . GetType ( ) . GetMethod ( "DoomThisConnection" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
87+ MethodInfo ? doomMethod = internalConnection . GetType ( ) . GetMethod ( "DoomThisConnection" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
8088 if ( doomMethod != null )
8189 {
8290 doomMethod . Invoke ( internalConnection , null ) ;
@@ -103,7 +111,7 @@ private static bool DoomMicrosoftDataConnection(SqlConnection connection)
103111 #region Configuration
104112
105113 /// <summary>
106- /// Sets up connection strings for both SQL client libraries with optimized settings
114+ /// Sets up connection string
107115 /// </summary>
108116 /// <param name="connectionString">Connection string to be set.</param>
109117 internal void SetConnectionString ( string connectionString )
@@ -132,6 +140,11 @@ internal void SetConnectionString(string connectionString)
132140 /// </summary>
133141 internal void ConnectionPoolStress_MsData_Sync ( )
134142 {
143+ if ( ConnectionString == null )
144+ {
145+ throw new InvalidOperationException ( "ConnectionString is not set. Call SetConnectionString() before running the test." ) ;
146+ }
147+
135148 RunStressTest (
136149 connectionString : ConnectionString ,
137150 doomAction : conn => DoomMicrosoftDataConnection ( ( SqlConnection ) conn ) ,
@@ -144,6 +157,11 @@ internal void ConnectionPoolStress_MsData_Sync()
144157 /// </summary>
145158 internal void ConnectionPoolStress_MsData_Async ( )
146159 {
160+ if ( ConnectionString == null )
161+ {
162+ throw new InvalidOperationException ( "ConnectionString is not set. Call SetConnectionString() before running the test." ) ;
163+ }
164+
147165 RunStressTest (
148166 connectionString : ConnectionString ,
149167 doomAction : conn => DoomMicrosoftDataConnection ( ( SqlConnection ) conn ) ,
@@ -201,7 +219,7 @@ private Thread CreateWorkerThread(
201219 CountdownEvent countdown ,
202220 bool doomConnections ,
203221 bool async ,
204- Func < DbConnection , bool > doomAction = null )
222+ Func < DbConnection , bool > ? doomAction = null )
205223 {
206224 return new Thread ( async ( ) =>
207225 {
@@ -220,7 +238,9 @@ private Thread CreateWorkerThread(
220238 await conn . OpenAsync ( ) ;
221239 }
222240 else
241+ {
223242 conn . Open ( ) ;
243+ }
224244
225245 await ExecuteCommand ( command , async , conn ) ;
226246
@@ -246,7 +266,9 @@ private Thread CreateWorkerThread(
246266 await conn . OpenAsync ( ) ;
247267 }
248268 else
269+ {
249270 conn . Open ( ) ;
271+ }
250272
251273 await ExecuteCommand ( command , async , conn ) ;
252274
@@ -283,7 +305,9 @@ private static async Task ExecuteCommand(string command, bool async, SqlConnecti
283305 await cmd . ExecuteScalarAsync ( ) ;
284306 }
285307 else
308+ {
286309 cmd . ExecuteScalar ( ) ;
310+ }
287311 }
288312 catch ( Exception ex )
289313 {
@@ -292,7 +316,7 @@ private static async Task ExecuteCommand(string command, bool async, SqlConnecti
292316 }
293317
294318 #endregion
295-
319+
296320 #region Helpers
297321
298322 private static bool RunSingleStressTest ( Action testAction )
@@ -328,7 +352,9 @@ private static async Task<bool> TestConnectionPoolExhaustion(string connectionSt
328352 await conn . OpenAsync ( ) ;
329353 }
330354 else
355+ {
331356 conn . Open ( ) ;
357+ }
332358 connections . Add ( conn ) ;
333359 }
334360 Assert . Equal ( maxPoolSize , connections . Count ) ;
@@ -373,7 +399,7 @@ public async Task ConnectionPoolStress_Sync()
373399 Assert . Fail ( "ConnectionPoolStress_MsData_Sync failed" ) ;
374400 }
375401
376- if ( ! await TestConnectionPoolExhaustion ( test . ConnectionString , test . MaxPoolSize , false ) )
402+ if ( ! await TestConnectionPoolExhaustion ( test . ConnectionString ! , test . MaxPoolSize , false ) )
377403 {
378404 // fail the test
379405 Assert . Fail ( "ConnectionPoolStress_MsData_Sync failed" ) ;
@@ -402,7 +428,7 @@ public async Task ConnectionPoolStress_Async()
402428 }
403429
404430 // Test connection pool exhaustion (async)
405- if ( ! await TestConnectionPoolExhaustion ( test . ConnectionString , test . MaxPoolSize , true ) )
431+ if ( ! await TestConnectionPoolExhaustion ( test . ConnectionString ! , test . MaxPoolSize , true ) )
406432 {
407433 // fail the test
408434 Assert . Fail ( "ConnectionPoolStress_MsData_Async failed" ) ;
0 commit comments