33// See the LICENSE file in the project root for more information.
44
55using System ;
6+ using System . Collections . Generic ;
67using System . Data ;
78using System . Threading ;
89using System . Threading . Tasks ;
@@ -39,6 +40,13 @@ public sealed class DatabaseContextReconnectionTest : IDisposable
3940 /// </summary>
4041 private readonly string _baseConnectionString ;
4142
43+ /// <summary>
44+ /// Tracks tables created during a test so they can be cleaned up from
45+ /// the initial catalog if a test failure causes them to land in the
46+ /// wrong database.
47+ /// </summary>
48+ private readonly List < string > _createdTableNames = new ( ) ;
49+
4250 public DatabaseContextReconnectionTest ( )
4351 {
4452 _baseConnectionString = DataTestUtility . TCPConnectionString ;
@@ -55,6 +63,37 @@ public void Dispose()
5563 {
5664 using SqlConnection conn = new ( _baseConnectionString ) ;
5765 conn . Open ( ) ;
66+
67+ // Clean up any tables that may have been created in the initial
68+ // catalog if the database context bug caused DDL to execute in the
69+ // wrong database.
70+ if ( _createdTableNames . Count > 0 )
71+ {
72+ string initialCatalog = new SqlConnectionStringBuilder (
73+ _baseConnectionString ) . InitialCatalog ;
74+
75+ if ( ! string . IsNullOrEmpty ( initialCatalog )
76+ && ! string . Equals ( initialCatalog , _tempDbName ,
77+ StringComparison . OrdinalIgnoreCase ) )
78+ {
79+ foreach ( string tableName in _createdTableNames )
80+ {
81+ try
82+ {
83+ using SqlCommand cmd = conn . CreateCommand ( ) ;
84+ cmd . CommandText =
85+ $ "IF OBJECT_ID(N'[{ initialCatalog } ].dbo.[{ tableName } ]') " +
86+ $ "IS NOT NULL DROP TABLE [{ initialCatalog } ].dbo.[{ tableName } ]";
87+ cmd . ExecuteNonQuery ( ) ;
88+ }
89+ catch
90+ {
91+ // Best-effort cleanup
92+ }
93+ }
94+ }
95+ }
96+
5897 DataTestUtility . DropDatabase ( conn , _tempDbName ) ;
5998 }
6099
@@ -153,8 +192,7 @@ private static Guid GetConnectionId(SqlConnection conn)
153192 /// </summary>
154193 [ ConditionalFact ( typeof ( DataTestUtility ) ,
155194 nameof ( DataTestUtility . AreConnStringsSetup ) ,
156- nameof ( DataTestUtility . IsNotAzureSynapse ) ,
157- nameof ( DataTestUtility . IsNotManagedInstance ) ) ]
195+ nameof ( DataTestUtility . IsNotAzureServer ) ) ]
158196 public void UseDatabase_KillReconnect_PreservesContext ( )
159197 {
160198 AppContext . SetSwitch ( SwitchName , false ) ;
@@ -185,8 +223,7 @@ public void UseDatabase_KillReconnect_PreservesContext()
185223 /// </summary>
186224 [ ConditionalFact ( typeof ( DataTestUtility ) ,
187225 nameof ( DataTestUtility . AreConnStringsSetup ) ,
188- nameof ( DataTestUtility . IsNotAzureSynapse ) ,
189- nameof ( DataTestUtility . IsNotManagedInstance ) ) ]
226+ nameof ( DataTestUtility . IsNotAzureServer ) ) ]
190227 public void ChangeDatabase_KillReconnect_PreservesContext ( )
191228 {
192229 AppContext . SetSwitch ( SwitchName , false ) ;
@@ -213,8 +250,7 @@ public void ChangeDatabase_KillReconnect_PreservesContext()
213250 /// </summary>
214251 [ ConditionalFact ( typeof ( DataTestUtility ) ,
215252 nameof ( DataTestUtility . AreConnStringsSetup ) ,
216- nameof ( DataTestUtility . IsNotAzureSynapse ) ,
217- nameof ( DataTestUtility . IsNotManagedInstance ) ) ]
253+ nameof ( DataTestUtility . IsNotAzureServer ) ) ]
218254 public void UseDatabase_KillReconnect_Pooled_PreservesContext ( )
219255 {
220256 AppContext . SetSwitch ( SwitchName , false ) ;
@@ -246,8 +282,7 @@ public void UseDatabase_KillReconnect_Pooled_PreservesContext()
246282 /// </summary>
247283 [ ConditionalFact ( typeof ( DataTestUtility ) ,
248284 nameof ( DataTestUtility . AreConnStringsSetup ) ,
249- nameof ( DataTestUtility . IsNotAzureSynapse ) ,
250- nameof ( DataTestUtility . IsNotManagedInstance ) ) ]
285+ nameof ( DataTestUtility . IsNotAzureServer ) ) ]
251286 public void UseDatabase_KillReconnect_MARS_PreservesContext ( )
252287 {
253288 AppContext . SetSwitch ( SwitchName , false ) ;
@@ -281,8 +316,7 @@ public void UseDatabase_KillReconnect_MARS_PreservesContext()
281316 /// </summary>
282317 [ ConditionalFact ( typeof ( DataTestUtility ) ,
283318 nameof ( DataTestUtility . AreConnStringsSetup ) ,
284- nameof ( DataTestUtility . IsNotAzureSynapse ) ,
285- nameof ( DataTestUtility . IsNotManagedInstance ) ) ]
319+ nameof ( DataTestUtility . IsNotAzureServer ) ) ]
286320 public void UseDatabase_KillReconnect_StressLoop_PreservesContext ( )
287321 {
288322 AppContext . SetSwitch ( SwitchName , false ) ;
@@ -320,8 +354,7 @@ public void UseDatabase_KillReconnect_StressLoop_PreservesContext()
320354 /// </summary>
321355 [ ConditionalFact ( typeof ( DataTestUtility ) ,
322356 nameof ( DataTestUtility . AreConnStringsSetup ) ,
323- nameof ( DataTestUtility . IsNotAzureSynapse ) ,
324- nameof ( DataTestUtility . IsNotManagedInstance ) ) ]
357+ nameof ( DataTestUtility . IsNotAzureServer ) ) ]
325358 public void ChangeDatabase_KillReconnect_StressLoop_PreservesContext ( )
326359 {
327360 AppContext . SetSwitch ( SwitchName , false ) ;
@@ -360,14 +393,14 @@ public void ChangeDatabase_KillReconnect_StressLoop_PreservesContext()
360393 /// </summary>
361394 [ ConditionalFact ( typeof ( DataTestUtility ) ,
362395 nameof ( DataTestUtility . AreConnStringsSetup ) ,
363- nameof ( DataTestUtility . IsNotAzureSynapse ) ,
364- nameof ( DataTestUtility . IsNotManagedInstance ) ) ]
396+ nameof ( DataTestUtility . IsNotAzureServer ) ) ]
365397 public void UseDatabase_KillReconnect_CreateTable_LandsInCorrectDb ( )
366398 {
367399 AppContext . SetSwitch ( SwitchName , false ) ;
368400
369401 var builder = BuildConnectionString ( pooling : false ) ;
370402 string tableName = "tbl_ctx_" + Guid . NewGuid ( ) . ToString ( "N" ) . Substring ( 0 , 8 ) ;
403+ _createdTableNames . Add ( tableName ) ;
371404
372405 using SqlConnection conn = new ( builder . ConnectionString ) ;
373406 conn . Open ( ) ;
@@ -436,8 +469,7 @@ public void UseDatabase_KillReconnect_CreateTable_LandsInCorrectDb()
436469 /// </summary>
437470 [ ConditionalFact ( typeof ( DataTestUtility ) ,
438471 nameof ( DataTestUtility . AreConnStringsSetup ) ,
439- nameof ( DataTestUtility . IsNotAzureSynapse ) ,
440- nameof ( DataTestUtility . IsNotManagedInstance ) ) ]
472+ nameof ( DataTestUtility . IsNotAzureServer ) ) ]
441473 public void UseDatabase_KillReconnect_StressCreateTables_LandInCorrectDb ( )
442474 {
443475 AppContext . SetSwitch ( SwitchName , false ) ;
@@ -494,6 +526,7 @@ public void UseDatabase_KillReconnect_StressCreateTables_LandInCorrectDb()
494526 // Reconnection happens here — create a table
495527 string tableName = $ "tbl_s{ i } _{ Guid . NewGuid ( ) . ToString ( "N" ) . Substring ( 0 , 6 ) } ";
496528 tableNames [ i ] = tableName ;
529+ _createdTableNames . Add ( tableName ) ;
497530
498531 using ( SqlCommand createCmd = new (
499532 $ "CREATE TABLE [{ tableName } ] (Id INT)", conn ) )
@@ -548,8 +581,7 @@ public void UseDatabase_KillReconnect_StressCreateTables_LandInCorrectDb()
548581 /// </summary>
549582 [ ConditionalFact ( typeof ( DataTestUtility ) ,
550583 nameof ( DataTestUtility . AreConnStringsSetup ) ,
551- nameof ( DataTestUtility . IsNotAzureSynapse ) ,
552- nameof ( DataTestUtility . IsNotManagedInstance ) ) ]
584+ nameof ( DataTestUtility . IsNotAzureServer ) ) ]
553585 public void MultipleDatabaseSwitches_KillReconnect_LastSwitchWins ( )
554586 {
555587 AppContext . SetSwitch ( SwitchName , false ) ;
@@ -558,6 +590,7 @@ public void MultipleDatabaseSwitches_KillReconnect_LastSwitchWins()
558590 string initialCatalog = new SqlConnectionStringBuilder (
559591 _baseConnectionString ) . InitialCatalog ;
560592 string tableName = "tbl_multi_" + Guid . NewGuid ( ) . ToString ( "N" ) . Substring ( 0 , 8 ) ;
593+ _createdTableNames . Add ( tableName ) ;
561594
562595 using SqlConnection conn = new ( builder . ConnectionString ) ;
563596 conn . Open ( ) ;
@@ -621,15 +654,15 @@ public void MultipleDatabaseSwitches_KillReconnect_LastSwitchWins()
621654 /// </summary>
622655 [ ConditionalFact ( typeof ( DataTestUtility ) ,
623656 nameof ( DataTestUtility . AreConnStringsSetup ) ,
624- nameof ( DataTestUtility . IsNotAzureSynapse ) ,
625- nameof ( DataTestUtility . IsNotManagedInstance ) ) ]
657+ nameof ( DataTestUtility . IsNotAzureServer ) ) ]
626658 public void UseDatabase_DoubleKill_CreateTable_LandsInCorrectDb ( )
627659 {
628660 AppContext . SetSwitch ( SwitchName , false ) ;
629661
630662 var builder = BuildConnectionString ( pooling : false ) ;
631663 builder . ConnectRetryCount = 3 ; // Need extra retries for double kill
632664 string tableName = "tbl_dblkill_" + Guid . NewGuid ( ) . ToString ( "N" ) . Substring ( 0 , 8 ) ;
665+ _createdTableNames . Add ( tableName ) ;
633666
634667 using SqlConnection conn = new ( builder . ConnectionString ) ;
635668 conn . Open ( ) ;
@@ -678,14 +711,14 @@ public void UseDatabase_DoubleKill_CreateTable_LandsInCorrectDb()
678711 /// </summary>
679712 [ ConditionalFact ( typeof ( DataTestUtility ) ,
680713 nameof ( DataTestUtility . AreConnStringsSetup ) ,
681- nameof ( DataTestUtility . IsNotAzureSynapse ) ,
682- nameof ( DataTestUtility . IsNotManagedInstance ) ) ]
714+ nameof ( DataTestUtility . IsNotAzureServer ) ) ]
683715 public async Task UseDatabase_KillReconnect_Async_CreateTable_LandsInCorrectDb ( )
684716 {
685717 AppContext . SetSwitch ( SwitchName , false ) ;
686718
687719 var builder = BuildConnectionString ( pooling : false ) ;
688720 string tableName = "tbl_async_" + Guid . NewGuid ( ) . ToString ( "N" ) . Substring ( 0 , 8 ) ;
721+ _createdTableNames . Add ( tableName ) ;
689722
690723 using SqlConnection conn = new ( builder . ConnectionString ) ;
691724 await conn . OpenAsync ( ) ;
0 commit comments