From eac3d480b246da667ac9578f9e64fa7d9d591e27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delaporte?= Date: Fri, 22 Dec 2017 22:31:02 +0100 Subject: [PATCH] Fix NH-3023 test * Cease freezing during 20 minutes in case of winner thread early failure. * Support connection string having a password in it. * Do not silently succeed on failing to setup the winner thread. --- .../NH3023/DeadlockConnectionPoolIssueTest.cs | 11 +++++++++-- .../Async/NHSpecificTest/NH3023/DeadlockHelper.cs | 9 +++++++-- .../NH3023/DeadlockConnectionPoolIssueTest.cs | 11 +++++++++-- .../NHSpecificTest/NH3023/DeadlockHelper.cs | 9 +++++++-- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/NHibernate.Test/Async/NHSpecificTest/NH3023/DeadlockConnectionPoolIssueTest.cs b/src/NHibernate.Test/Async/NHSpecificTest/NH3023/DeadlockConnectionPoolIssueTest.cs index 91325e5a843..75f13d1a623 100644 --- a/src/NHibernate.Test/Async/NHSpecificTest/NH3023/DeadlockConnectionPoolIssueTest.cs +++ b/src/NHibernate.Test/Async/NHSpecificTest/NH3023/DeadlockConnectionPoolIssueTest.cs @@ -102,7 +102,9 @@ public async Task ConnectionPoolCorruptionAfterDeadlockAsync(bool distributed, b _log.Debug("Session enlisted"); try { - await (new DeadlockHelper().ForceDeadlockOnConnectionAsync((SqlConnection)session.Connection)); + await (new DeadlockHelper().ForceDeadlockOnConnectionAsync( + (SqlConnection)session.Connection, + GetConnectionString())); } catch (SqlException x) { @@ -275,7 +277,7 @@ private static TransactionScope CreateDistributedTransactionScope() private void RunScript(string script) { - var cxnString = cfg.Properties["connection.connection_string"] + "; Pooling=No"; + var cxnString = GetConnectionString() + "; Pooling=No"; // Disable connection pooling so this won't be hindered by // problems encountered during the actual test @@ -300,6 +302,11 @@ private void RunScript(string script) } } } + + private string GetConnectionString() + { + return cfg.Properties["connection.connection_string"]; + } } [TestFixture] diff --git a/src/NHibernate.Test/Async/NHSpecificTest/NH3023/DeadlockHelper.cs b/src/NHibernate.Test/Async/NHSpecificTest/NH3023/DeadlockHelper.cs index 8070f052443..7131b8af054 100644 --- a/src/NHibernate.Test/Async/NHSpecificTest/NH3023/DeadlockHelper.cs +++ b/src/NHibernate.Test/Async/NHSpecificTest/NH3023/DeadlockHelper.cs @@ -13,6 +13,8 @@ using System.Threading; using System.Transactions; using log4net; +using NHibernate.Util; +using NUnit.Framework; namespace NHibernate.Test.NHSpecificTest.NH3023 { @@ -20,7 +22,7 @@ namespace NHibernate.Test.NHSpecificTest.NH3023 public partial class DeadlockHelper { - public async Task ForceDeadlockOnConnectionAsync(SqlConnection connection, CancellationToken cancellationToken = default(CancellationToken)) + public async Task ForceDeadlockOnConnectionAsync(SqlConnection connection, string connectionString, CancellationToken cancellationToken = default(CancellationToken)) { using (var victimLock = new SemaphoreSlim(0)) using (var winnerLock = new SemaphoreSlim(0)) @@ -37,7 +39,7 @@ public partial class DeadlockHelper { using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, TransactionScopeAsyncFlowOption.Enabled)) { - using (var cxn = new SqlConnection(connection.ConnectionString + ";Pooling=No")) + using (var cxn = new SqlConnection(connectionString + ";Pooling=No")) { cxn.Open(); DeadlockParticipant(cxn, false, winnerLock, victimLock); @@ -48,6 +50,7 @@ public partial class DeadlockHelper catch (Exception ex) { winnerEx = ex; + winnerLock.Release(); } }); @@ -67,6 +70,8 @@ public partial class DeadlockHelper if (winnerEx != null) _log.Warn("Winner thread failed", winnerEx); } + // If getting here, expected victim has not fail. If expected winner has failed instead, fail the test. + Assert.That(winnerEx, Is.Null); // // Should never get here diff --git a/src/NHibernate.Test/NHSpecificTest/NH3023/DeadlockConnectionPoolIssueTest.cs b/src/NHibernate.Test/NHSpecificTest/NH3023/DeadlockConnectionPoolIssueTest.cs index a1c1464a17d..96b83b0179d 100644 --- a/src/NHibernate.Test/NHSpecificTest/NH3023/DeadlockConnectionPoolIssueTest.cs +++ b/src/NHibernate.Test/NHSpecificTest/NH3023/DeadlockConnectionPoolIssueTest.cs @@ -91,7 +91,9 @@ public void ConnectionPoolCorruptionAfterDeadlock(bool distributed, bool dispose _log.Debug("Session enlisted"); try { - new DeadlockHelper().ForceDeadlockOnConnection((SqlConnection)session.Connection); + new DeadlockHelper().ForceDeadlockOnConnection( + (SqlConnection)session.Connection, + GetConnectionString()); } catch (SqlException x) { @@ -264,7 +266,7 @@ private static TransactionScope CreateDistributedTransactionScope() private void RunScript(string script) { - var cxnString = cfg.Properties["connection.connection_string"] + "; Pooling=No"; + var cxnString = GetConnectionString() + "; Pooling=No"; // Disable connection pooling so this won't be hindered by // problems encountered during the actual test @@ -289,6 +291,11 @@ private void RunScript(string script) } } } + + private string GetConnectionString() + { + return cfg.Properties["connection.connection_string"]; + } } [TestFixture] diff --git a/src/NHibernate.Test/NHSpecificTest/NH3023/DeadlockHelper.cs b/src/NHibernate.Test/NHSpecificTest/NH3023/DeadlockHelper.cs index 9f744994b97..032e6cb5012 100644 --- a/src/NHibernate.Test/NHSpecificTest/NH3023/DeadlockHelper.cs +++ b/src/NHibernate.Test/NHSpecificTest/NH3023/DeadlockHelper.cs @@ -3,6 +3,8 @@ using System.Threading; using System.Transactions; using log4net; +using NHibernate.Util; +using NUnit.Framework; namespace NHibernate.Test.NHSpecificTest.NH3023 { @@ -10,7 +12,7 @@ public partial class DeadlockHelper { private static readonly ILog _log = LogManager.GetLogger(typeof(DeadlockHelper)); - public void ForceDeadlockOnConnection(SqlConnection connection) + public void ForceDeadlockOnConnection(SqlConnection connection, string connectionString) { using (var victimLock = new SemaphoreSlim(0)) using (var winnerLock = new SemaphoreSlim(0)) @@ -27,7 +29,7 @@ public void ForceDeadlockOnConnection(SqlConnection connection) { using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew)) { - using (var cxn = new SqlConnection(connection.ConnectionString + ";Pooling=No")) + using (var cxn = new SqlConnection(connectionString + ";Pooling=No")) { cxn.Open(); DeadlockParticipant(cxn, false, winnerLock, victimLock); @@ -38,6 +40,7 @@ public void ForceDeadlockOnConnection(SqlConnection connection) catch (Exception ex) { winnerEx = ex; + winnerLock.Release(); } }); @@ -57,6 +60,8 @@ public void ForceDeadlockOnConnection(SqlConnection connection) if (winnerEx != null) _log.Warn("Winner thread failed", winnerEx); } + // If getting here, expected victim has not fail. If expected winner has failed instead, fail the test. + Assert.That(winnerEx, Is.Null); // // Should never get here