Skip to content

Fix NH-3023 test #1501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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

Expand All @@ -300,6 +302,11 @@ private void RunScript(string script)
}
}
}

private string GetConnectionString()
{
return cfg.Properties["connection.connection_string"];
}
}

[TestFixture]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
using System.Threading;
using System.Transactions;
using log4net;
using NHibernate.Util;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3023
{
using System.Threading.Tasks;
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))
Expand All @@ -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);
Expand All @@ -48,6 +50,7 @@ public partial class DeadlockHelper
catch (Exception ex)
{
winnerEx = ex;
winnerLock.Release();
}
});

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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

Expand All @@ -289,6 +291,11 @@ private void RunScript(string script)
}
}
}

private string GetConnectionString()
{
return cfg.Properties["connection.connection_string"];
}
}

[TestFixture]
Expand Down
9 changes: 7 additions & 2 deletions src/NHibernate.Test/NHSpecificTest/NH3023/DeadlockHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
using System.Threading;
using System.Transactions;
using log4net;
using NHibernate.Util;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.NH3023
{
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))
Expand All @@ -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"))
Copy link
Member Author

@fredericDelaporte fredericDelaporte Dec 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

connection.ConnectionString is purged from password, when there is one. This causes the winner thread to fail in such case.

{
cxn.Open();
DeadlockParticipant(cxn, false, winnerLock, victimLock);
Expand All @@ -38,6 +40,7 @@ public void ForceDeadlockOnConnection(SqlConnection connection)
catch (Exception ex)
{
winnerEx = ex;
winnerLock.Release();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of early failure of the winner thread, the "loser thread" was stuck for 2 minutes. Better release him.

}
});

Expand All @@ -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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of winner thread failure while loser thread does not fail, the test was "succeeding". Prevent this.


//
// Should never get here
Expand Down