Files
ThreadPilot/Tests/ThreadPilot.Core.Tests/PassiveProcessErrorThrottleTests.cs
2026-06-24 17:52:45 +02:00

60 lines
2.3 KiB
C#

namespace ThreadPilot.Core.Tests
{
using ThreadPilot.Services;
public sealed class PassiveProcessErrorThrottleTests
{
[Fact]
public void ShouldLog_ReturnsFalseForRepeatedErrorInsideTtl()
{
var now = new DateTimeOffset(2026, 5, 9, 12, 0, 0, TimeSpan.Zero);
var throttle = new PassiveProcessErrorThrottle(TimeSpan.FromMinutes(1), () => now);
Assert.True(throttle.ShouldLog(42, PassiveProcessErrorKind.AccessDenied));
Assert.False(throttle.ShouldLog(42, PassiveProcessErrorKind.AccessDenied));
}
[Fact]
public void ShouldLog_ReturnsTrueAfterTtlExpires()
{
var now = new DateTimeOffset(2026, 5, 9, 12, 0, 0, TimeSpan.Zero);
var throttle = new PassiveProcessErrorThrottle(TimeSpan.FromMinutes(1), () => now);
Assert.True(throttle.ShouldLog(42, PassiveProcessErrorKind.AccessDenied));
now = now.AddMinutes(2);
Assert.True(throttle.ShouldLog(42, PassiveProcessErrorKind.AccessDenied));
}
[Fact]
public void ShouldLog_TracksPidAndErrorKindSeparately()
{
var now = new DateTimeOffset(2026, 5, 9, 12, 0, 0, TimeSpan.Zero);
var throttle = new PassiveProcessErrorThrottle(TimeSpan.FromMinutes(1), () => now);
Assert.True(throttle.ShouldLog(42, PassiveProcessErrorKind.AccessDenied));
Assert.True(throttle.ShouldLog(42, PassiveProcessErrorKind.Terminated));
Assert.True(throttle.ShouldLog(43, PassiveProcessErrorKind.AccessDenied));
}
[Fact]
public void ShouldLog_WhenElapsedTimeEqualsTtl_ReturnsTrue()
{
var now = new DateTimeOffset(2026, 5, 9, 12, 0, 0, TimeSpan.Zero);
var throttle = new PassiveProcessErrorThrottle(TimeSpan.FromMinutes(1), () => now);
Assert.True(throttle.ShouldLog(42, PassiveProcessErrorKind.Unknown));
now = now.AddMinutes(1);
Assert.True(throttle.ShouldLog(42, PassiveProcessErrorKind.Unknown));
}
[Fact]
public void Constructor_WhenTtlIsZero_Throws()
{
Assert.Throws<ArgumentOutOfRangeException>(() =>
new PassiveProcessErrorThrottle(TimeSpan.Zero, () => DateTimeOffset.UtcNow));
}
}
}