diff --git a/tests/Bydlo.Tests/BackupBufferTests.cs b/tests/Bydlo.Tests/BackupBufferTests.cs index 86861c1f..46f59dd3 100644 --- a/tests/Bydlo.Tests/BackupBufferTests.cs +++ b/tests/Bydlo.Tests/BackupBufferTests.cs @@ -7,7 +7,12 @@ namespace Bydlo.Tests; +/// Allocation measurements share ArrayPool.Shared, which parallel tests can drain or Gen2 GC can trim. +[CollectionDefinition(nameof(AllocationMeasurements), DisableParallelization = true)] +public sealed class AllocationMeasurements; + /// Bounded backup buffers (issue #394): same format, fewer copies, limits enforced early. +[Collection(nameof(AllocationMeasurements))] public sealed class BackupBufferTests { private const string Password = "a long backup password for buffers"; @@ -158,13 +163,20 @@ public void Export_allocates_less_than_the_previous_implementation() data.Profiles.Add(new(new Profile { Name = "p" + i, Protocol = ProtocolKind.WireGuard, Host = "h", Port = 1 }, new ProfileSecrets { Values = { [Keys.RawConfig] = config + i } })); } - BackupArchive.Encrypt(data, Password); // warm the pool - var start = GC.GetAllocatedBytesForCurrentThread(); - BackupArchive.Encrypt(data, Password); - var current = GC.GetAllocatedBytesForCurrentThread() - start; - start = GC.GetAllocatedBytesForCurrentThread(); - LegacyEncrypt(data, Password); - var legacy = GC.GetAllocatedBytesForCurrentThread() - start; + // Best of three: a single run can lose its pooled buffer to a GC trim (flaky on busy CI runners). + var current = long.MaxValue; + var legacy = long.MaxValue; + for (var attempt = 0; attempt < 3; attempt++) + { + BackupArchive.Encrypt(data, Password); // warm the pool + var start = GC.GetAllocatedBytesForCurrentThread(); + BackupArchive.Encrypt(data, Password); + current = Math.Min(current, GC.GetAllocatedBytesForCurrentThread() - start); + start = GC.GetAllocatedBytesForCurrentThread(); + LegacyEncrypt(data, Password); + legacy = Math.Min(legacy, GC.GetAllocatedBytesForCurrentThread() - start); + } + Assert.True(current * 3 < legacy * 2, $"current={current} legacy={legacy}"); } diff --git a/tests/Bydlo.Tests/BackupCopyTests.cs b/tests/Bydlo.Tests/BackupCopyTests.cs index 173e9f8f..ae8f8be3 100644 --- a/tests/Bydlo.Tests/BackupCopyTests.cs +++ b/tests/Bydlo.Tests/BackupCopyTests.cs @@ -11,6 +11,7 @@ namespace Bydlo.Tests; /// Typed backup copy (issue #393): every field survives and no mutable state is shared. +[Collection(nameof(AllocationMeasurements))] public sealed class BackupCopyTests { [Fact] diff --git a/tests/Bydlo.Tests/SessionSecretsCleanupTests.cs b/tests/Bydlo.Tests/SessionSecretsCleanupTests.cs index 4f892014..48954399 100644 --- a/tests/Bydlo.Tests/SessionSecretsCleanupTests.cs +++ b/tests/Bydlo.Tests/SessionSecretsCleanupTests.cs @@ -71,7 +71,8 @@ public async Task SingBox_config_is_removed_after_failed_start() var engine = SingBox(runner); var work = TestDirs.New("sb-clean-fail"); var start = engine.StartAsync(SingBoxRequest(work)); - await Task.Delay(100); + // Wait for the fake process instead of a fixed delay: a slow runner made runner.Last null. + await WaitForAsync(() => runner.Last is not null); runner.Last!.Crash(1); await Assert.ThrowsAsync(() => start); Assert.False(File.Exists(Path.Combine(work, "sing-box.json"))); @@ -84,7 +85,8 @@ public async Task Xray_config_is_removed_after_failed_start() var engine = Xray(runner); var work = TestDirs.New("xray-clean-fail"); var start = engine.StartAsync(XrayRequest(work)); - await Task.Delay(100); + // Wait for the fake process instead of a fixed delay: a slow runner made runner.Last null. + await WaitForAsync(() => runner.Last is not null); runner.Last!.Crash(1); await Assert.ThrowsAsync(() => start); Assert.False(File.Exists(Path.Combine(work, "xray.json")));