165 lines
4.3 KiB
C#
165 lines
4.3 KiB
C#
using Heimdall.Domain.Entities;
|
|||
|
|
using Heimdall.Domain.Enums;
|
||
|
|
using Shouldly;
|
||
|
|
|
||
|
|
namespace Heimdall.Domain.Tests;
|
||
|
|
|
||
|
|
public class AnalysisJobTests
|
||
|
|
{
|
||
|
|
private static readonly Guid TestCameraId = Guid.NewGuid();
|
||
|
|
private static readonly Guid TestSiteId = Guid.NewGuid();
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void Constructor_SetsDefaultValues()
|
||
|
|
{
|
||
|
|
var job = new AnalysisJob(
|
||
|
|
TestCameraId, TestSiteId,
|
||
|
|
AnalysisJobType.MotionDetection,
|
||
|
|
AnalysisJobPriority.Periodic);
|
||
|
|
|
||
|
|
job.CameraId.ShouldBe(TestCameraId);
|
||
|
|
job.SiteId.ShouldBe(TestSiteId);
|
||
|
|
job.JobType.ShouldBe(AnalysisJobType.MotionDetection);
|
||
|
|
job.Priority.ShouldBe(AnalysisJobPriority.Periodic);
|
||
|
|
job.Status.ShouldBe(AnalysisJobStatus.Pending);
|
||
|
|
job.RetryCount.ShouldBe(0);
|
||
|
|
job.MaxRetries.ShouldBe(3);
|
||
|
|
job.MaxExecutionTime.ShouldBe(TimeSpan.FromMinutes(5));
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void Dispatch_SetsStatusAndTimestamp()
|
||
|
|
{
|
||
|
|
var job = CreatePendingJob();
|
||
|
|
var agentId = Guid.NewGuid();
|
||
|
|
|
||
|
|
job.Dispatch(agentId);
|
||
|
|
|
||
|
|
job.Status.ShouldBe(AnalysisJobStatus.Dispatched);
|
||
|
|
job.AssignedAgentId.ShouldBe(agentId);
|
||
|
|
job.DispatchedAt.ShouldNotBeNull();
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void Start_SetsRunningAndTimestamp()
|
||
|
|
{
|
||
|
|
var job = CreatePendingJob();
|
||
|
|
job.Dispatch(Guid.NewGuid());
|
||
|
|
|
||
|
|
job.Start();
|
||
|
|
|
||
|
|
job.Status.ShouldBe(AnalysisJobStatus.Running);
|
||
|
|
job.StartedAt.ShouldNotBeNull();
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void Complete_SetsCompletedWithResult()
|
||
|
|
{
|
||
|
|
var job = CreatePendingJob();
|
||
|
|
job.Dispatch(Guid.NewGuid());
|
||
|
|
job.Start();
|
||
|
|
|
||
|
|
job.Complete("{\"detections\": 5}");
|
||
|
|
|
||
|
|
job.Status.ShouldBe(AnalysisJobStatus.Completed);
|
||
|
|
job.ResultJson.ShouldBe("{\"detections\": 5}");
|
||
|
|
job.CompletedAt.ShouldNotBeNull();
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void Fail_BelowMaxRetries_ReturnsToPending()
|
||
|
|
{
|
||
|
|
var job = CreatePendingJob();
|
||
|
|
job.Dispatch(Guid.NewGuid());
|
||
|
|
job.Start();
|
||
|
|
|
||
|
|
job.Fail("Agent crashed");
|
||
|
|
|
||
|
|
job.Status.ShouldBe(AnalysisJobStatus.Pending);
|
||
|
|
job.RetryCount.ShouldBe(1);
|
||
|
|
job.AssignedAgentId.ShouldBeNull(); // Cleared for re-selection
|
||
|
|
job.ErrorMessage.ShouldBe("Agent crashed");
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void Fail_ExceedsMaxRetries_DeadLetters()
|
||
|
|
{
|
||
|
|
var job = CreatePendingJob();
|
||
|
|
job.SetMaxRetries(2);
|
||
|
|
|
||
|
|
// Fail twice
|
||
|
|
job.Dispatch(Guid.NewGuid());
|
||
|
|
job.Start();
|
||
|
|
job.Fail("Error 1");
|
||
|
|
job.Dispatch(Guid.NewGuid());
|
||
|
|
job.Start();
|
||
|
|
job.Fail("Error 2");
|
||
|
|
|
||
|
|
job.Status.ShouldBe(AnalysisJobStatus.DeadLettered);
|
||
|
|
job.RetryCount.ShouldBe(2);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void TimeOut_BelowMaxRetries_ReturnsToPending()
|
||
|
|
{
|
||
|
|
var job = CreatePendingJob();
|
||
|
|
job.Dispatch(Guid.NewGuid());
|
||
|
|
job.Start();
|
||
|
|
|
||
|
|
job.TimeOut();
|
||
|
|
|
||
|
|
job.Status.ShouldBe(AnalysisJobStatus.Pending);
|
||
|
|
job.RetryCount.ShouldBe(1);
|
||
|
|
job.ErrorMessage.ShouldNotBeNull();
|
||
|
|
job.ErrorMessage!.ShouldContain("exceeded maximum execution time");
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void TimeOut_ExceedsMaxRetries_DeadLetters()
|
||
|
|
{
|
||
|
|
var job = CreatePendingJob();
|
||
|
|
job.SetMaxRetries(1);
|
||
|
|
|
||
|
|
job.Dispatch(Guid.NewGuid());
|
||
|
|
job.Start();
|
||
|
|
job.TimeOut();
|
||
|
|
|
||
|
|
job.Status.ShouldBe(AnalysisJobStatus.DeadLettered);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void Cancel_SetsCancelled()
|
||
|
|
{
|
||
|
|
var job = CreatePendingJob();
|
||
|
|
|
||
|
|
job.Cancel();
|
||
|
|
|
||
|
|
job.Status.ShouldBe(AnalysisJobStatus.Cancelled);
|
||
|
|
job.CompletedAt.ShouldNotBeNull();
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void SetCapabilityRequirements_StoresValues()
|
||
|
|
{
|
||
|
|
var job = CreatePendingJob();
|
||
|
|
|
||
|
|
job.SetCapabilityRequirements(requiresGpu: true, requiredCpuCores: 4, requiredMemoryMb: 4096);
|
||
|
|
|
||
|
|
job.RequiresGpu.ShouldBeTrue();
|
||
|
|
job.RequiredCpuCores.ShouldBe(4);
|
||
|
|
job.RequiredMemoryMb.ShouldBe(4096);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public void SetTimeout_StoresValue()
|
||
|
|
{
|
||
|
|
var job = CreatePendingJob();
|
||
|
|
|
||
|
|
job.SetTimeout(TimeSpan.FromMinutes(10));
|
||
|
|
|
||
|
|
job.MaxExecutionTime.ShouldBe(TimeSpan.FromMinutes(10));
|
||
|
|
}
|
||
|
|
|
||
|
|
private static AnalysisJob CreatePendingJob() =>
|
||
|
|
new(TestCameraId, TestSiteId, AnalysisJobType.MotionDetection, AnalysisJobPriority.Periodic);
|
||
|
|
}
|