Master-poll: fouten zichtbaar loggen (Warning bij eerste fout, Information bij herstel); tests dat master-status na fail-open direct terugkomt
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+75
@@ -349,6 +349,81 @@ public class MasterAvailabilityServiceTests : IDisposable
|
||||
status.DisableMessage.Should().BeNull();
|
||||
}
|
||||
|
||||
private MasterRegistration RegisteredLongAgo()
|
||||
{
|
||||
var existing = new MasterRegistration
|
||||
{
|
||||
Id = MasterRegistration.SingletonId,
|
||||
MasterUrl = "http://master.example.com",
|
||||
ApiKey = "enc:key",
|
||||
RegisteredAt = DateTimeOffset.UtcNow.AddHours(-3),
|
||||
LastPolledAt = DateTimeOffset.UtcNow.AddMinutes(-10)
|
||||
};
|
||||
_repo.GetAsync().Returns(existing);
|
||||
_protector.Unprotect("enc:key").Returns("key");
|
||||
return existing;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AfterAFailOpen_ASuccessfulPoll_AppliesTheMasterNotAvailableAgainAtOnce()
|
||||
{
|
||||
RegisteredLongAgo();
|
||||
await _svc.PushStatusAsync("key", false, "Onderhoud");
|
||||
await _svc.RecordPollFailureAsync(TimeSpan.FromMinutes(5));
|
||||
_svc.GetMasterStatus().IsAvailable.Should().BeTrue(); // failed open
|
||||
|
||||
await _svc.ApplyPolledStatusAsync(false, "Onderhoud"); // the master is reachable again
|
||||
|
||||
var status = _svc.GetMasterStatus();
|
||||
status.IsAvailable.Should().BeFalse();
|
||||
status.DisableMessage.Should().Be("Onderhoud");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AfterAFailOpen_APushAppliesTheMasterNotAvailableAgainAtOnce()
|
||||
{
|
||||
RegisteredLongAgo();
|
||||
await _svc.PushStatusAsync("key", false, "Onderhoud");
|
||||
await _svc.RecordPollFailureAsync(TimeSpan.FromMinutes(5));
|
||||
|
||||
(await _svc.PushStatusAsync("key", false, "Onderhoud")).Should().BeTrue();
|
||||
|
||||
_svc.GetMasterStatus().IsAvailable.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AfterTheRecovery_FurtherFailedPollsDoNotFailOpenAgainWithinTheGracePeriod()
|
||||
{
|
||||
RegisteredLongAgo();
|
||||
await _svc.PushStatusAsync("key", false, "Onderhoud");
|
||||
await _svc.RecordPollFailureAsync(TimeSpan.FromMinutes(5));
|
||||
await _svc.ApplyPolledStatusAsync(false, "Onderhoud"); // sets LastPolledAt = now
|
||||
|
||||
await _svc.RecordPollFailureAsync(TimeSpan.FromMinutes(5));
|
||||
|
||||
_svc.GetMasterStatus().IsAvailable.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReRegistration_NeverChangesTheMasterStatus_InEitherDirection()
|
||||
{
|
||||
var existing = RegisteredLongAgo();
|
||||
await _svc.PushStatusAsync("key", false, "Onderhoud");
|
||||
|
||||
(await _svc.RegisterAsync("https://master.example.com", "key")).Should().BeTrue(); // wrong URL corrected
|
||||
|
||||
existing.MasterUrl.Should().Be("https://master.example.com");
|
||||
var status = _svc.GetMasterStatus();
|
||||
status.IsAvailable.Should().BeFalse();
|
||||
status.DisableMessage.Should().Be("Onderhoud");
|
||||
|
||||
// the other way round: a slave that failed open stays Available until the master says otherwise
|
||||
existing.LastPolledAt = DateTimeOffset.UtcNow.AddMinutes(-10);
|
||||
await _svc.RecordPollFailureAsync(TimeSpan.FromMinutes(5));
|
||||
await _svc.RegisterAsync("https://master.example.com", "key");
|
||||
_svc.GetMasterStatus().IsAvailable.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task RecordPollFailureAsync_DoesNothing_WhenNoRegistration()
|
||||
{
|
||||
|
||||
@@ -50,6 +50,23 @@ public class MasterStatusPollClientTests
|
||||
result.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task LastFailure_NamesTheStatusOrTheExceptionType_NeverTheUrlOrKey()
|
||||
{
|
||||
var unauthorized = CreateSut(new FakeHttpMessageHandler(HttpStatusCode.Unauthorized));
|
||||
await unauthorized.GetStatusAsync(MasterUrl, ApiKey);
|
||||
unauthorized.LastFailure.Should().Be("http_401");
|
||||
|
||||
var refused = CreateSut(new ThrowingHttpMessageHandler());
|
||||
await refused.GetStatusAsync(MasterUrl, ApiKey);
|
||||
refused.LastFailure.Should().StartWith("HttpRequestException");
|
||||
refused.LastFailure.Should().NotContain("Connection refused").And.NotContain(ApiKey).And.NotContain("master.test");
|
||||
|
||||
var ok = CreateSut(new FakeHttpMessageHandler(HttpStatusCode.OK, JsonSerializer.Serialize(new { IsAvailable = true })));
|
||||
await ok.GetStatusAsync(MasterUrl, ApiKey);
|
||||
ok.LastFailure.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetStatusAsync_SetsApiKeyHeader()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user