Files
Chronos/tests/Titano.App.Tests/Jobs/JobListModelTests.cs
T
Alby96andClaude Opus 5.5 43b6871cdc feat: jobs run one at a time, parallelism per photo and a priority queue
Owner review 1, phase 2 (docs/requests/2026-10-15-owner-review-1.md, R2).

Jobs now run one at a time on the desktop and on the server. Parallelism
(Settings > Rendering, TITANO_PARALLELISM) is how many photos the running
job processes at once: thumbnail and proxy passes draw that many photos
together and still write the pack and checkpoints in frame order, decode
tiers are probed that many at a time, the deep analysis measures that many
frames and a render decodes that many ahead. Wherever originals are
developed whole (TIFF, PNG, RAW without a usable preview), fewer run at
once within the memory budget, which the running job now has whole; the
server's frame window is sized to the host instead of 1 GiB.

A new job waits its turn and pausing the running job starts the next one.
An optional priority queue gives each job kind a level (renders, imports
and downloads high, verifies and proxies low by default): a queued job of
a higher level pauses the running one without asking, and that job goes
back to the queue with its checkpoint and resumes by itself afterwards.
Jobs of the same level, and a job's own follow-ups, never interrupt it.
The Jobs screen and the admin panel show the parallelism and the queue;
Settings > Rendering and the admin panel's Settings set them, at once.

TITANO_WORKERS and the Workers key of server.json still set the
parallelism (1 to 64, with a warning at start for the variable).
Titano.Tools gains i18n prune, which removed the renamed keys from the 36
machine-translated languages; the new strings are translated in all 38.

Found while testing: uploads and imports failed when a pass read the
sidecar while they wrote it (atomic writes now retry the rename while a
reader holds the file, and never for a read-only one), and taking a lease
just released while another process read it failed on Windows (the create
now waits for the reader). Five adversarial review rounds checked the
phase before this commit.

Also updates BRIEF (12.8, 14.6, 14.8, 14.9, 24), DECISIONS, CHANGELOG,
ARCHITECTURE, STATUS, the request file and the compose/Unraid templates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 14:56:24 +02:00

234 lines
11 KiB
C#

