Skip to content

Commit 82e6e1c

Browse files
Fix NH-3023 test (#1501)
* 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.
1 parent 2582efb commit 82e6e1c

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

src/NHibernate.Test/Async/NHSpecificTest/NH3023/DeadlockConnectionPoolIssueTest.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ public async Task ConnectionPoolCorruptionAfterDeadlockAsync(bool distributed, b
102102
_log.Debug("Session enlisted");
103103
try
104104
{
105-
await (new DeadlockHelper().ForceDeadlockOnConnectionAsync((SqlConnection)session.Connection));
105+
await (new DeadlockHelper().ForceDeadlockOnConnectionAsync(
106+
(SqlConnection)session.Connection,
107+
GetConnectionString()));
106108
}
107109
catch (SqlException x)
108110
{
@@ -275,7 +277,7 @@ private static TransactionScope CreateDistributedTransactionScope()
275277

276278
private void RunScript(string script)
277279
{
278-
var cxnString = cfg.Properties["connection.connection_string"] + "; Pooling=No";
280+
var cxnString = GetConnectionString() + "; Pooling=No";
279281
// Disable connection pooling so this won't be hindered by
280282
// problems encountered during the actual test
281283

@@ -300,6 +302,11 @@ private void RunScript(string script)
300302
}
301303
}
302304
}
305+
306+
private string GetConnectionString()
307+
{
308+
return cfg.Properties["connection.connection_string"];
309+
}
303310
}
304311

305312
[TestFixture]

src/NHibernate.Test/Async/NHSpecificTest/NH3023/DeadlockHelper.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
using System.Threading;
1414
using System.Transactions;
1515
using log4net;
16+
using NHibernate.Util;
17+
using NUnit.Framework;
1618

1719
namespace NHibernate.Test.NHSpecificTest.NH3023
1820
{
1921
using System.Threading.Tasks;
2022
public partial class DeadlockHelper
2123
{
2224

23-
public async Task ForceDeadlockOnConnectionAsync(SqlConnection connection, CancellationToken cancellationToken = default(CancellationToken))
25+
public async Task ForceDeadlockOnConnectionAsync(SqlConnection connection, string connectionString, CancellationToken cancellationToken = default(CancellationToken))
2426
{
2527
using (var victimLock = new SemaphoreSlim(0))
2628
using (var winnerLock = new SemaphoreSlim(0))
@@ -37,7 +39,7 @@ public partial class DeadlockHelper
3739
{
3840
using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, TransactionScopeAsyncFlowOption.Enabled))
3941
{
40-
using (var cxn = new SqlConnection(connection.ConnectionString + ";Pooling=No"))
42+
using (var cxn = new SqlConnection(connectionString + ";Pooling=No"))
4143
{
4244
cxn.Open();
4345
DeadlockParticipant(cxn, false, winnerLock, victimLock);
@@ -48,6 +50,7 @@ public partial class DeadlockHelper
4850
catch (Exception ex)
4951
{
5052
winnerEx = ex;
53+
winnerLock.Release();
5154
}
5255
});
5356

@@ -67,6 +70,8 @@ public partial class DeadlockHelper
6770
if (winnerEx != null)
6871
_log.Warn("Winner thread failed", winnerEx);
6972
}
73+
// If getting here, expected victim has not fail. If expected winner has failed instead, fail the test.
74+
Assert.That(winnerEx, Is.Null);
7075

7176
//
7277
// Should never get here

src/NHibernate.Test/NHSpecificTest/NH3023/DeadlockConnectionPoolIssueTest.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ public void ConnectionPoolCorruptionAfterDeadlock(bool distributed, bool dispose
9191
_log.Debug("Session enlisted");
9292
try
9393
{
94-
new DeadlockHelper().ForceDeadlockOnConnection((SqlConnection)session.Connection);
94+
new DeadlockHelper().ForceDeadlockOnConnection(
95+
(SqlConnection)session.Connection,
96+
GetConnectionString());
9597
}
9698
catch (SqlException x)
9799
{
@@ -264,7 +266,7 @@ private static TransactionScope CreateDistributedTransactionScope()
264266

265267
private void RunScript(string script)
266268
{
267-
var cxnString = cfg.Properties["connection.connection_string"] + "; Pooling=No";
269+
var cxnString = GetConnectionString() + "; Pooling=No";
268270
// Disable connection pooling so this won't be hindered by
269271
// problems encountered during the actual test
270272

@@ -289,6 +291,11 @@ private void RunScript(string script)
289291
}
290292
}
291293
}
294+
295+
private string GetConnectionString()
296+
{
297+
return cfg.Properties["connection.connection_string"];
298+
}
292299
}
293300

294301
[TestFixture]

src/NHibernate.Test/NHSpecificTest/NH3023/DeadlockHelper.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
using System.Threading;
44
using System.Transactions;
55
using log4net;
6+
using NHibernate.Util;
7+
using NUnit.Framework;
68

79
namespace NHibernate.Test.NHSpecificTest.NH3023
810
{
911
public partial class DeadlockHelper
1012
{
1113
private static readonly ILog _log = LogManager.GetLogger(typeof(DeadlockHelper));
1214

13-
public void ForceDeadlockOnConnection(SqlConnection connection)
15+
public void ForceDeadlockOnConnection(SqlConnection connection, string connectionString)
1416
{
1517
using (var victimLock = new SemaphoreSlim(0))
1618
using (var winnerLock = new SemaphoreSlim(0))
@@ -27,7 +29,7 @@ public void ForceDeadlockOnConnection(SqlConnection connection)
2729
{
2830
using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))
2931
{
30-
using (var cxn = new SqlConnection(connection.ConnectionString + ";Pooling=No"))
32+
using (var cxn = new SqlConnection(connectionString + ";Pooling=No"))
3133
{
3234
cxn.Open();
3335
DeadlockParticipant(cxn, false, winnerLock, victimLock);
@@ -38,6 +40,7 @@ public void ForceDeadlockOnConnection(SqlConnection connection)
3840
catch (Exception ex)
3941
{
4042
winnerEx = ex;
43+
winnerLock.Release();
4144
}
4245
});
4346

@@ -57,6 +60,8 @@ public void ForceDeadlockOnConnection(SqlConnection connection)
5760
if (winnerEx != null)
5861
_log.Warn("Winner thread failed", winnerEx);
5962
}
63+
// If getting here, expected victim has not fail. If expected winner has failed instead, fail the test.
64+
Assert.That(winnerEx, Is.Null);
6065

6166
//
6267
// Should never get here

0 commit comments

Comments
 (0)