Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions tests/Bydlo.Tests/BackupBufferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

namespace Bydlo.Tests;

/// <summary>Allocation measurements share ArrayPool.Shared, which parallel tests can drain or Gen2 GC can trim.</summary>
[CollectionDefinition(nameof(AllocationMeasurements), DisableParallelization = true)]
public sealed class AllocationMeasurements;

/// <summary>Bounded backup buffers (issue #394): same format, fewer copies, limits enforced early.</summary>
[Collection(nameof(AllocationMeasurements))]
public sealed class BackupBufferTests
{
private const string Password = "a long backup password for buffers";
Expand Down Expand Up @@ -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}");
}

Expand Down
1 change: 1 addition & 0 deletions tests/Bydlo.Tests/BackupCopyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Bydlo.Tests;

/// <summary>Typed backup copy (issue #393): every field survives and no mutable state is shared.</summary>
[Collection(nameof(AllocationMeasurements))]
public sealed class BackupCopyTests
{
[Fact]
Expand Down
6 changes: 4 additions & 2 deletions tests/Bydlo.Tests/SessionSecretsCleanupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EngineException>(() => start);
Assert.False(File.Exists(Path.Combine(work, "sing-box.json")));
Expand All @@ -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<EngineException>(() => start);
Assert.False(File.Exists(Path.Combine(work, "xray.json")));
Expand Down
Loading