using System.Globalization;
using Titano.App.Infrastructure;
using Titano.App.Jobs;
using Titano.Core.Localization;
using Titano.Core.Model;
using Titano.Data.Jobs;
using Titano.Testing;
namespace Titano.App.Tests.Jobs;
public sealed class JobListModelTests
{
private static readonly DateTimeOffset T0 = new(2026, 9, 28, 10, 0, 0, TimeSpan.Zero);
[Test]
public void OrderIsRunningPausedQueueThenNewestHistory()
{
var model = new JobListModel();
var oldDone = Job(JobState.Done, created: -60, started: -60, finished: -50);
var newDone = Job(JobState.Done, created: -40, started: -40, finished: -10);
var failed = Job(JobState.Failed, created: -30, started: -30, finished: -20);
var queuedLow = Job(JobState.Queued, created: -50, priority: JobPriority.Background);
var queuedOld = Job(JobState.Queued, created: -45);
var queuedNew = Job(JobState.Queued, created: -5);
var queuedUrgent = Job(JobState.Queued, created: -1, priority: JobPriority.User);
var paused = Job(JobState.Paused, created: -35, started: -35);
var runningLate = Job(JobState.Running, created: -8, started: -3);
var runningEarly = Job(JobState.Running, created: -9, started: -7);
foreach (var j in new[] { oldDone, newDone, failed, queuedLow, queuedOld, queuedNew, queuedUrgent, paused, runningLate, runningEarly }) model.Apply(j);
var order = model.Ordered().Select(e => e.Id).ToList();
Assert.SequenceEqual(
new[] { runningEarly, runningLate, paused, queuedUrgent, queuedOld, queuedNew, queuedLow, newDone, failed, oldDone }.Select(j => j.Id),
order);
Assert.Equal(1, model.QueuePosition(queuedUrgent.Id));
Assert.Equal(2, model.QueuePosition(queuedOld.Id));
Assert.Equal(4, model.QueuePosition(queuedLow.Id));
Assert.Equal(0, model.QueuePosition(runningEarly.Id));
Assert.Equal(2, model.RunningCount);
Assert.Equal(4, model.QueuedCount);
}
[Test]
public void FiltersCountAndHideClearedHistory()
{
var model = new JobListModel();
model.Apply(Job(JobState.Running, started: -1));
model.Apply(Job(JobState.Paused, started: -2));
model.Apply(Job(JobState.Queued));
model.Apply(Job(JobState.Done, started: -30, finished: -20));
model.Apply(Job(JobState.Done, started: -5, finished: -2));
model.Apply(Job(JobState.Failed, started: -6, finished: -4));
model.Apply(Job(JobState.Cancelled, started: -7, finished: -6));
Assert.Equal(7, model.CountOf(JobFilter.All));
Assert.Equal(2, model.CountOf(JobFilter.Running));
Assert.Equal(1, model.CountOf(JobFilter.Queued));
Assert.Equal(2, model.CountOf(JobFilter.Done));
Assert.Equal(2, model.CountOf(JobFilter.Failed));
var cleared = T0.AddMinutes(-10);
Assert.Equal(1, model.CountOf(JobFilter.Done, cleared));
Assert.Equal(6, model.Ordered(JobFilter.All, null, cleared).Count);
Assert.Equal(3, model.Ordered(JobFilter.All, null, T0).Count); // only the active jobs remain
}
[Test]
public void SearchMatchesTargetAndKind()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
var model = new JobListModel();
model.Apply(Job(JobState.Queued, kind: JobKind.Render, label: "Harbour sunset → 4K"));
model.Apply(Job(JobState.Queued, kind: JobKind.Verify, label: "Archive · 2024"));
Assert.Equal(1, model.Ordered(JobFilter.All, "harbour").Count);
Assert.Equal(1, model.Ordered(JobFilter.All, "verify").Count);
Assert.Equal(2, model.Ordered(JobFilter.All, " ").Count);
Assert.Empty(model.Ordered(JobFilter.All, "comet"));
}
[Test]
public void ProgressMergesIntoRunningJobsOnly()
{
var model = new JobListModel();
var running = Job(JobState.Running, started: -1, done: 10, total: 100);
var queued = Job(JobState.Queued);
model.Apply(running);
model.Apply(queued);
Assert.True(model.Apply(Progress(running.Id, 62, 100, 11.3, TimeSpan.FromMinutes(2))));
Assert.False(model.Apply(Progress(queued.Id, 5, 100)));
Assert.False(model.Apply(Progress(Guid.CreateVersion7(), 5, 100)));
var entry = model[running.Id]!;
Assert.Equal(62, entry.Done);
Assert.Approximately(0.62, entry.Fraction!.Value, 1e-9);
Assert.Equal(11.3, entry.ItemsPerSecond);
Assert.Same(entry, model.Current);
Assert.Equal(0, model[queued.Id]!.Done);
// Finishing drops the live progress and falls back to the stored snapshot.
model.Apply(running with { State = JobState.Done, ProgressDone = 100, Finished = T0 });
Assert.Null(model[running.Id]!.Live);
Assert.Equal(100, model[running.Id]!.Done);
Assert.Null(model.Current);
}
[Test]
public void ALoadedPageNeverOverwritesANewerEvent()
{
var model = new JobListModel();
var queued = Job(JobState.Queued);
var readAt = model.Stamp; // page requested
model.Apply(queued with { State = JobState.Running, Started = T0 }); // event arrives first
Assert.False(model.ApplyLoaded(queued, readAt)); // the stale page row is ignored
Assert.Equal(JobState.Running, model[queued.Id]!.State);
var other = Job(JobState.Done, started: -3, finished: -1);
Assert.True(model.ApplyLoaded(other, model.Stamp));
Assert.True(model.Apply(other with { State = JobState.Queued, Finished = null }));
Assert.Equal(JobState.Queued, model[other.Id]!.State);
}
[Test]
public void CurrentPrefersTheJobWithLiveProgress()
{
var model = new JobListModel();
var a = Job(JobState.Running, started: -10);
var b = Job(JobState.Running, started: -1);
model.Apply(a);
model.Apply(b);
Assert.Equal(a.Id, model.Current!.Id);
model.Apply(Progress(b.Id, 1, 2));
Assert.Equal(b.Id, model.Current!.Id);
}
[Test]
public void ClaimOrderMatchesTheRepository()
{
var older = Job(JobState.Queued, created: -10);
var newer = Job(JobState.Queued, created: -5);
var urgent = Job(JobState.Queued, created: -1, priority: JobPriority.User);
Assert.True(JobListModel.ClaimsBefore(older, newer));
Assert.True(JobListModel.ClaimsBefore(urgent, older));
Assert.False(JobListModel.ClaimsBefore(newer, older));
}
[Test]
public void ThePriorityQueueOrdersTheQueueAsTheEngineClaimsIt()
{
var model = new JobListModel();
var verify = Job(JobState.Queued, created: -30, priority: JobPriority.User, kind: JobKind.Verify);
var scan = Job(JobState.Queued, created: -20, kind: JobKind.Scan);
var render = Job(JobState.Queued, created: -1, kind: JobKind.Render);
foreach (var j in new[] { verify, scan, render }) model.Apply(j);
Assert.SequenceEqual(new[] { verify.Id, scan.Id, render.Id }, model.Ordered().Select(e => e.Id));
// Owner review 1: on, renders come first and verifies last, whatever their age and priority.
model.PriorityQueue = new JobPriorityQueue { Enabled = true };
Assert.SequenceEqual(new[] { render.Id, scan.Id, verify.Id }, model.Ordered().Select(e => e.Id));
Assert.Equal((1, 2, 3), (model.QueuePosition(render.Id), model.QueuePosition(scan.Id), model.QueuePosition(verify.Id)));
}
[Test]
public void TextsDescribeTheJob()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
var running = new JobEntry(Job(JobState.Running, kind: JobKind.Render, label: "Harbour sunset", started: -3))
{
Live = Progress(Guid.Empty, 2232, 3600, 11.3, TimeSpan.FromMinutes(2)),
};
Assert.Equal("Render · Harbour sunset · 2,232 / 3,600 · 11.3 fps · 2 min left", JobText.Bar(running));
Assert.Equal("2 min", JobText.Left(running, 0));
// Imports and verifies count bytes; scans count folders; frame jobs read in fps (owner review 1).
Assert.Equal("118 B/s", JobText.Speed(JobKind.Import, 118));
Assert.Equal(JobText.Speed(JobKind.Verify, 57_000_000), Loc.L("Jobs.Speed.Bytes", Formatting.Bytes(57_000_000)));
Assert.Equal("4/s", JobText.Speed(JobKind.Scan, 4));
Assert.Equal("1.7 fps", JobText.Speed(JobKind.Proxies, 1.7));
Assert.Equal(string.Empty, JobText.Speed(JobKind.Render, null));
Assert.Equal("this PC", JobText.Where("STUDIO#2", "studio"));
Assert.Equal("render-box", JobText.Where("render-box", "studio"));
var queued = new JobEntry(Job(JobState.Queued));
Assert.Equal("next", JobText.Left(queued, 1));
Assert.Equal("after 2", JobText.Left(queued, 3));
var done = new JobEntry(Job(JobState.Done, started: -10, finished: -7));
Assert.Equal("done · 3:00", JobText.State(done));
}
[Test]
public void TheWorkersReadoutCountsOnlyJobsOfThisPc()
{
var model = new JobListModel();
model.Apply(Job(JobState.Running, started: -1) with { NodeId = "STUDIO#0" });
model.Apply(Job(JobState.Running, started: -2) with { NodeId = "studio#3" });
model.Apply(Job(JobState.Running, started: -3) with { NodeId = "render-box#0" });
model.Apply(Job(JobState.Running, started: -4));
model.Apply(Job(JobState.Done, started: -9, finished: -8) with { NodeId = "studio#1" });
Assert.Equal(4, model.RunningCount);
Assert.Equal(2, model.RunningOn("studio"));
Assert.True(JobText.IsLocal("studio#2", "STUDIO"));
Assert.False(JobText.IsLocal("studio-2", "studio"));
Assert.False(JobText.IsLocal(null, "studio"));
}
[Test]
public void RowsAreNamedByKindAndTarget()
{
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
Assert.Equal("Render · Harbour sunset", JobText.Name(Job(JobState.Running, kind: JobKind.Render, label: "Harbour sunset")));
Assert.Equal("Proxies", JobText.Name(Job(JobState.Queued)));
}
private static Job Job(JobState state, int created = 0, int? started = null, int? finished = null, JobPriority priority = JobPriority.Normal,
JobKind kind = JobKind.Proxies, string? label = null, long done = 0, long total = 0) => new()
{
Id = Guid.CreateVersion7(),
Kind = kind,
State = state,
Priority = priority,
TargetLabel = label,
ProgressDone = done,
ProgressTotal = total,
Created = T0.AddMinutes(created),
Started = started is { } s ? T0.AddMinutes(s) : null,
Finished = finished is { } f ? T0.AddMinutes(f) : null,
};
private static JobProgressInfo Progress(Guid id, long done, long total, double? rate = null, TimeSpan? eta = null) =>
new(id, JobKind.Render, null, done, total, rate, eta, null);
}