Fix pool manager conflicts (#6075)

This commit is contained in:
Leon Friedrich
2025-07-12 06:55:49 +12:00
committed by GitHub
parent 4851e913b0
commit efa8975bc6
5 changed files with 49 additions and 7 deletions

View 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;
}

View 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; }
}

View 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;
}

View File

@@ -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);

View File

@@ -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();
}