From 4be7778e9d6c524b272229632db559ffda1d8242 Mon Sep 17 00:00:00 2001 From: endercodezz Date: Sun, 22 Nov 2026 03:55:05 +0200 Subject: [PATCH 1/2] Stabilize backup allocation measurements Run allocation-measuring tests in a non-parallel collection and compare the best of three runs, so a pool trim or a parallel renter no longer fails the export comparison. Thresholds are unchanged. Closes #503 --- tests/Bydlo.Tests/BackupBufferTests.cs | 26 +++++++++++++++++++------- tests/Bydlo.Tests/BackupCopyTests.cs | 1 + 2 files changed, 20 insertions(+), 7 deletions(-) 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] From 99175c92ff1aef6f83526760da7ab5527b07ed1e Mon Sep 17 00:00:00 2001 From: endercodezz Date: Sun, 22 Nov 2026 04:07:25 +0200 Subject: [PATCH 2/2] Wait for the fake engine process instead of a fixed delay SessionSecretsCleanupTests crashed runner.Last after Task.Delay(100); on a slow runner the process was not started yet and the test failed with a null reference. Wait for the condition like the neighbouring tests do. --- tests/Bydlo.Tests/SessionSecretsCleanupTests.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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")));