mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-14 19:29:36 +01:00
Fix pool manager conflicts (#6075)
This commit is contained in:
12
Robust.UnitTesting/Pool/ExternalTestContext.cs
Normal file
12
Robust.UnitTesting/Pool/ExternalTestContext.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.IO;
|
||||
|
||||
namespace Robust.UnitTesting.Pool;
|
||||
|
||||
/// <summary>
|
||||
/// Generic implementation of <see cref="ITestContextLike"/> for usage outside of actual tests.
|
||||
/// </summary>
|
||||
public sealed class ExternalTestContext(string name, TextWriter writer) : ITestContextLike
|
||||
{
|
||||
public string FullName => name;
|
||||
public TextWriter Out => writer;
|
||||
}
|
||||
14
Robust.UnitTesting/Pool/ITestContextLike.cs
Normal file
14
Robust.UnitTesting/Pool/ITestContextLike.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System.IO;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Robust.UnitTesting.Pool;
|
||||
|
||||
/// <summary>
|
||||
/// Something that looks like a <see cref="TestContext"/>, for passing to integration tests.
|
||||
/// </summary>
|
||||
public interface ITestContextLike
|
||||
{
|
||||
string FullName { get; }
|
||||
TextWriter Out { get; }
|
||||
}
|
||||
|
||||
13
Robust.UnitTesting/Pool/NUnitTestContextWrap.cs
Normal file
13
Robust.UnitTesting/Pool/NUnitTestContextWrap.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.IO;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Robust.UnitTesting.Pool;
|
||||
|
||||
/// <summary>
|
||||
/// Canonical implementation of <see cref="ITestContextLike"/> for usage in actual NUnit tests.
|
||||
/// </summary>
|
||||
public sealed class NUnitTestContextWrap(TestContext context, TextWriter writer) : ITestContextLike
|
||||
{
|
||||
public string FullName => context.Test.FullName;
|
||||
public TextWriter Out => writer;
|
||||
}
|
||||
@@ -113,9 +113,9 @@ public class PoolManager<TPair> : BasePoolManager where TPair : class, ITestPair
|
||||
TestPrototypes.Clear();
|
||||
}
|
||||
|
||||
protected virtual string GetDefaultTestName(TestContext testContext)
|
||||
protected virtual string GetDefaultTestName(ITestContextLike testContext)
|
||||
{
|
||||
return testContext.Test.FullName.Replace("Robust.UnitTesting.", "");
|
||||
return testContext.FullName.Replace("Robust.UnitTesting.", "");
|
||||
}
|
||||
|
||||
public string DeathReport()
|
||||
@@ -140,16 +140,17 @@ public class PoolManager<TPair> : BasePoolManager where TPair : class, ITestPair
|
||||
|
||||
public virtual PairSettings DefaultSettings => new();
|
||||
|
||||
public async Task<TPair> GetPair(PairSettings? settings = null)
|
||||
public async Task<TPair> GetPair(
|
||||
PairSettings? settings = null,
|
||||
ITestContextLike? testContext = null)
|
||||
{
|
||||
if (!_initialized)
|
||||
throw new InvalidOperationException($"Pool manager has not been initialized");
|
||||
|
||||
settings ??= DefaultSettings;
|
||||
|
||||
// Trust issues with the AsyncLocal that backs this.
|
||||
var testContext = TestContext.CurrentContext;
|
||||
var testOut = TestContext.Out;
|
||||
testContext ??= new NUnitTestContextWrap(TestContext.CurrentContext, TestContext.Out);
|
||||
var testOut = testContext.Out;
|
||||
|
||||
DieIfPoolFailure();
|
||||
var currentTestName = settings.TestName ?? GetDefaultTestName(testContext);
|
||||
|
||||
@@ -8,6 +8,7 @@ using Robust.Shared;
|
||||
using Robust.Shared.Exceptions;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Robust.UnitTesting.Pool;
|
||||
|
||||
@@ -79,6 +80,7 @@ public partial class TestPair<TServer, TClient>
|
||||
|
||||
var returnTime = Watch.Elapsed;
|
||||
await TestOut.WriteLineAsync($"{nameof(CleanReturnAsync)}: PoolManager took {returnTime.TotalMilliseconds} ms to put pair {Id} back into the pool");
|
||||
State = PairState.Ready;
|
||||
}
|
||||
|
||||
public async ValueTask CleanReturnAsync()
|
||||
@@ -89,7 +91,7 @@ public partial class TestPair<TServer, TClient>
|
||||
await TestOut.WriteLineAsync($"{nameof(CleanReturnAsync)}: Return of pair {Id} started");
|
||||
State = PairState.CleanDisposed;
|
||||
await OnCleanDispose();
|
||||
State = PairState.Ready;
|
||||
DebugTools.Assert(State is PairState.Dead or PairState.Ready);
|
||||
Manager.Return(this);
|
||||
ClearContext();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user