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.
531 lines
19 KiB
C#
531 lines
19 KiB
C#
using System;
|
|
using System.Buffers;
|
|
using System.Collections.Generic;
|
|
using System.Net.Sockets;
|
|
using System.Network;
|
|
using Server.Network;
|
|
using Xunit;
|
|
|
|
namespace Server.Tests.Network;
|
|
|
|
[Collection("Sequential Server Tests")]
|
|
public class NetStateSendBufferTests
|
|
{
|
|
// Authentication is the 0x91 credentials check on the game server; that is where buffers promote
|
|
private static NetState CreateAuthenticatedNetState(out Socket client)
|
|
{
|
|
var ns = PacketTestUtilities.CreateTestNetState(out client);
|
|
ns._protocolState = NetState.ProtocolState.GameServer_AwaitingGameServerLogin;
|
|
ns.Account = new MockAccount();
|
|
return ns;
|
|
}
|
|
|
|
[SkippableFact]
|
|
public void Account_InTheGameServerState_PromotesBothBuffers()
|
|
{
|
|
NetTestHelpers.RequirePreAuthBuffers();
|
|
using var ping = NetTestHelpers.RegisterPing();
|
|
var ns = PacketTestUtilities.CreateTestNetState(out var client);
|
|
try
|
|
{
|
|
Assert.Equal(NetState.InitialSendBufferSize, ns._socket.SendBuffer.PhysicalSize);
|
|
Assert.Equal(NetState.InitialRecvBufferSize, ns._socket.RecvBuffer.PhysicalSize);
|
|
|
|
ns._protocolState = NetState.ProtocolState.GameServer_AwaitingGameServerLogin;
|
|
ns.Account = new MockAccount();
|
|
|
|
// Send promotes at once; nothing was in flight so no buffer retires
|
|
Assert.Equal(NetState.SendBufferSize, ns._socket.SendBuffer.PhysicalSize);
|
|
Assert.False(ns._sendBufferGrown);
|
|
|
|
// Recv promotes at the next completion
|
|
ns._protocolState = NetState.ProtocolState.GameServer_LoggedIn;
|
|
client.Send(new byte[] { NetTestHelpers.PingPacketId, 0x01 });
|
|
NetTestHelpers.SliceUntil(() => ns._socket.RecvBuffer.PhysicalSize == NetState.RecvBufferSize);
|
|
Assert.True(ns.Running);
|
|
}
|
|
finally
|
|
{
|
|
ns.Dispose();
|
|
client.Close();
|
|
}
|
|
}
|
|
|
|
[SkippableFact]
|
|
public void Account_OnTheLoginServer_KeepsTheInitialBuffers()
|
|
{
|
|
NetTestHelpers.RequirePreAuthBuffers();
|
|
var ns = PacketTestUtilities.CreateTestNetState(out var client);
|
|
try
|
|
{
|
|
ns._protocolState = NetState.ProtocolState.LoginServer_AwaitingLogin;
|
|
ns.Account = new MockAccount();
|
|
|
|
Assert.Equal(NetState.InitialSendBufferSize, ns._socket.SendBuffer.PhysicalSize);
|
|
Assert.Equal(NetState.InitialRecvBufferSize, ns._socket.RecvBuffer.PhysicalSize);
|
|
}
|
|
finally
|
|
{
|
|
ns.Dispose();
|
|
client.Close();
|
|
}
|
|
}
|
|
|
|
[SkippableFact]
|
|
public void Send_BeyondTheInitialBuffer_BeforeCredentials_IsExhausted()
|
|
{
|
|
// Nothing promotes before credentials verify: the 4 KiB ring is the whole pre-auth budget
|
|
NetTestHelpers.RequirePreAuthBuffers();
|
|
var ns = PacketTestUtilities.CreateTestNetState(out var client);
|
|
try
|
|
{
|
|
var data = NetTestHelpers.Pattern(NetState.InitialSendBufferSize + 512, 7);
|
|
ns.Send(data);
|
|
|
|
Assert.Contains("exhausted", ns._disconnectReason, StringComparison.OrdinalIgnoreCase);
|
|
Assert.Equal(NetState.InitialSendBufferSize, ns._socket.SendBuffer.PhysicalSize);
|
|
|
|
// A graceful server-side close half-closes and waits for the peer's own FIN; closing the
|
|
// peer here stands in for the client eventually going away, so Running can be observed.
|
|
// Once it completes the NetState is disposed and _socket goes null, so nothing below
|
|
// this point may touch it.
|
|
client.Close();
|
|
NetTestHelpers.SliceUntil(() => !ns.Running);
|
|
}
|
|
finally
|
|
{
|
|
ns.Dispose();
|
|
client.Close();
|
|
}
|
|
}
|
|
|
|
[SkippableFact]
|
|
public void Send_BeyondTheInitialBuffer_WithCredentials_PromotesOnDemand()
|
|
{
|
|
// 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
|
|
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 = 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, NetTestHelpers.ReadAll(client, data.Length));
|
|
}
|
|
finally
|
|
{
|
|
ns.Dispose();
|
|
client.Close();
|
|
}
|
|
}
|
|
|
|
[SkippableFact]
|
|
public void Send_BacklogBeforeCredentials_IsExhaustedInsteadOfPromoted()
|
|
{
|
|
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 = NetTestHelpers.Pattern(NetState.InitialSendBufferSize / 2, 11);
|
|
ns.Send(half);
|
|
ns.Send(half);
|
|
ns.Send(half); // cannot fit, buffer not empty, no account: refused
|
|
|
|
Assert.Equal(NetState.InitialSendBufferSize, ns._socket.SendBuffer.PhysicalSize);
|
|
Assert.Contains("exhausted", ns._disconnectReason, StringComparison.OrdinalIgnoreCase);
|
|
|
|
// A graceful server-side close waits for the queued bytes to drain and then for the
|
|
// peer's own FIN; closing the peer here stands in for the client eventually going away.
|
|
// Once it completes the NetState is disposed and _socket goes null, so nothing below
|
|
// this point may touch it.
|
|
client.Close();
|
|
NetTestHelpers.SliceUntil(() => !ns.Running);
|
|
}
|
|
finally
|
|
{
|
|
ns.Dispose();
|
|
client.Close();
|
|
}
|
|
}
|
|
|
|
[SkippableFact]
|
|
public void Account_AssignedAfterTheStateFlipped_StillPromotes()
|
|
{
|
|
NetTestHelpers.RequirePreAuthBuffers();
|
|
var ns = PacketTestUtilities.CreateTestNetState(out var client);
|
|
try
|
|
{
|
|
ns._protocolState = NetState.ProtocolState.GameServer_LoggedIn;
|
|
ns.Account = new MockAccount();
|
|
Assert.Equal(NetState.SendBufferSize, ns._socket.SendBuffer.PhysicalSize);
|
|
}
|
|
finally
|
|
{
|
|
ns.Dispose();
|
|
client.Close();
|
|
}
|
|
}
|
|
|
|
// A random prefix lands under the target; zeros (2 bits each) walk it up a byte at a time
|
|
private static byte[] CompressesToExactly(int compressedLength, int seed)
|
|
{
|
|
var randomLength = compressedLength * 5 / 8;
|
|
var data = new byte[compressedLength * 8];
|
|
NetTestHelpers.Pattern(randomLength, seed).CopyTo(data, 0);
|
|
|
|
var scratch = new byte[compressedLength * 2 + 16];
|
|
for (var length = randomLength; length < data.Length; length++)
|
|
{
|
|
var compressed = NetworkCompression.Compress(data.AsSpan(0, length), scratch);
|
|
Assert.InRange(compressed, 1, compressedLength);
|
|
|
|
if (compressed == compressedLength)
|
|
{
|
|
return data[..length];
|
|
}
|
|
}
|
|
|
|
throw new InvalidOperationException($"No chunk compresses to exactly {compressedLength} bytes");
|
|
}
|
|
|
|
// Peer receipt does not free the buffer; the completion must land first
|
|
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()
|
|
{
|
|
var ns = CreateAuthenticatedNetState(out var client);
|
|
var baseSize = ns._socket.SendBuffer.PhysicalSize;
|
|
|
|
try
|
|
{
|
|
// No Slice(): nothing drains, so the base buffer overflows
|
|
var chunk = baseSize / 4;
|
|
var expected = new byte[chunk * 6];
|
|
for (var i = 0; i < 6; i++)
|
|
{
|
|
var data = NetTestHelpers.Pattern(chunk, i);
|
|
data.CopyTo(expected, i * chunk);
|
|
ns.Send(data);
|
|
}
|
|
|
|
Assert.True(ns.Running);
|
|
Assert.Equal(string.Empty, ns._disconnectReason);
|
|
Assert.True(ns._socket.SendBuffer.PhysicalSize > baseSize);
|
|
Assert.True(ns._sendBufferGrown);
|
|
|
|
Assert.Equal(expected, NetTestHelpers.ReadAll(client, expected.Length));
|
|
}
|
|
finally
|
|
{
|
|
ns.Dispose();
|
|
client.Close();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Send_CompressedGrowth_RetriesAndStreamStaysIntact()
|
|
{
|
|
var ns = CreateAuthenticatedNetState(out var client);
|
|
ns.CompressionEnabled = true;
|
|
var baseSize = ns._socket.SendBuffer.PhysicalSize;
|
|
|
|
try
|
|
{
|
|
// No Slice(): compressed output accumulates until the base overflows
|
|
var chunkLength = baseSize / 4;
|
|
var expected = new byte[(chunkLength * 2 + 4) * 6];
|
|
var scratch = new byte[chunkLength * 2 + 4];
|
|
var expectedLength = 0;
|
|
for (var i = 0; i < 6; i++)
|
|
{
|
|
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));
|
|
expectedLength += compressedLength;
|
|
ns.Send(data);
|
|
}
|
|
|
|
Assert.True(ns.Running);
|
|
Assert.Equal(string.Empty, ns._disconnectReason);
|
|
Assert.True(ns._socket.SendBuffer.PhysicalSize > baseSize);
|
|
Assert.True(ns._sendBufferGrown);
|
|
|
|
Array.Resize(ref expected, expectedLength);
|
|
Assert.Equal(expected, NetTestHelpers.ReadAll(client, expected.Length));
|
|
}
|
|
finally
|
|
{
|
|
ns.Dispose();
|
|
client.Close();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Send_CompressedGrowth_FullBuffer_GrowsOneTierAndFits()
|
|
{
|
|
var ns = CreateAuthenticatedNetState(out var client);
|
|
ns.CompressionEnabled = true;
|
|
var baseSize = ns._socket.SendBuffer.PhysicalSize;
|
|
var expected = new List<byte>(baseSize * 2);
|
|
|
|
void SendAndRecord(byte[] data)
|
|
{
|
|
var scratch = new byte[data.Length * 2 + 4];
|
|
var compressedLength = NetworkCompression.Compress(data, scratch);
|
|
Assert.True(compressedLength > 0);
|
|
expected.AddRange(scratch.AsSpan(0, compressedLength));
|
|
ns.Send(data);
|
|
}
|
|
|
|
try
|
|
{
|
|
// Chunks sized so the fill cannot overflow (random bytes never compress; ratio at most 11/8)
|
|
var seed = 600;
|
|
while (ns._socket.SendBuffer.WritableBytes > 4096)
|
|
{
|
|
var writable = ns._socket.SendBuffer.WritableBytes;
|
|
SendAndRecord(NetTestHelpers.Pattern(Math.Min(baseSize / 8, (writable - 2048) * 8 / 11), seed++));
|
|
}
|
|
|
|
// Full; no span to compress into
|
|
SendAndRecord(CompressesToExactly(ns._socket.SendBuffer.WritableBytes, seed));
|
|
Assert.Equal(0, ns._socket.SendBuffer.WritableBytes);
|
|
Assert.Equal(baseSize, ns._socket.SendBuffer.PhysicalSize);
|
|
|
|
// Raw exceeds the remainder; compressed fits in one tier
|
|
SendAndRecord(new byte[Math.Min(baseSize * 3 / 4, NetworkCompression.DefiniteOverflow)]);
|
|
|
|
Assert.True(ns.Running);
|
|
Assert.Equal(string.Empty, ns._disconnectReason);
|
|
Assert.Equal(baseSize * 2, ns._socket.SendBuffer.PhysicalSize);
|
|
Assert.True(ns._sendBufferGrown);
|
|
|
|
Assert.Equal(expected.ToArray(), NetTestHelpers.ReadAll(client, expected.Count));
|
|
}
|
|
finally
|
|
{
|
|
ns.Dispose();
|
|
client.Close();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Send_WithSendInFlight_GrowsAndStreamStaysIntact()
|
|
{
|
|
var ns = CreateAuthenticatedNetState(out var client);
|
|
var baseSize = ns._socket.SendBuffer.PhysicalSize;
|
|
|
|
try
|
|
{
|
|
// A small peer buffer keeps the first send in flight through the growth
|
|
client.ReceiveBufferSize = 4096;
|
|
|
|
var first = NetTestHelpers.Pattern(baseSize / 2, 300);
|
|
var second = NetTestHelpers.Pattern(baseSize / 2 + 1, 301);
|
|
|
|
ns.Send(first);
|
|
NetState.Slice(); // posts the first send
|
|
Assert.True(ns._socket.SendBuffer.InFlightBytes > 0);
|
|
|
|
ns.Send(second);
|
|
|
|
Assert.True(ns.Running);
|
|
Assert.Equal(string.Empty, ns._disconnectReason);
|
|
Assert.True(ns._socket.SendBuffer.PhysicalSize > baseSize);
|
|
Assert.True(ns._sendBufferGrown);
|
|
|
|
var expected = new byte[first.Length + second.Length];
|
|
first.CopyTo(expected, 0);
|
|
second.CopyTo(expected, first.Length);
|
|
|
|
Assert.Equal(expected, NetTestHelpers.ReadAll(client, expected.Length));
|
|
}
|
|
finally
|
|
{
|
|
ns.Dispose();
|
|
client.Close();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Shrink_AfterDrainAndHold_ReturnsToBase()
|
|
{
|
|
var ns = CreateAuthenticatedNetState(out var client);
|
|
var baseSize = ns._socket.SendBuffer.PhysicalSize;
|
|
|
|
try
|
|
{
|
|
var chunk = baseSize / 4;
|
|
for (var i = 0; i < 6; i++)
|
|
{
|
|
ns.Send(NetTestHelpers.Pattern(chunk, i));
|
|
}
|
|
|
|
Assert.True(ns._sendBufferGrown);
|
|
NetTestHelpers.ReadAll(client, chunk * 6);
|
|
WaitForDrain(ns);
|
|
|
|
var grewAt = ns._sendBufferGrewAt;
|
|
Assert.False(ns.TryShrinkSendBuffer(grewAt + NetState.SendBufferHoldMs - 1));
|
|
Assert.True(ns.TryShrinkSendBuffer(grewAt + NetState.SendBufferHoldMs));
|
|
Assert.Equal(baseSize, ns._socket.SendBuffer.PhysicalSize);
|
|
Assert.False(ns._sendBufferGrown);
|
|
}
|
|
finally
|
|
{
|
|
ns.Dispose();
|
|
client.Close();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Shrink_ThroughAliveSweep_ReturnsToBase()
|
|
{
|
|
var ns = CreateAuthenticatedNetState(out var client);
|
|
var baseSize = ns._socket.SendBuffer.PhysicalSize;
|
|
var previousTicks = Core._tickCount;
|
|
|
|
try
|
|
{
|
|
var chunk = baseSize / 4;
|
|
for (var i = 0; i < 6; i++)
|
|
{
|
|
ns.Send(NetTestHelpers.Pattern(chunk, i + 400));
|
|
}
|
|
|
|
Assert.True(ns._sendBufferGrown);
|
|
NetTestHelpers.ReadAll(client, chunk * 6);
|
|
WaitForDrain(ns);
|
|
|
|
// The sweep shrinks; nothing here calls TryShrinkSendBuffer
|
|
Core._tickCount = ns._sendBufferGrewAt + NetState.SendBufferHoldMs;
|
|
NetState.CheckAllAlive();
|
|
|
|
Assert.Equal(baseSize, ns._socket.SendBuffer.PhysicalSize);
|
|
Assert.False(ns._sendBufferGrown);
|
|
}
|
|
finally
|
|
{
|
|
Core._tickCount = previousTicks;
|
|
ns.Dispose();
|
|
client.Close();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Send_PastTheMaximum_FallsBackToExhaustion()
|
|
{
|
|
var ns = CreateAuthenticatedNetState(out var client);
|
|
|
|
try
|
|
{
|
|
var chunk = 64 * 1024;
|
|
var writes = NetState.MaxSendBufferSize / chunk + 1;
|
|
for (var i = 0; i < writes; i++)
|
|
{
|
|
ns.Send(NetTestHelpers.Pattern(chunk, i));
|
|
}
|
|
|
|
Assert.Contains("Send buffer exhausted", ns._disconnectReason);
|
|
Assert.Equal(NetState.MaxSendBufferSize, ns._socket.SendBuffer.PhysicalSize);
|
|
}
|
|
finally
|
|
{
|
|
ns.Dispose();
|
|
client.Close();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Send_AboveMemoryCeiling_DoesNotGrow()
|
|
{
|
|
var previous = NetState._availableMemoryBytes;
|
|
NetState ns = null;
|
|
Socket client = null;
|
|
|
|
try
|
|
{
|
|
ns = CreateAuthenticatedNetState(out client);
|
|
var baseSize = ns._socket.SendBuffer.PhysicalSize;
|
|
NetState._availableMemoryBytes = 1; // any working set is above 80% of one byte
|
|
|
|
for (var i = 0; i < 6; i++)
|
|
{
|
|
ns.Send(NetTestHelpers.Pattern(baseSize / 4, i));
|
|
}
|
|
|
|
Assert.Equal(baseSize, ns._socket.SendBuffer.PhysicalSize);
|
|
Assert.Contains("Send buffer exhausted", ns._disconnectReason);
|
|
}
|
|
finally
|
|
{
|
|
NetState._availableMemoryBytes = previous;
|
|
ns?.Dispose();
|
|
client?.Close();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Send_OnClosingSocket_DoesNotGrow()
|
|
{
|
|
var ns = CreateAuthenticatedNetState(out var client);
|
|
var baseSize = ns._socket.SendBuffer.PhysicalSize;
|
|
|
|
try
|
|
{
|
|
ns.Disconnect("test");
|
|
NetState.Slice(); // handoff
|
|
|
|
// CannotSendPackets short-circuits before any growth
|
|
ns.Send(NetTestHelpers.Pattern(baseSize * 2, 500));
|
|
|
|
Assert.Equal(baseSize, ns._socket.SendBuffer.PhysicalSize);
|
|
Assert.False(ns._sendBufferGrown);
|
|
}
|
|
finally
|
|
{
|
|
ns.Dispose();
|
|
client.Close();
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(3 * 1024 * 1024, 4 * 1024 * 1024)] // not a power of two; rounded up
|
|
[InlineData(512 * 1024 * 1024, 256 * 1024 * 1024)] // above the transport ceiling; capped
|
|
[InlineData(1024, 64 * 1024)] // below the minimum; raised
|
|
[InlineData(2 * 1024 * 1024, 2 * 1024 * 1024)] // already valid; untouched
|
|
public void CoercePowerOfTwoSetting_CoercesToATierTheTransportAccepts(int configured, int expected) =>
|
|
Assert.Equal(expected, NetState.CoercePowerOfTwoSetting("network.sendBufferMaxSize", configured, 64 * 1024));
|
|
|
|
[Fact]
|
|
public void CoerceSendBufferGrowthBudget_ClampsNegativeAndRaisesBelowOneSlab()
|
|
{
|
|
const int sendBufferSize = 256 * 1024;
|
|
const int maxSendBufferSize = 2 * 1024 * 1024;
|
|
var minimum = RingSocketManager.MinimumSendBufferGrowthBudget(sendBufferSize);
|
|
|
|
Assert.Equal(0L, NetState.CoerceSendBufferGrowthBudget(-1, sendBufferSize, maxSendBufferSize));
|
|
|
|
// zero means never grow
|
|
Assert.Equal(0L, NetState.CoerceSendBufferGrowthBudget(0, sendBufferSize, maxSendBufferSize));
|
|
|
|
Assert.Equal(minimum, NetState.CoerceSendBufferGrowthBudget(1, sendBufferSize, maxSendBufferSize));
|
|
Assert.Equal(minimum * 4, NetState.CoerceSendBufferGrowthBudget(minimum * 4, sendBufferSize, maxSendBufferSize));
|
|
|
|
// growth off; budget untouched
|
|
Assert.Equal(1L, NetState.CoerceSendBufferGrowthBudget(1, sendBufferSize, sendBufferSize));
|
|
}
|
|
}
|