V10: fix build warnings in test projects (#12509)
* Run code cleanup * Dotnet format benchmarks project * Fix up Test.Common * Run dotnet format + manual cleanup * Run code cleanup for unit tests * Run dotnet format * Fix up errors * Manual cleanup of Unit test project * Update tests/Umbraco.Tests.Benchmarks/HexStringBenchmarks.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update tests/Umbraco.Tests.Integration/Testing/TestDbMeta.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update tests/Umbraco.Tests.Benchmarks/TypeFinderBenchmarks.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update tests/Umbraco.Tests.Integration/Testing/UmbracoIntegrationTest.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update tests/Umbraco.Tests.Integration/Umbraco.Core/Events/EventAggregatorTests.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Fix according to review * Fix after merge * Fix errors Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk> Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> Co-authored-by: Zeegaan <nge@umbraco.dk>
This commit is contained in:
@@ -9,139 +9,130 @@ using System.Data.Common;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Core.Configuration;
|
||||
using Umbraco.Cms.Core.Events;
|
||||
using Umbraco.Cms.Infrastructure.Migrations.Install;
|
||||
using Umbraco.Cms.Infrastructure.Persistence;
|
||||
|
||||
namespace Umbraco.Cms.Tests.Integration.Testing
|
||||
namespace Umbraco.Cms.Tests.Integration.Testing;
|
||||
|
||||
public abstract class BaseTestDatabase
|
||||
{
|
||||
public abstract class BaseTestDatabase
|
||||
protected IUmbracoDatabaseFactory _databaseFactory;
|
||||
|
||||
protected ILoggerFactory _loggerFactory;
|
||||
|
||||
protected BlockingCollection<TestDbMeta> _prepareQueue;
|
||||
protected BlockingCollection<TestDbMeta> _readyEmptyQueue;
|
||||
protected BlockingCollection<TestDbMeta> _readySchemaQueue;
|
||||
protected IList<TestDbMeta> _testDatabases;
|
||||
|
||||
public BaseTestDatabase() => Instance = this;
|
||||
public static BaseTestDatabase Instance { get; private set; }
|
||||
public static bool IsSqlite() => Instance is SqliteTestDatabase;
|
||||
public static bool IsSqlServer() => Instance is SqlServerBaseTestDatabase;
|
||||
|
||||
protected abstract void Initialize();
|
||||
|
||||
public virtual TestDbMeta AttachEmpty()
|
||||
{
|
||||
public static bool IsSqlite() => BaseTestDatabase.Instance is SqliteTestDatabase;
|
||||
public static bool IsSqlServer() => BaseTestDatabase.Instance is SqlServerBaseTestDatabase;
|
||||
|
||||
protected ILoggerFactory _loggerFactory;
|
||||
protected IUmbracoDatabaseFactory _databaseFactory;
|
||||
protected IList<TestDbMeta> _testDatabases;
|
||||
|
||||
protected BlockingCollection<TestDbMeta> _prepareQueue;
|
||||
protected BlockingCollection<TestDbMeta> _readySchemaQueue;
|
||||
protected BlockingCollection<TestDbMeta> _readyEmptyQueue;
|
||||
public static BaseTestDatabase Instance { get; private set; }
|
||||
|
||||
public BaseTestDatabase() => Instance = this;
|
||||
|
||||
protected abstract void Initialize();
|
||||
|
||||
public virtual TestDbMeta AttachEmpty()
|
||||
if (_prepareQueue == null)
|
||||
{
|
||||
if (_prepareQueue == null)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
return _readyEmptyQueue.Take();
|
||||
Initialize();
|
||||
}
|
||||
|
||||
public virtual TestDbMeta AttachSchema()
|
||||
{
|
||||
if (_prepareQueue == null)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
return _readyEmptyQueue.Take();
|
||||
}
|
||||
|
||||
return _readySchemaQueue.Take();
|
||||
public virtual TestDbMeta AttachSchema()
|
||||
{
|
||||
if (_prepareQueue == null)
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
public virtual void Detach(TestDbMeta meta)
|
||||
{
|
||||
_prepareQueue.TryAdd(meta);
|
||||
}
|
||||
return _readySchemaQueue.Take();
|
||||
}
|
||||
|
||||
protected virtual void PrepareDatabase() =>
|
||||
Retry(10, () =>
|
||||
{
|
||||
while (_prepareQueue.IsCompleted == false)
|
||||
{
|
||||
TestDbMeta meta;
|
||||
try
|
||||
{
|
||||
meta = _prepareQueue.Take();
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
ResetTestDatabase(meta);
|
||||
|
||||
if (!meta.IsEmpty)
|
||||
{
|
||||
using (var conn = GetConnection(meta))
|
||||
{
|
||||
conn.Open();
|
||||
using (var cmd = conn.CreateCommand())
|
||||
{
|
||||
RebuildSchema(cmd, meta);
|
||||
}
|
||||
}
|
||||
|
||||
_readySchemaQueue.TryAdd(meta);
|
||||
}
|
||||
else
|
||||
{
|
||||
_readyEmptyQueue.TryAdd(meta);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
protected static void AddParameter(IDbCommand cmd, UmbracoDatabase.ParameterInfo parameterInfo)
|
||||
{
|
||||
IDbDataParameter p = cmd.CreateParameter();
|
||||
p.ParameterName = parameterInfo.Name;
|
||||
p.Value = parameterInfo.Value;
|
||||
p.DbType = parameterInfo.DbType;
|
||||
p.Size = parameterInfo.Size;
|
||||
cmd.Parameters.Add(p);
|
||||
}
|
||||
|
||||
protected abstract DbConnection GetConnection(TestDbMeta meta);
|
||||
|
||||
protected abstract void RebuildSchema(IDbCommand command, TestDbMeta meta);
|
||||
|
||||
protected abstract void ResetTestDatabase(TestDbMeta meta);
|
||||
|
||||
protected static void Retry(int maxIterations, Action action)
|
||||
{
|
||||
for (int i = 0; i < maxIterations; i++)
|
||||
public virtual void Detach(TestDbMeta meta) => _prepareQueue.TryAdd(meta);
|
||||
|
||||
protected virtual void PrepareDatabase() =>
|
||||
Retry(10, () =>
|
||||
{
|
||||
while (_prepareQueue.IsCompleted == false)
|
||||
{
|
||||
TestDbMeta meta;
|
||||
try
|
||||
{
|
||||
action();
|
||||
return;
|
||||
}
|
||||
catch (DbException ex)
|
||||
{
|
||||
// Console.Error.WriteLine($"SqlException occured, but we try again {i+1}/{maxIterations}.\n{e}");
|
||||
// This can occur when there's a transaction deadlock which means (i think) that the database is still in use and hasn't been closed properly yet
|
||||
// so we need to just wait a little bit
|
||||
Thread.Sleep(100 * i);
|
||||
if (i == maxIterations - 1)
|
||||
{
|
||||
Debugger.Launch();
|
||||
throw;
|
||||
}
|
||||
meta = _prepareQueue.Take();
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
// Ignore
|
||||
continue;
|
||||
}
|
||||
|
||||
ResetTestDatabase(meta);
|
||||
|
||||
if (!meta.IsEmpty)
|
||||
{
|
||||
using (var conn = GetConnection(meta))
|
||||
{
|
||||
conn.Open();
|
||||
using (var cmd = conn.CreateCommand())
|
||||
{
|
||||
RebuildSchema(cmd, meta);
|
||||
}
|
||||
}
|
||||
|
||||
_readySchemaQueue.TryAdd(meta);
|
||||
}
|
||||
else
|
||||
{
|
||||
_readyEmptyQueue.TryAdd(meta);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
public abstract void TearDown();
|
||||
protected static void AddParameter(IDbCommand cmd, UmbracoDatabase.ParameterInfo parameterInfo)
|
||||
{
|
||||
var p = cmd.CreateParameter();
|
||||
p.ParameterName = parameterInfo.Name;
|
||||
p.Value = parameterInfo.Value;
|
||||
p.DbType = parameterInfo.DbType;
|
||||
p.Size = parameterInfo.Size;
|
||||
cmd.Parameters.Add(p);
|
||||
}
|
||||
|
||||
protected abstract DbConnection GetConnection(TestDbMeta meta);
|
||||
|
||||
protected abstract void RebuildSchema(IDbCommand command, TestDbMeta meta);
|
||||
|
||||
protected abstract void ResetTestDatabase(TestDbMeta meta);
|
||||
|
||||
protected static void Retry(int maxIterations, Action action)
|
||||
{
|
||||
for (var i = 0; i < maxIterations; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
action();
|
||||
return;
|
||||
}
|
||||
catch (DbException ex)
|
||||
{
|
||||
// Console.Error.WriteLine($"SqlException occured, but we try again {i+1}/{maxIterations}.\n{e}");
|
||||
// This can occur when there's a transaction deadlock which means (i think) that the database is still in use and hasn't been closed properly yet
|
||||
// so we need to just wait a little bit
|
||||
Thread.Sleep(100 * i);
|
||||
if (i == maxIterations - 1)
|
||||
{
|
||||
Debugger.Launch();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void TearDown();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user