Fixing tests

This commit is contained in:
Shannon
2021-03-08 12:31:17 +11:00
parent 1094ae3723
commit 455181dbb4
2 changed files with 40 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
@@ -263,10 +263,22 @@ where tbl.[name]=@0 and col.[name]=@1;", tableName, columnName)
public void WriteLock(IDatabase db, TimeSpan timeout, params int[] lockIds)
{
if (db is null)
{
throw new ArgumentNullException(nameof(db));
}
if (db.Transaction is null)
{
throw new ArgumentException(nameof(db) + "." + nameof(db.Transaction) + " is null");
}
// soon as we get Database, a transaction is started
if (db.Transaction.IsolationLevel < IsolationLevel.ReadCommitted)
{
throw new InvalidOperationException("A transaction with minimum ReadCommitted isolation level is required.");
}
// *not* using a unique 'WHERE IN' query here because the *order* of lockIds is important to avoid deadlocks
@@ -275,24 +287,40 @@ where tbl.[name]=@0 and col.[name]=@1;", tableName, columnName)
db.Execute($"SET LOCK_TIMEOUT {timeout.TotalMilliseconds};");
var i = db.Execute(@"UPDATE umbracoLock WITH (REPEATABLEREAD) SET value = (CASE WHEN (value=1) THEN -1 ELSE 1 END) WHERE id=@id", new { id = lockId });
if (i == 0) // ensure we are actually locking!
{
throw new ArgumentException($"LockObject with id={lockId} does not exist.");
}
}
}
public override void ReadLock(IDatabase db, params int[] lockIds)
{
if (db is null)
{
throw new ArgumentNullException(nameof(db));
}
if (db.Transaction is null)
{
throw new ArgumentException(nameof(db) + "." + nameof(db.Transaction) + " is null");
}
// soon as we get Database, a transaction is started
if (db.Transaction.IsolationLevel < IsolationLevel.ReadCommitted)
{
throw new InvalidOperationException("A transaction with minimum ReadCommitted isolation level is required.");
}
// *not* using a unique 'WHERE IN' query here because the *order* of lockIds is important to avoid deadlocks
foreach (var lockId in lockIds)
{
var i = db.ExecuteScalar<int?>("SELECT value FROM umbracoLock WITH (REPEATABLEREAD) WHERE id=@id", new { id = lockId });
if (i == null) // ensure we are actually locking!
{
throw new ArgumentException($"LockObject with id={lockId} does not exist.", nameof(lockIds));
}
}
}

View File

@@ -147,9 +147,12 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services
threads.Add(t);
}
// start all threads
log.LogInformation("Starting threads");
threads.ForEach(x => x.Start());
using (ExecutionContext.SuppressFlow())
{
// start all threads
log.LogInformation("Starting threads");
threads.ForEach(x => x.Start());
}
// wait for all to complete
log.LogInformation("Joining threads");
@@ -221,8 +224,11 @@ namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Services
threads.Add(t);
}
// start all threads
threads.ForEach(x => x.Start());
using (ExecutionContext.SuppressFlow())
{
// start all threads
threads.ForEach(x => x.Start());
}
// wait for all to complete
threads.ForEach(x => x.Join());