Files
Chronos/tests/Chronos.Data.Tests/Jobs/DetailRecorder.cs
T
Alby96 20d64af4a4
CI / Build and test (push) Successful in 11m36s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 12s
Release / Build, test and publish (push) Successful in 14m56s
CI / PostgreSQL 16 integration (push) Successful in 10m11s
CI / PostgreSQL 17 integration (push) Successful in 10m15s
CI / Docker image (push) Successful in 45s
Release / Server image (push) Successful in 4m55s
feat: single setup, mass-pass progress, per-server homes, Desktop shares its library (owner review 5, v1.1.0) (#68)
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-11-15 18:11:43 +01:00

63 lines
2.3 KiB
C#

using Chronos.Core.Model;
using Chronos.Data.Jobs;
namespace Chronos.Data.Tests.Jobs;
/// <summary>
/// Every progress report a job engine publishes, kept to tell which details a handler sent and in what order (owner review
/// 5, #65). The engine it records has no publish throttle (<see cref="Options"/>), so each report a handler makes goes out.
/// </summary>
internal sealed class DetailRecorder
{
private readonly List<JobProgressInfo> _published = [];
/// <summary>A started engine's options that publish every report: one worker, fast polls and checkpoints, no progress interval.</summary>
public static JobEngineOptions Options(string worker = "detail") => new()
{
Workers = 1,
WorkerId = worker,
PollInterval = TimeSpan.FromMilliseconds(50),
CheckpointInterval = TimeSpan.FromMilliseconds(50),
ProgressInterval = TimeSpan.Zero,
};
/// <summary>A new engine with <paramref name="handlers"/>, recorded, not started.</summary>
public JobEngine Engine(ChronosDatabase db, IEnumerable<IJobHandler> handlers, string worker = "detail")
{
var engine = new JobEngine(db, [.. handlers], Options(worker));
engine.Progress += p =>
{
lock (_published) _published.Add(p);
};
return engine;
}
/// <summary>The reports of <paramref name="jobId"/> in the order they went out.</summary>
public IReadOnlyList<JobProgressInfo> Of(Guid jobId)
{
lock (_published) return [.. _published.Where(p => p.JobId == jobId)];
}
/// <summary>The details of <paramref name="jobId"/>, without repeats of the one before.</summary>
public IReadOnlyList<JobDetail> Details(Guid jobId)
{
var details = new List<JobDetail>();
foreach (var report in Of(jobId))
{
if (report.Detail is { } detail && (details.Count == 0 || details[^1] != detail)) details.Add(detail);
}
return details;
}
/// <summary>The steps of <paramref name="jobId"/> in order, each once in a row.</summary>
public IReadOnlyList<JobStep> Steps(Guid jobId)
{
var steps = new List<JobStep>();
foreach (var detail in Details(jobId))
{
if (steps.Count == 0 || steps[^1] != detail.Step) steps.Add(detail.Step);
}
return steps;
}
}