test: fix shared state, CI-vacuous tests and harness duplication (#2722)

Follow-up to #2721. It fixes how the remaining tests behave and changes no production code.

## CI
- Both `dotnet test` steps pass `--filter "Category!=Manual"`. Before this, Manual-tagged tests ran on CI anyway.
- The one surviving movement-throttle latency test is the only coverage of burst forgiveness over a full, wrapped history. It loses its Manual tag, so it runs on CI.

## Tests that passed on CI without testing anything
- **Corpse and throwing tests.** They relied on tiledata for the weapon `Layer`, so on CI the equip silently failed and they asserted against empty lists. They now set `Layer` explicitly and assert the equip took effect. `TestThrown` also pins its own strength and range values, so a shard retuning SoulGlaive no longer breaks them.
- **Healthbar poison rows.** Server.Tests registers no poisons, so every row tested `null`. They now use real poison levels.
- **Account packet tests.** The mock accounts reported `Length` 0, so the character-list tests only ever encoded empty slots. They now encode the real characters.
- **Pathing and AI tests that only mean something on real Trammel/Felucca terrain** are now guarded with `TileDataRequirement.SkipIfMissing()`: Britain desk and clutter scenes, house footprints (no MultiData on CI) and fingerprints. Tests that are valid on flat blank terrain stay unguarded. (A synthetic tiledata fixture to run these on CI is a planned follow-up.)
- **Assertions that couldn't fail now check their claim.** Affected tests include the `DefaultWalker` cache use, cache on/off parity requiring a path, the recorder capability flags, the idle-pet gate, the monthly DST recurrence, the AdvancedSearch `Record.Exception` wrappers, `CanFitItem` with no branch-guarded assert, the Firewall callback, and `Import_NoMap`. The Geometry Parse asserts also now use the same input as their TryParse.

## Shared state
Every test that changes process-wide state now restores it, even when an assert fails:
- **The clock.** A new `TimerWheelScope` saves and restores `Core._tickCount`, `Core._now` and the timer wheel. `SimulatedClockScope` covers decay tests.
- **StepCache settings and caches**, via `StepCacheStateScope`.
- **Network state.** The ManualAllowlist, LoginAllowlist, firewall and BanChannel reporters are restored. Global packet handlers are now registered per test and removed afterwards, so they no longer stay registered for later tests.
- **Data and config tables.** `ArchiveJournal` statics, `TileData.ItemTable`, `ObjectPropertyList.Enabled`, `Core.Expansion` and the localization table are restored. One `MobileStatusVersion` restore was resetting a different expansion than the one the test changed.
- **Entities.** Tests with fixed serials now delete what they create. Fixed mobile serials moved to `World.NewMobile`, because deleting a hard-coded serial could unregister a live entity.
- **Collection race.** `SpiralScanTests` no longer clears the spawn cache from a separate collection that could run in parallel with the cache-manager tests.
- **Shared statics.** Classes reading shared static allowlists joined the sequential collection.

## Duplication
New shared helpers replace copy-pasted harness code:
- **Server.Tests:** `PacketAssert.Sends`, used by about 130 packet tests; `TestClientHarness`, `PersistencePipelineScope`, `NetTestHelpers`, `EntityCleanup` and one `TestMulti`.
- **UOContent.Tests:** `TimerWheelScope`, which also holds one wraparound-safe `RunFor`/`RunUntil`; `AiTestStubs`, which merges six walker copies and five identical creature stubs; `MobileTestHarness`, `CommandTestHarness` and `TempDirectory`.

Dead oracle packet classes are removed.

## Cost
| Test | Before | After |
|---|---|---|
| `VirtualSerialTests` | 1.57s | 2ms (starts near the wrap via reflection instead of drawing ~17.9M serials) |
| Event scheduler recurrence loop | 473ms | 1ms |
| CrowdSec drain | 517ms | 16ms (event-driven instead of a fixed 500ms wait) |
| AuthId tests | 248ms | 99ms (SHA2 except the two GameLogin tests) |
| Container cap tests | 5,000 items | 3,449 / 3,277 (one past each protocol cap) |
| Bulk removal | 100k children | 20k children |
| OPL hash distribution | 60k builds | 10k builds |

## Shard-tuning decoupling
- AI assertions read `ReacquireDelay` and `FollowMoveSpeed` instead of hard-coding production tuning.
- The guard friendly-fire test uses an `AI_Mage` stub instead of WhiteWyrm.
- Argon2 assertions pin the `argon2id` variant and `v=19` but read the cost parameters from the configured hasher.

## Tests
| Run | Server.Tests | UOContent.Tests |
|---|---|---|
| With client files | 812 pass | 1,123 pass |
| Without client files (CI simulation) | 800 pass, 12 skip | 1,021 pass, 102 skip |

- The UOContent fixture reads `MODERNUO_TEST_DATA_DIR`; Server.Tests reads `MODERNUO_CLIENT_PATH`.
- The 15 new UOContent skips are the map-dependent guards above.
- Total run time with client files is about 5s, down from about 8s.
This commit is contained in:
Kamron Batman 2026-10-18 20:11:33 -07:00 • committed by GitHub
parent 79f736aa83
commit 00134cac33
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
135 changed files with 3164 additions and 4020 deletions

View file

@ -1,10 +1,8 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Sockets;
using System.Network;
using System.Threading;
using Server.Network;
using Xunit;
@ -22,42 +20,11 @@ public class NetStateSendBufferTests
return ns;
}
private static byte[] Pattern(int length, int seed)
{
var data = new byte[length];
new System.Random(seed).NextBytes(data);
return data;
}
private static unsafe void RegisterNoOpPing()
{
if (IncomingPackets.GetHandler(0x73) == null)
{
IncomingPackets.Register(0x73, 2, false, &NoOp);
}
}
private static void NoOp(NetState state, SpanReader reader)
{
}
private static void SliceUntil(Func<bool> done)
{
var deadline = Stopwatch.StartNew();
while (!done() && deadline.ElapsedMilliseconds < 5000)
{
NetState.Slice();
Thread.Sleep(5);
}
Assert.True(done());
}
[SkippableFact]
public void Account_InTheGameServerState_PromotesBothBuffers()
{
Skip.If(NetState.InitialSendBufferSize == 0);
RegisterNoOpPing();
NetTestHelpers.RequirePreAuthBuffers();
using var ping = NetTestHelpers.RegisterPing();
var ns = PacketTestUtilities.CreateTestNetState(out var client);
try
{
@ -73,8 +40,8 @@ public class NetStateSendBufferTests
// Recv promotes at the next completion
ns._protocolState = NetState.ProtocolState.GameServer_LoggedIn;
client.Send(new byte[] { 0x73, 0x01 });
SliceUntil(() => ns._socket.RecvBuffer.PhysicalSize == NetState.RecvBufferSize);
client.Send(new byte[] { NetTestHelpers.PingPacketId, 0x01 });
NetTestHelpers.SliceUntil(() => ns._socket.RecvBuffer.PhysicalSize == NetState.RecvBufferSize);
Assert.True(ns.Running);
}
finally
@ -87,7 +54,7 @@ public class NetStateSendBufferTests
[SkippableFact]
public void Account_OnTheLoginServer_KeepsTheInitialBuffers()
{
Skip.If(NetState.InitialSendBufferSize == 0);
NetTestHelpers.RequirePreAuthBuffers();
var ns = PacketTestUtilities.CreateTestNetState(out var client);
try
{
@ -108,11 +75,11 @@ public class NetStateSendBufferTests
public void Send_BeyondTheInitialBuffer_BeforeCredentials_IsExhausted()
{
// Nothing promotes before credentials verify: the 4 KiB ring is the whole pre-auth budget
Skip.If(NetState.InitialSendBufferSize == 0);
NetTestHelpers.RequirePreAuthBuffers();
var ns = PacketTestUtilities.CreateTestNetState(out var client);
try
{
var data = Pattern(NetState.InitialSendBufferSize + 512, 7);
var data = NetTestHelpers.Pattern(NetState.InitialSendBufferSize + 512, 7);
ns.Send(data);
Assert.Contains("exhausted", ns._disconnectReason, StringComparison.OrdinalIgnoreCase);
@ -123,7 +90,7 @@ public class NetStateSendBufferTests
// Once it completes the NetState is disposed and _socket goes null, so nothing below
// this point may touch it.
client.Close();
SliceUntil(() => !ns.Running);
NetTestHelpers.SliceUntil(() => !ns.Running);
}
finally
{
@ -137,19 +104,19 @@ public class NetStateSendBufferTests
{
// The late-verdict path from Account_AssignedAfterTheStateFlipped_StillPromotes: an account
// attached while the setter's own gate does not fire must still let the send path promote
Skip.If(NetState.InitialSendBufferSize == 0);
NetTestHelpers.RequirePreAuthBuffers();
var ns = PacketTestUtilities.CreateTestNetState(out var client);
try
{
ns.Account = new MockAccount(); // _protocolState is still AwaitingSeed: the setter does not promote
var data = Pattern(NetState.InitialSendBufferSize + 512, 7);
var data = NetTestHelpers.Pattern(NetState.InitialSendBufferSize + 512, 7);
ns.Send(data);
Assert.True(ns.Running);
Assert.Equal(string.Empty, ns._disconnectReason);
Assert.Equal(NetState.SendBufferSize, ns._socket.SendBuffer.PhysicalSize);
Assert.False(ns._sendBufferGrown); // promotion is not growth: nothing to shrink later
Assert.Equal(data, ReadAll(client, data.Length));
Assert.Equal(data, NetTestHelpers.ReadAll(client, data.Length));
}
finally
{
@ -161,12 +128,12 @@ public class NetStateSendBufferTests
[SkippableFact]
public void Send_BacklogBeforeCredentials_IsExhaustedInsteadOfPromoted()
{
Skip.If(NetState.InitialSendBufferSize == 0);
NetTestHelpers.RequirePreAuthBuffers();
var ns = PacketTestUtilities.CreateTestNetState(out var client);
try
{
// No Slice() between sends: nothing drains, so a second write finds a non-empty initial buffer
var half = Pattern(NetState.InitialSendBufferSize / 2, 11);
var half = NetTestHelpers.Pattern(NetState.InitialSendBufferSize / 2, 11);
ns.Send(half);
ns.Send(half);
ns.Send(half); // cannot fit, buffer not empty, no account: refused
@ -179,7 +146,7 @@ public class NetStateSendBufferTests
// Once it completes the NetState is disposed and _socket goes null, so nothing below
// this point may touch it.
client.Close();
SliceUntil(() => !ns.Running);
NetTestHelpers.SliceUntil(() => !ns.Running);
}
finally
{
@ -191,7 +158,7 @@ public class NetStateSendBufferTests
[SkippableFact]
public void Account_AssignedAfterTheStateFlipped_StillPromotes()
{
Skip.If(NetState.InitialSendBufferSize == 0);
NetTestHelpers.RequirePreAuthBuffers();
var ns = PacketTestUtilities.CreateTestNetState(out var client);
try
{
@ -211,7 +178,7 @@ public class NetStateSendBufferTests
{
var randomLength = compressedLength * 5 / 8;
var data = new byte[compressedLength * 8];
Pattern(randomLength, seed).CopyTo(data, 0);
NetTestHelpers.Pattern(randomLength, seed).CopyTo(data, 0);
var scratch = new byte[compressedLength * 2 + 16];
for (var length = randomLength; length < data.Length; length++)
@ -228,47 +195,12 @@ public class NetStateSendBufferTests
throw new InvalidOperationException($"No chunk compresses to exactly {compressedLength} bytes");
}
private static byte[] ReadAll(Socket client, int length)
{
var received = new byte[length];
var total = 0;
var deadline = Stopwatch.StartNew();
while (total < length && deadline.ElapsedMilliseconds < 10000)
{
NetState.Slice();
// Poll takes microseconds, not milliseconds
if (client.Poll(50_000, SelectMode.SelectRead))
{
var read = client.Receive(received, total, length - total, SocketFlags.None);
Assert.NotEqual(0, read);
total += read;
}
}
Assert.Equal(length, total);
return received;
}
// Peer receipt does not free the buffer; the completion must land first
private static void WaitForDrain(NetState ns)
{
var deadline = Stopwatch.StartNew();
while (deadline.ElapsedMilliseconds < 10000)
{
NetState.Slice();
if (ns._socket.SendBuffer.ReadableBytes == 0 && ns._socket.SendBuffer.InFlightBytes == 0)
{
break;
}
Thread.Sleep(5);
}
Assert.Equal(0, ns._socket.SendBuffer.ReadableBytes);
Assert.Equal(0, ns._socket.SendBuffer.InFlightBytes);
}
private static void WaitForDrain(NetState ns) =>
NetTestHelpers.SliceUntil(
() => ns._socket.SendBuffer.ReadableBytes == 0 && ns._socket.SendBuffer.InFlightBytes == 0,
10000
);
[Fact]
public void Send_GrowsInsteadOfDisconnecting_AndStreamStaysIntact()
@ -283,7 +215,7 @@ public class NetStateSendBufferTests
var expected = new byte[chunk * 6];
for (var i = 0; i < 6; i++)
{
var data = Pattern(chunk, i);
var data = NetTestHelpers.Pattern(chunk, i);
data.CopyTo(expected, i * chunk);
ns.Send(data);
}
@ -293,7 +225,7 @@ public class NetStateSendBufferTests
Assert.True(ns._socket.SendBuffer.PhysicalSize > baseSize);
Assert.True(ns._sendBufferGrown);
Assert.Equal(expected, ReadAll(client, expected.Length));
Assert.Equal(expected, NetTestHelpers.ReadAll(client, expected.Length));
}
finally
{
@ -318,7 +250,7 @@ public class NetStateSendBufferTests
var expectedLength = 0;
for (var i = 0; i < 6; i++)
{
var data = Pattern(chunkLength, i + 100);
var data = NetTestHelpers.Pattern(chunkLength, i + 100);
var compressedLength = NetworkCompression.Compress(data, scratch);
Assert.True(compressedLength > 0);
scratch.AsSpan(0, compressedLength).CopyTo(expected.AsSpan(expectedLength));
@ -332,7 +264,7 @@ public class NetStateSendBufferTests
Assert.True(ns._sendBufferGrown);
Array.Resize(ref expected, expectedLength);
Assert.Equal(expected, ReadAll(client, expected.Length));
Assert.Equal(expected, NetTestHelpers.ReadAll(client, expected.Length));
}
finally
{
@ -365,7 +297,7 @@ public class NetStateSendBufferTests
while (ns._socket.SendBuffer.WritableBytes > 4096)
{
var writable = ns._socket.SendBuffer.WritableBytes;
SendAndRecord(Pattern(Math.Min(baseSize / 8, (writable - 2048) * 8 / 11), seed++));
SendAndRecord(NetTestHelpers.Pattern(Math.Min(baseSize / 8, (writable - 2048) * 8 / 11), seed++));
}
// Full; no span to compress into
@ -381,7 +313,7 @@ public class NetStateSendBufferTests
Assert.Equal(baseSize * 2, ns._socket.SendBuffer.PhysicalSize);
Assert.True(ns._sendBufferGrown);
Assert.Equal(expected.ToArray(), ReadAll(client, expected.Count));
Assert.Equal(expected.ToArray(), NetTestHelpers.ReadAll(client, expected.Count));
}
finally
{
@ -401,8 +333,8 @@ public class NetStateSendBufferTests
// A small peer buffer keeps the first send in flight through the growth
client.ReceiveBufferSize = 4096;
var first = Pattern(baseSize / 2, 300);
var second = Pattern(baseSize / 2 + 1, 301);
var first = NetTestHelpers.Pattern(baseSize / 2, 300);
var second = NetTestHelpers.Pattern(baseSize / 2 + 1, 301);
ns.Send(first);
NetState.Slice(); // posts the first send
@ -419,7 +351,7 @@ public class NetStateSendBufferTests
first.CopyTo(expected, 0);
second.CopyTo(expected, first.Length);
Assert.Equal(expected, ReadAll(client, expected.Length));
Assert.Equal(expected, NetTestHelpers.ReadAll(client, expected.Length));
}
finally
{
@ -439,11 +371,11 @@ public class NetStateSendBufferTests
var chunk = baseSize / 4;
for (var i = 0; i < 6; i++)
{
ns.Send(Pattern(chunk, i));
ns.Send(NetTestHelpers.Pattern(chunk, i));
}
Assert.True(ns._sendBufferGrown);
ReadAll(client, chunk * 6);
NetTestHelpers.ReadAll(client, chunk * 6);
WaitForDrain(ns);
var grewAt = ns._sendBufferGrewAt;
@ -471,11 +403,11 @@ public class NetStateSendBufferTests
var chunk = baseSize / 4;
for (var i = 0; i < 6; i++)
{
ns.Send(Pattern(chunk, i + 400));
ns.Send(NetTestHelpers.Pattern(chunk, i + 400));
}
Assert.True(ns._sendBufferGrown);
ReadAll(client, chunk * 6);
NetTestHelpers.ReadAll(client, chunk * 6);
WaitForDrain(ns);
// The sweep shrinks; nothing here calls TryShrinkSendBuffer
@ -504,7 +436,7 @@ public class NetStateSendBufferTests
var writes = NetState.MaxSendBufferSize / chunk + 1;
for (var i = 0; i < writes; i++)
{
ns.Send(Pattern(chunk, i));
ns.Send(NetTestHelpers.Pattern(chunk, i));
}
Assert.Contains("Send buffer exhausted", ns._disconnectReason);
@ -532,7 +464,7 @@ public class NetStateSendBufferTests
for (var i = 0; i < 6; i++)
{
ns.Send(Pattern(baseSize / 4, i));
ns.Send(NetTestHelpers.Pattern(baseSize / 4, i));
}
Assert.Equal(baseSize, ns._socket.SendBuffer.PhysicalSize);
@ -558,7 +490,7 @@ public class NetStateSendBufferTests
NetState.Slice(); // handoff
// CannotSendPackets short-circuits before any growth
ns.Send(Pattern(baseSize * 2, 500));
ns.Send(NetTestHelpers.Pattern(baseSize * 2, 500));
Assert.Equal(baseSize, ns._socket.SendBuffer.PhysicalSize);
Assert.False(ns._sendBufferGrown);