Merge branch 'main' into fix/214-hangfire-startup-lock
Heimdall Gitea / Detect CI scope (pull_request) Successful in 5s
Heimdall Gitea / Build and tests (pull_request) Failing after 27m42s
Heimdall Gitea / Build and scan server image (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Skipped
Heimdall Gitea / Build and scan NVIDIA Agent image (pull_request) Skipped
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Failing after 2s

This commit is contained in:
arnaud.dartois committed 2026-11-07 10:59:58 +01:00
commit 1e32d585f5
14 files changed
+397 -25

No files matched your search

@@ -254,6 +254,36 @@ public class AgentRegistrationServiceTests : IDisposable
authorization.ShouldBeNull();
}
[Fact]
public async Task Heartbeat_HttpTimeout_DoesNotStopService_AndNextHeartbeatSucceeds()
{
var heartbeatCount = 0;
var handler = new AsyncMockHttpMessageHandler(async (request, cancellationToken) =>
{
if (request.RequestUri?.ToString().Contains("/heartbeat") == true &&
Interlocked.Increment(ref heartbeatCount) == 1)
{
await Task.Delay(Timeout.InfiniteTimeSpan, cancellationToken);
}
return request.RequestUri?.ToString().Contains("/register") == true
? CreateRegistrationResponse()
: new HttpResponseMessage(HttpStatusCode.OK);
});
var factory = CreateHttpClientFactory(handler, TimeSpan.FromMilliseconds(100));
var service = CreateService(factory);
await service.StartAsync(CancellationToken.None);
var deadline = DateTime.UtcNow.AddSeconds(8);
while (DateTime.UtcNow < deadline && Volatile.Read(ref heartbeatCount) < 2)
await Task.Delay(50);
await service.StopAsync(CancellationToken.None);
heartbeatCount.ShouldBeGreaterThanOrEqualTo(2);
}
public void Dispose()
{
_tmp.Dispose();
@@ -284,9 +314,14 @@ public class AgentRegistrationServiceTests : IDisposable
}
private static IHttpClientFactory CreateHttpClientFactory(
MockHttpMessageHandler handler)
HttpMessageHandler handler,
TimeSpan? timeout = null)
{
var client = new HttpClient(handler) { BaseAddress = new Uri("http://heimdall.test") };
var client = new HttpClient(handler)
{
BaseAddress = new Uri("http://heimdall.test"),
Timeout = timeout ?? TimeSpan.FromSeconds(100)
};
var factory = Substitute.For<IHttpClientFactory>();
factory.CreateClient("Central").Returns(client);
factory.CreateClient("IpResolver").Returns(client);
@@ -330,3 +365,14 @@ public class MockHttpMessageHandler(Func<HttpRequestMessage, HttpResponseMessage
return Task.FromResult(handler(request));
}
}
public sealed class AsyncMockHttpMessageHandler(
Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> handler)
: HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
return handler(request, cancellationToken);
}
}
@@ -42,4 +42,40 @@ public class NetworkCameraScannerTests
var scanner = new NetworkCameraScanner(logger, loggerFactory);
scanner.ShouldNotBeNull();
}
[Fact]
public async Task ConnectWithTimeoutAsync_CancelsAndObservesTimedOutConnect()
{
var connectCancellationObserved = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var connected = await NetworkCameraScanner.ConnectWithTimeoutAsync(async cancellationToken =>
{
try
{
await Task.Delay(Timeout.InfiniteTimeSpan, cancellationToken);
}
catch (OperationCanceledException)
{
connectCancellationObserved.TrySetResult();
throw;
}
}, TimeSpan.FromMilliseconds(25), CancellationToken.None);
connected.ShouldBeFalse();
await connectCancellationObserved.Task.WaitAsync(TimeSpan.FromSeconds(1));
}
[Fact]
public async Task ConnectWithTimeoutAsync_PropagatesCallerCancellation()
{
using var cancellation = new CancellationTokenSource();
var connecting = NetworkCameraScanner.ConnectWithTimeoutAsync(
token => new ValueTask(Task.Delay(Timeout.InfiniteTimeSpan, token)),
TimeSpan.FromSeconds(5),
cancellation.Token);
await cancellation.CancelAsync();
await Should.ThrowAsync<OperationCanceledException>(async () => await connecting);
}
}