fix: preserve endurance failures and validate milestone reports

This commit is contained in:
2026-09-05 22:55:56 +08:00
parent 1e8e894dba
commit e0d1ea1022
3 changed files with 110 additions and 20 deletions
@@ -107,6 +107,66 @@ public sealed class WechatStabilityTests
Assert.Contains("thread-growth-over-100", report.Findings);
}
[Theory]
[InlineData(30, false, 0, true)]
[InlineData(15, false, 0, false)]
[InlineData(30, true, 0, false)]
[InlineData(30, false, 11, false)]
public void RunChecks_ApplyDurationCancellationAndHeartbeatToEveryReport(int seconds, bool cancelled, int snapshotAge, bool healthy)
{
var finish = DateTimeOffset.UtcNow;
var last = Sample(finish, 100, 10, 1) with { LastSnapshotAt = finish.AddSeconds(-snapshotAge) };
var report = WechatStabilityAnalyzer.Analyze(finish.AddSeconds(-seconds), finish, [last], 0, 0);
var checkedReport = WechatStabilityAnalyzer.CheckRun(report, TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(seconds), cancelled);
Assert.Equal(healthy, checkedReport.Healthy);
Assert.Equal(seconds >= 29 && !cancelled, checkedReport.CompletedDuration);
}
[Fact]
public void RunChecks_RejectMissingHeartbeatAndBackgroundFailures()
{
var finish = DateTimeOffset.UtcNow;
var report = WechatStabilityAnalyzer.Analyze(finish.AddHours(-8), finish, [Sample(finish, 0, 0, 0)], 0, 0);
report = WechatStabilityAnalyzer.CheckRun(report, TimeSpan.FromHours(8), TimeSpan.FromHours(8), false,
"InvalidOperationException", "IOException");
Assert.False(report.Healthy);
Assert.Contains("listener-snapshot-stale", report.Findings);
Assert.Contains("listener-failed:InvalidOperationException", report.Findings);
Assert.Contains("sampling-failed:IOException", report.Findings);
}
[Fact]
public void StabilityAnalyzer_DoesNotHideMissingHeartbeatBeforeRecovery()
{
var started = DateTimeOffset.UtcNow;
var missing = Sample(started.AddMinutes(6), 100, 10, 1);
var recovered = missing with { Timestamp = started.AddMinutes(7), LastSnapshotAt = started.AddMinutes(7) };
var report = WechatStabilityAnalyzer.Analyze(started, recovered.Timestamp, [missing, recovered], 0, 1);
Assert.Contains("listener-snapshot-gap-over-5min", report.Findings);
Assert.False(report.Healthy);
}
[Fact]
public async Task ReportWrite_PreservesExistingEvidenceWhenCancelled()
{
var directory = Path.Combine(Path.GetTempPath(), "wx-stability-" + Guid.NewGuid());
Directory.CreateDirectory(directory);
try
{
var path = Path.Combine(directory, "report.json");
await File.WriteAllTextAsync(path, "previous evidence");
var now = DateTimeOffset.UtcNow;
var report = WechatStabilityAnalyzer.Analyze(now, now, [Sample(now, 0, 0, 0)], 0, 0);
using var cancelled = new CancellationTokenSource();
cancelled.Cancel();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() =>
WechatStabilityAnalyzer.WriteJsonAsync(report, path, cancelled.Token));
Assert.Equal("previous evidence", await File.ReadAllTextAsync(path));
Assert.False(File.Exists(path + ".tmp"));
}
finally { Directory.Delete(directory, recursive: true); }
}
private static WechatStabilitySample Sample(
DateTimeOffset timestamp,
long memory,