2026-07-23 15:37:20 +00:00
|
|
|
using Heimdall.Agent.Services;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using NSubstitute;
|
2026-10-21 20:07:30 +02:00
|
|
|
using System.Runtime.InteropServices;
|
2026-07-23 15:37:20 +00:00
|
|
|
|
|
|
|
|
namespace Heimdall.Agent.Tests;
|
|
|
|
|
|
|
|
|
|
public class CameraStreamServiceTests
|
|
|
|
|
{
|
2026-07-24 06:38:15 +00:00
|
|
|
private const string FfmpegNotFound = "/usr/bin/nonexistent-ffmpeg";
|
|
|
|
|
|
2026-07-23 15:37:20 +00:00
|
|
|
[Fact]
|
|
|
|
|
public void Constructor_ActiveStreamCount_IsZero()
|
|
|
|
|
{
|
2026-07-24 06:38:15 +00:00
|
|
|
using var service = CreateService();
|
2026-07-23 15:37:20 +00:00
|
|
|
service.ActiveStreamCount.ShouldBe(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task StartStreamAsync_FfmpegNotFound_ReturnsFalse()
|
|
|
|
|
{
|
2026-07-24 06:38:15 +00:00
|
|
|
using var service = CreateService();
|
2026-07-23 15:37:20 +00:00
|
|
|
var result = await service.StartStreamAsync(Guid.NewGuid(), "rtsp://localhost:8554/test");
|
|
|
|
|
result.ShouldBeFalse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task StartStreamAsync_MultipleCalls_AllReturnFalse()
|
|
|
|
|
{
|
2026-07-24 06:38:15 +00:00
|
|
|
using var service = CreateService();
|
2026-07-23 15:37:20 +00:00
|
|
|
var id = Guid.NewGuid();
|
|
|
|
|
var first = await service.StartStreamAsync(id, "rtsp://localhost:8554/test");
|
|
|
|
|
var second = await service.StartStreamAsync(id, "rtsp://localhost:8554/test");
|
|
|
|
|
first.ShouldBeFalse();
|
|
|
|
|
second.ShouldBeFalse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task StopStreamAsync_UnknownCamera_DoesNotThrow()
|
|
|
|
|
{
|
2026-07-24 06:38:15 +00:00
|
|
|
using var service = CreateService();
|
2026-07-23 15:37:20 +00:00
|
|
|
await service.StopStreamAsync(Guid.NewGuid());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task ActiveStreamCount_AfterMultipleStarts_RemainsZero()
|
|
|
|
|
{
|
2026-07-24 06:38:15 +00:00
|
|
|
using var service = CreateService();
|
2026-07-23 15:37:20 +00:00
|
|
|
for (int i = 0; i < 5; i++)
|
|
|
|
|
{
|
|
|
|
|
await service.StartStreamAsync(Guid.NewGuid(), $"rtsp://camera{i}/test");
|
|
|
|
|
}
|
|
|
|
|
service.ActiveStreamCount.ShouldBe(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void Dispose_MultipleTimes_DoesNotThrow()
|
|
|
|
|
{
|
2026-07-24 06:38:15 +00:00
|
|
|
var service = CreateService();
|
2026-07-23 15:37:20 +00:00
|
|
|
service.Dispose();
|
|
|
|
|
service.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task StartStreamAsync_EmptyRtspUrl_ReturnsFalse()
|
|
|
|
|
{
|
2026-07-24 06:38:15 +00:00
|
|
|
using var service = CreateService();
|
2026-07-23 15:37:20 +00:00
|
|
|
var result = await service.StartStreamAsync(Guid.NewGuid(), string.Empty);
|
|
|
|
|
result.ShouldBeFalse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task StartStreamAsync_WithCancellationToken_RespectsCancellation()
|
|
|
|
|
{
|
2026-07-24 06:38:15 +00:00
|
|
|
using var service = CreateService();
|
2026-07-23 15:37:20 +00:00
|
|
|
await service.StartStreamAsync(Guid.NewGuid(), "rtsp://localhost:8554/test", CancellationToken.None);
|
|
|
|
|
service.ActiveStreamCount.ShouldBe(0);
|
|
|
|
|
}
|
2026-07-23 19:19:05 +00:00
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task StartStreamAsync_PreCancelledToken_ThrowsTaskCanceled()
|
|
|
|
|
{
|
2026-07-24 06:38:15 +00:00
|
|
|
using var service = CreateService();
|
2026-07-23 19:19:05 +00:00
|
|
|
var cts = new CancellationTokenSource();
|
|
|
|
|
cts.Cancel();
|
|
|
|
|
var ex = await Record.ExceptionAsync(() =>
|
|
|
|
|
service.StartStreamAsync(Guid.NewGuid(), "rtsp://localhost:8554/test", cts.Token));
|
|
|
|
|
ex.ShouldBeOfType<TaskCanceledException>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task StartStreamAsync_AfterDispose_ThrowsObjectDisposed()
|
|
|
|
|
{
|
2026-07-24 06:38:15 +00:00
|
|
|
var service = CreateService();
|
2026-07-23 19:19:05 +00:00
|
|
|
service.Dispose();
|
|
|
|
|
var ex = await Record.ExceptionAsync(() =>
|
|
|
|
|
service.StartStreamAsync(Guid.NewGuid(), "rtsp://localhost:8554/test"));
|
|
|
|
|
ex.ShouldBeOfType<ObjectDisposedException>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task StartStreamAsync_ConcurrentDifferentCameras_AllFail()
|
|
|
|
|
{
|
2026-07-24 06:38:15 +00:00
|
|
|
using var service = CreateService();
|
2026-07-23 19:19:05 +00:00
|
|
|
var tasks = Enumerable.Range(0, 10)
|
|
|
|
|
.Select(i => service.StartStreamAsync(Guid.NewGuid(), $"rtsp://camera{i}/test"))
|
|
|
|
|
.ToArray();
|
|
|
|
|
var results = await Task.WhenAll(tasks);
|
|
|
|
|
results.ShouldAllBe(r => r == false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task StartStreamAsync_ThenStop_ActiveCountIsZero()
|
|
|
|
|
{
|
2026-07-24 06:38:15 +00:00
|
|
|
using var service = CreateService();
|
2026-07-23 19:19:05 +00:00
|
|
|
var id = Guid.NewGuid();
|
|
|
|
|
await service.StartStreamAsync(id, "rtsp://localhost:8554/test");
|
|
|
|
|
await service.StopStreamAsync(id);
|
|
|
|
|
service.ActiveStreamCount.ShouldBe(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task StartStreamAsync_InvalidRtspUrl_ReturnsFalse()
|
|
|
|
|
{
|
2026-07-24 06:38:15 +00:00
|
|
|
using var service = CreateService();
|
2026-07-23 19:19:05 +00:00
|
|
|
var result = await service.StartStreamAsync(Guid.NewGuid(), "not-a-url");
|
|
|
|
|
result.ShouldBeFalse();
|
|
|
|
|
}
|
2026-07-24 06:38:15 +00:00
|
|
|
|
2026-08-11 21:16:28 +02:00
|
|
|
[Fact]
|
|
|
|
|
public void SnapshotChannel_IsBounded_AndWritable()
|
|
|
|
|
{
|
|
|
|
|
using var service = CreateService();
|
|
|
|
|
service.SnapshotChannel.ShouldNotBeNull();
|
|
|
|
|
service.SnapshotChannel.Writer.TryWrite(new Snapshot(Guid.NewGuid(), DateTime.UtcNow, [1, 2, 3])).ShouldBeTrue();
|
|
|
|
|
service.DroppedSnapshotCount.ShouldBe(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void DroppedSnapshotCount_InitialZero()
|
|
|
|
|
{
|
|
|
|
|
using var service = CreateService();
|
|
|
|
|
service.DroppedSnapshotCount.ShouldBe(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IsStreamActive_UnknownCamera_ReturnsFalse()
|
|
|
|
|
{
|
|
|
|
|
using var service = CreateService();
|
|
|
|
|
service.IsStreamActive(Guid.NewGuid()).ShouldBeFalse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ActiveCameraIds_InitialEmpty()
|
|
|
|
|
{
|
|
|
|
|
using var service = CreateService();
|
|
|
|
|
service.ActiveCameraIds.ShouldBeEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task StopAllAsync_WithoutStreams_DoesNotThrow()
|
|
|
|
|
{
|
|
|
|
|
using var service = CreateService();
|
|
|
|
|
await service.StopAllAsync();
|
2026-08-20 14:13:06 +02:00
|
|
|
service.ShouldNotBeNull();
|
2026-08-11 21:16:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task StartStreamAsync_InvalidUrl_DoesNotCreateSession()
|
|
|
|
|
{
|
|
|
|
|
using var service = CreateService();
|
|
|
|
|
await service.StartStreamAsync(Guid.NewGuid(), string.Empty);
|
|
|
|
|
service.ActiveStreamCount.ShouldBe(0);
|
|
|
|
|
service.IsStreamActive(Guid.NewGuid()).ShouldBeFalse();
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-21 20:07:30 +02:00
|
|
|
[Fact]
|
|
|
|
|
public async Task StartStreamAsync_DrainsLargeFfmpegStandardErrorOutput()
|
|
|
|
|
{
|
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var scriptPath = Path.Combine(Path.GetTempPath(), $"heimdall-ffmpeg-test-{Guid.NewGuid():N}.sh");
|
|
|
|
|
await File.WriteAllTextAsync(scriptPath, "#!/bin/sh\nhead -c 131072 /dev/zero >&2\n");
|
|
|
|
|
File.SetUnixFileMode(scriptPath, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var service = new CameraStreamService(
|
|
|
|
|
Substitute.For<ILogger<CameraStreamService>>(), scriptPath);
|
|
|
|
|
var started = await service.StartStreamAsync(Guid.NewGuid(), "rtsp://camera/test");
|
|
|
|
|
|
|
|
|
|
started.ShouldBeTrue();
|
|
|
|
|
var deadline = DateTime.UtcNow.AddSeconds(5);
|
|
|
|
|
while (service.ActiveStreamCount > 0 && DateTime.UtcNow < deadline)
|
|
|
|
|
await Task.Delay(25);
|
|
|
|
|
|
|
|
|
|
service.ActiveStreamCount.ShouldBe(0);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
File.Delete(scriptPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-11 21:16:28 +02:00
|
|
|
[Fact]
|
|
|
|
|
public void Constructor_WithSceneDependencies_DoesNotThrow()
|
|
|
|
|
{
|
|
|
|
|
using var service = new CameraStreamService(
|
|
|
|
|
Substitute.For<ILogger<CameraStreamService>>(),
|
|
|
|
|
FfmpegNotFound,
|
|
|
|
|
new SceneStateTracker(),
|
|
|
|
|
new SystemMetricsProvider(),
|
|
|
|
|
new AdaptiveSnapshotInterval());
|
|
|
|
|
service.ShouldNotBeNull();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-24 06:38:15 +00:00
|
|
|
private static CameraStreamService CreateService()
|
|
|
|
|
=> new(Substitute.For<ILogger<CameraStreamService>>(), FfmpegNotFound);
|
2026-07-23 15:37:20 +00:00
|
|
|
}
|