From 9828af2bd542f6f98ec5ffe95d4870bf484039e9 Mon Sep 17 00:00:00 2001 From: Malcolm Daigle Date: Wed, 26 Feb 2025 10:35:40 -0800 Subject: [PATCH] Improve DTC test stability. --- .../DistributedTransactionTest.Windows.cs | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/TransactionTest/DistributedTransactionTest.Windows.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/TransactionTest/DistributedTransactionTest.Windows.cs index 9cda941762..5719130cf9 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/TransactionTest/DistributedTransactionTest.Windows.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/TransactionTest/DistributedTransactionTest.Windows.cs @@ -108,25 +108,21 @@ private void EnlistedTransactionPreservedWhilePooled() transactionException = ex; } - if (Utils.IsAzureSqlServer(new SqlConnectionStringBuilder((ConnectionString)).DataSource)) - { - // Even if an application swallows the command exception, completing the transaction should indicate that it failed. - Assert.IsType(transactionException); - // See https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors-3000-to-3999?view=sql-server-ver16 - // Error 3971 corresponds to "The server failed to resume the transaction." - Assert.Equal(3971, ((SqlException)commandException).Number); - } - else - { - Assert.IsType(transactionException); + // Even if an application swallows the command exception, completing the transaction should indicate that it failed. + var expectedTransactionExceptions = new[] { typeof(TransactionAbortedException), typeof(TransactionInDoubtException) }; + Assert.Contains(transactionException.GetType(), expectedTransactionExceptions); -#if NETFRAMEWORK + var expectedCommandExceptions = new[] { typeof(SqlException), typeof(InvalidOperationException) }; + Assert.Contains(commandException.GetType(), expectedCommandExceptions); + + if (commandException is SqlException) + { // See https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors-8000-to-8999?view=sql-server-ver16 // The distributed transaction failed - Assert.Equal(8525, ((SqlException)commandException).Number); -#else - Assert.IsType(commandException); -#endif + // See https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors-3000-to-3999?view=sql-server-ver16 + // Error 3971 corresponds to "The server failed to resume the transaction." + var expectedExceptionCodes = new[] { 3971, 8525 }; + Assert.Contains(((SqlException)commandException).Number, expectedExceptionCodes); } // Verify that nothing made it into the database