Files
Heimdall/tests/Heimdall.Agent.Tests/PipelineDegradationManagerTests.cs
T
Arnaud Dartois fa86185b00
Heimdall Gitea / Detect CI scope (pull_request) Successful in 4s
Heimdall Gitea / Build and tests (pull_request) Successful in 16m42s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Successful in 4m37s
Heimdall Gitea / Build and scan server image (pull_request) Successful in 9m0s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Successful in 2s
test(agent): avoid collection analyzer naming warning
2026-10-25 13:55:26 +01:00

255 lines
7.7 KiB
C#

using System.Diagnostics.Metrics;
using Heimdall.Application.Metrics;
using Heimdall.Agent.Services;
using Microsoft.Extensions.Logging;
using NSubstitute;
namespace Heimdall.Agent.Tests;
[CollectionDefinition("Pipeline degradation metric", DisableParallelization = true)]
public sealed class PipelineDegradationMetricTestGroup { }
[Collection("Pipeline degradation metric")]
public class PipelineDegradationManagerTests
{
private readonly PipelineDegradationManager _manager;
public PipelineDegradationManagerTests()
{
_manager = new PipelineDegradationManager(Substitute.For<ILogger<PipelineDegradationManager>>());
}
[Fact]
public void InitialLevel_IsNormal()
{
_manager.Level.ShouldBe(DegradationLevel.Normal);
}
[Fact]
public void PipelineLevelMetric_TracksDegradationAndRecovery()
{
var observedLevels = new List<int>();
using var listener = new MeterListener();
listener.InstrumentPublished = (instrument, meterListener) =>
{
if (instrument.Meter.Name == HeimdallMetrics.MeterName &&
instrument.Name == "heimdall.ai.pipeline.level")
meterListener.EnableMeasurementEvents(instrument);
};
listener.SetMeasurementEventCallback<int>((instrument, value, _, _) =>
{
if (instrument.Name == "heimdall.ai.pipeline.level")
observedLevels.Add(value);
});
listener.Start();
var manager = new PipelineDegradationManager(Substitute.For<ILogger<PipelineDegradationManager>>());
manager.ReportCpuSaturation(85);
listener.RecordObservableInstruments();
observedLevels.Last().ShouldBe((int)DegradationLevel.CpuSaturation);
manager.ReportCpuRecovery(50);
listener.RecordObservableInstruments();
observedLevels.Last().ShouldBe((int)DegradationLevel.Normal);
}
[Fact]
public void ReportInferenceFailure_AfterThreshold_EscalatesToSlowExecutor()
{
// Report failures below slow threshold — should stay Normal
for (int i = 0; i < 2; i++)
_manager.ReportInferenceFailure();
_manager.Level.ShouldBe(DegradationLevel.Normal);
// Third failure should trigger SlowExecutor
_manager.ReportInferenceFailure();
_manager.Level.ShouldBe(DegradationLevel.SlowExecutor);
}
[Fact]
public void ReportInferenceFailure_AfterDownThreshold_EscalatesToExecutorDown()
{
// Report enough failures to reach ExecutorDown
for (int i = 0; i < 10; i++)
_manager.ReportInferenceFailure();
_manager.Level.ShouldBe(DegradationLevel.ExecutorDown);
}
[Fact]
public void ReportInferenceSlow_AfterThreshold_EscalatesToSlowExecutor()
{
for (int i = 0; i < 3; i++)
_manager.ReportInferenceSlow(600);
_manager.Level.ShouldBe(DegradationLevel.SlowExecutor);
}
[Fact]
public void ReportInferenceSuccess_ResetsConsecutiveFailures()
{
_manager.ReportInferenceFailure();
_manager.ReportInferenceFailure();
_manager.ConsecutiveFailures.ShouldBe(2);
_manager.ReportInferenceSuccess();
_manager.ConsecutiveFailures.ShouldBe(0);
}
[Fact]
public void ReportInferenceSuccess_RecoversFromSlowExecutor_AfterThreshold()
{
// Escalate to SlowExecutor
for (int i = 0; i < 3; i++)
_manager.ReportInferenceSlow(600);
_manager.Level.ShouldBe(DegradationLevel.SlowExecutor);
// Report enough successes to recover
for (int i = 0; i < 5; i++)
_manager.ReportInferenceSuccess();
_manager.Level.ShouldBe(DegradationLevel.Normal);
}
[Fact]
public void ReportInferenceSuccess_RecoversFromExecutorDown_AfterThreshold()
{
// Escalate to ExecutorDown
for (int i = 0; i < 10; i++)
_manager.ReportInferenceFailure();
_manager.Level.ShouldBe(DegradationLevel.ExecutorDown);
// Report enough successes to recover
for (int i = 0; i < 5; i++)
_manager.ReportInferenceSuccess();
_manager.Level.ShouldBe(DegradationLevel.Normal);
}
[Fact]
public void ReportCpuSaturation_AboveThreshold_SetsLevel3()
{
_manager.ReportCpuSaturation(85.0);
_manager.Level.ShouldBe(DegradationLevel.CpuSaturation);
}
[Fact]
public void ReportCpuSaturation_BelowThreshold_DoesNotChangeLevel()
{
_manager.ReportCpuSaturation(50.0);
_manager.Level.ShouldBe(DegradationLevel.Normal);
}
[Fact]
public void ReportCpuRecovery_BelowThreshold_ClearsLevel3()
{
_manager.ReportCpuSaturation(85.0);
_manager.Level.ShouldBe(DegradationLevel.CpuSaturation);
_manager.ReportCpuRecovery(50.0);
_manager.Level.ShouldBe(DegradationLevel.Normal);
}
[Fact]
public void ShouldUseInference_AtExecutorDown_ReturnsFalse()
{
for (int i = 0; i < 10; i++)
_manager.ReportInferenceFailure();
_manager.ShouldUseInference().ShouldBeFalse();
}
[Fact]
public void ShouldUseInference_AtNormal_ReturnsTrue()
{
_manager.ShouldUseInference().ShouldBeTrue();
}
[Fact]
public void ShouldUseInference_AtSlowExecutor_ReturnsTrue()
{
for (int i = 0; i < 3; i++)
_manager.ReportInferenceSlow(600);
_manager.ShouldUseInference().ShouldBeTrue();
}
[Fact]
public void GetConfidenceThreshold_AtNormal_Returns05()
{
_manager.GetConfidenceThreshold().ShouldBe(0.5f);
}
[Fact]
public void GetConfidenceThreshold_AtSlowExecutor_Returns03()
{
for (int i = 0; i < 3; i++)
_manager.ReportInferenceSlow(600);
_manager.GetConfidenceThreshold().ShouldBe(0.3f);
}
[Fact]
public void GetSnapshotIntervalMultiplier_AtCpuSaturation_Returns2()
{
_manager.ReportCpuSaturation(85.0);
_manager.GetSnapshotIntervalMultiplier().ShouldBe(2.0);
}
[Fact]
public void GetSnapshotIntervalMultiplier_AtNormal_Returns1()
{
_manager.GetSnapshotIntervalMultiplier().ShouldBe(1.0);
}
[Fact]
public void Reset_ClearsAllState()
{
for (int i = 0; i < 10; i++)
_manager.ReportInferenceFailure();
_manager.Level.ShouldBe(DegradationLevel.ExecutorDown);
_manager.Reset();
_manager.Level.ShouldBe(DegradationLevel.Normal);
_manager.ConsecutiveFailures.ShouldBe(0);
}
[Fact]
public void HighestLevel_TracksMaximum()
{
for (int i = 0; i < 3; i++)
_manager.ReportInferenceSlow(600);
_manager.HighestLevel.ShouldBe(DegradationLevel.SlowExecutor);
for (int i = 0; i < 10; i++)
_manager.ReportInferenceFailure();
_manager.HighestLevel.ShouldBe(DegradationLevel.ExecutorDown);
}
[Fact]
public void DegradationDuration_IsNonZero_AfterDegradation()
{
for (int i = 0; i < 3; i++)
_manager.ReportInferenceSlow(600);
_manager.DegradationDuration.ShouldBeGreaterThan(TimeSpan.Zero);
}
[Fact]
public void ReportInferenceFailure_AfterSlow_UsesSlowThreshold()
{
// Slow already escalated to SlowExecutor
for (int i = 0; i < 3; i++)
_manager.ReportInferenceSlow(600);
_manager.Level.ShouldBe(DegradationLevel.SlowExecutor);
// Now report failures — should escalate to ExecutorDown faster
// (because consecutiveSlow already reached threshold)
_manager.ReportInferenceFailure();
_manager.ConsecutiveFailures.ShouldBe(1);
// Level should still be SlowExecutor (need 10 failures for ExecutorDown)
_manager.Level.ShouldBe(DegradationLevel.SlowExecutor);
}
}