required"), a.Has("all")),
"import" => Import(root, catalog, a.Get("culture") ?? throw new ToolException("--culture required"),
a.Positional.ElementAtOrDefault(1) ?? throw new ToolException("TSV file required")),
+ "prune" => Prune(Path.Combine(root, "i18n"), catalog),
_ => throw new ToolException($"Unknown i18n subcommand '{sub}'"),
});
}
@@ -233,6 +236,44 @@ internal sealed partial class I18nCommand : ICommand
return 0;
}
+ ///
+ /// Removes the orphan keys of every translation under (those reports: not in
+ /// English and not a plural form of an English plural key), rewriting only the files that had some, as
+ /// writes them. English is the source and is never touched. Returns the number of keys removed.
+ ///
+ internal static int PruneOrphans(string dir, Catalog catalog)
+ {
+ var en = catalog.Cultures["en"];
+ var removed = 0;
+ foreach (var cultureDir in Directory.EnumerateDirectories(dir).Order(StringComparer.Ordinal))
+ {
+ var culture = Path.GetFileName(cultureDir);
+ if (culture.Equals("en", StringComparison.OrdinalIgnoreCase)) continue;
+ foreach (var path in Directory.EnumerateFiles(cultureDir, "*.resx").Order(StringComparer.Ordinal))
+ {
+ var values = new SortedDictionary(StringComparer.Ordinal);
+ var orphans = 0;
+ foreach (var data in XDocument.Load(path).Root!.Elements("data"))
+ {
+ var key = (string)data.Attribute("name")!;
+ if (en.ContainsKey(key) || PluralStem(key, en) is not null) values[key] = (string?)data.Element("value") ?? string.Empty;
+ else orphans++;
+ }
+ if (orphans == 0) continue;
+ WriteResx(path, values);
+ removed += orphans;
+ }
+ }
+ return removed;
+ }
+
+ private static int Prune(string dir, Catalog catalog)
+ {
+ var removed = PruneOrphans(dir, catalog);
+ Console.WriteLine($"i18n: {removed} orphan key(s) removed");
+ return 0;
+ }
+
private static void WriteResx(string path, IEnumerable> values)
{
var sb = new StringBuilder();
diff --git a/tests/Titano.App.Tests/Jobs/JobListModelTests.cs b/tests/Titano.App.Tests/Jobs/JobListModelTests.cs
index dc319bbf..8f6e898c 100644
--- a/tests/Titano.App.Tests/Jobs/JobListModelTests.cs
+++ b/tests/Titano.App.Tests/Jobs/JobListModelTests.cs
@@ -145,6 +145,22 @@ public sealed class JobListModelTests
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()
{
diff --git a/tests/Titano.App.Tests/Jobs/JobsScreenSmokeTests.cs b/tests/Titano.App.Tests/Jobs/JobsScreenSmokeTests.cs
index 863cb4e2..f2c854ce 100644
--- a/tests/Titano.App.Tests/Jobs/JobsScreenSmokeTests.cs
+++ b/tests/Titano.App.Tests/Jobs/JobsScreenSmokeTests.cs
@@ -36,8 +36,8 @@ public sealed class JobsScreenSmokeTests
Assert.Greater(running, 0);
Assert.Equal(running, jobs.Badge);
Assert.Equal(Loc.Plural("Shell.Status.Jobs", running), shell.JobsText);
- // The capture engine starts no workers: there is no "Workers n / 0" to show.
- Assert.False(jobs.HasWorkers);
+ // The capture engine runs no jobs: there is no "8 photos at once" to show.
+ Assert.False(jobs.HasEngine);
// Tab reaches every row button: the rows take no focus, the list passes Tab on to their content.
var list = app.FindAll().Single(l => l.Name == "JobList");
diff --git a/tests/Titano.App.Tests/Server/ServerScreenSmokeTests.cs b/tests/Titano.App.Tests/Server/ServerScreenSmokeTests.cs
index e68a4475..d37b187b 100644
--- a/tests/Titano.App.Tests/Server/ServerScreenSmokeTests.cs
+++ b/tests/Titano.App.Tests/Server/ServerScreenSmokeTests.cs
@@ -122,7 +122,8 @@ public sealed class ServerScreenSmokeTests
Assert.SequenceEqual([SyncAction.Pause], Actions(comet));
Assert.Contains("46", comet.ServerText);
Assert.Equal(StatusKind.Queued, Row(table, "Fog lifting, Asti").StateKind);
- Assert.Equal((StatusKind.Running, Loc.L("Server.Sync.State.Downloading")), (Row(table, "Sunrise over Superga").StateKind, Row(table, "Sunrise over Superga").StateText));
+ // One job at a time (owner review 1): the download halfway waits for the upload.
+ Assert.Equal((StatusKind.Queued, Loc.L("Server.Sync.State.Waiting")), (Row(table, "Sunrise over Superga").StateKind, Row(table, "Sunrise over Superga").StateText));
Assert.SequenceEqual([SyncAction.Resume], Actions(Row(table, "Lake Como storm")));
await app.ExpectAsync("server-sync");
diff --git a/tests/Titano.App.Tests/Settings/SettingsLogicTests.cs b/tests/Titano.App.Tests/Settings/SettingsLogicTests.cs
index 63f27905..7fe98207 100644
--- a/tests/Titano.App.Tests/Settings/SettingsLogicTests.cs
+++ b/tests/Titano.App.Tests/Settings/SettingsLogicTests.cs
@@ -181,34 +181,39 @@ public sealed class SettingsLogicTests
var settings = new Titano.App.Infrastructure.AppSettings { Quality = Titano.App.Infrastructure.QualityProfile.Best, Parallelism = 3, MemoryBudgetGb = 6 };
var basis = new Titano.Data.Processing.ProcessingOptions();
- // Three jobs at once share the 6 GB: 2 GB each; the user's 3 also caps the decodes and the analysis threads.
+ // One job runs at a time (owner review 1): it has the whole 6 GB, and decodes and measures 3 photos at once.
var best = ProcessingTuning.Apply(basis, settings, 64L << 30, 16, @"C:\cache");
- Assert.Equal((2L << 30, @"C:\cache", 8, 2), (best.FrameWindow.MemoryBudget, best.FrameWindow.SpillDirectory, best.FrameWindow.Ahead, best.FrameWindow.MaxConcurrentLoads));
+ Assert.Equal((6L << 30, @"C:\cache", 8, 3), (best.FrameWindow.MemoryBudget, best.FrameWindow.SpillDirectory, best.FrameWindow.Ahead, best.FrameWindow.MaxConcurrentLoads));
Assert.Equal((1024, Titano.Core.Project.FlowQualitySetting.Best), (best.Render.AnalysisLongEdge, best.Render.DefaultFlowQuality));
- Assert.Equal(int.Min(3, Titano.Core.Analysis.Deep.DeepAnalysisOptions.Default.MaxDegreeOfParallelism), best.DeepAnalysis.MaxDegreeOfParallelism);
+ Assert.Equal(3, best.DeepAnalysis.MaxDegreeOfParallelism);
settings.Parallelism = 1;
var single = ProcessingTuning.Apply(basis, settings, 64L << 30, 16, @"C:\cache");
Assert.Equal((6L << 30, 1, 1), (single.FrameWindow.MemoryBudget, single.FrameWindow.MaxConcurrentLoads, single.DeepAnalysis.MaxDegreeOfParallelism));
+ // A parallelism beyond the read-ahead widens the window, so the decodes have frames to work on.
+ settings.Parallelism = 12;
+ var wide = ProcessingTuning.Apply(basis, settings, 64L << 30, 16, @"C:\cache");
+ Assert.Equal((12, 12, 12), (wide.FrameWindow.Ahead, wide.FrameWindow.MaxConcurrentLoads, wide.DeepAnalysis.MaxDegreeOfParallelism));
- // Auto: a quarter of 32 GB shared by the engine's 8 workers on 16 cores; the jobs keep their own thread counts.
+ // Auto: a quarter of 32 GB, and half the 16 cores at once, with no cap of the analysis threads.
settings.Quality = Titano.App.Infrastructure.QualityProfile.Fast;
settings.MemoryBudgetGb = null;
settings.Parallelism = null;
var fast = ProcessingTuning.Apply(basis, settings, 32L << 30, 16, @"C:\cache");
- Assert.Equal((1L << 30, 4), (fast.FrameWindow.MemoryBudget, fast.FrameWindow.Ahead));
- Assert.Equal((basis.FrameWindow.MaxConcurrentLoads, basis.DeepAnalysis.MaxDegreeOfParallelism), (fast.FrameWindow.MaxConcurrentLoads, fast.DeepAnalysis.MaxDegreeOfParallelism));
+ Assert.Equal((8L << 30, 8), (fast.FrameWindow.MemoryBudget, fast.FrameWindow.Ahead));
+ Assert.Equal((8, 8), (fast.FrameWindow.MaxConcurrentLoads, fast.DeepAnalysis.MaxDegreeOfParallelism));
Assert.Equal((256, Titano.Core.Project.FlowQualitySetting.Draft), (fast.Render.AnalysisLongEdge, fast.Render.DefaultFlowQuality));
Assert.Equal(512, ProcessingTuning.PreparationLongEdge(Titano.App.Infrastructure.QualityProfile.Balanced));
}
[Test]
- public void ParallelismAutoIsTheJobEnginesDefault()
+ public void ParallelismAutoIsHalfTheCores()
{
- // BRIEF §14.6: workers default to half the cores; a 4-core laptop keeps two, so a long render never blocks a scan.
+ // Photos the running job processes at once (owner review 1): half the cores, the archive engine's own default.
Assert.Equal(8, ProcessingTuning.ParallelismAuto(16));
Assert.Equal(2, ProcessingTuning.ParallelismAuto(4));
Assert.Equal(1, ProcessingTuning.ParallelismAuto(1));
- Assert.Equal(new Titano.Data.Jobs.JobEngineOptions().Workers, ProcessingTuning.ParallelismAuto(Environment.ProcessorCount));
+ Assert.Equal(Titano.Data.Archiving.ArchiveServiceOptions.DefaultParallelism, ProcessingTuning.ParallelismAuto(Environment.ProcessorCount));
+ Assert.Equal(1, new Titano.Data.Jobs.JobEngineOptions().Workers);
Assert.Equal(8, ProcessingTuning.Parallelism(new Titano.App.Infrastructure.AppSettings(), 16));
Assert.Equal(5, ProcessingTuning.Parallelism(new Titano.App.Infrastructure.AppSettings { Parallelism = 5 }, 16));
}
diff --git a/tests/Titano.Core.Tests/Archiving/LeaseFileTests.cs b/tests/Titano.Core.Tests/Archiving/LeaseFileTests.cs
index 7b8e98b8..6ffa2012 100644
--- a/tests/Titano.Core.Tests/Archiving/LeaseFileTests.cs
+++ b/tests/Titano.Core.Tests/Archiving/LeaseFileTests.cs
@@ -26,6 +26,30 @@ public sealed class LeaseFileTests(TestContext context)
Assert.Equal("client", LeaseFile.Read(LeaseFile.PathFor(locks, "seq-1"))!.Owner);
}
+ [Test]
+ public void ALeaseReleasedWhileAReaderHoldsItIsTakenOnceTheReaderLetsGo()
+ {
+ // Found under load while testing owner review 1: on Windows the released file stays pending deletion while a reader
+ // has it open, and creating the name answers "access denied" meanwhile instead of succeeding or saying "taken".
+ var locks = context.CreateTempDirectory();
+ var held = LeaseFile.TryAcquire(locks, "seq-1", "server")!;
+ var path = LeaseFile.PathFor(locks, "seq-1");
+ using var opened = new ManualResetEventSlim();
+ var reader = new Thread(() =>
+ {
+ using var reading = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
+ opened.Set();
+ Thread.Sleep(30);
+ });
+ reader.Start();
+ Assert.True(opened.Wait(TimeSpan.FromSeconds(10)), "the reader opened the lease");
+ held.Dispose();
+ using var taken = LeaseFile.TryAcquire(locks, "seq-1", "client");
+ reader.Join();
+ Assert.NotNull(taken);
+ Assert.Equal("client", LeaseFile.Read(path)!.Owner);
+ }
+
[Test]
public void AnExpiredLeaseOfACrashedHolderIsTakenOver()
{
diff --git a/tests/Titano.Core.Tests/Archiving/ManifestStoreTests.cs b/tests/Titano.Core.Tests/Archiving/ManifestStoreTests.cs
index 702ea933..2fe46a45 100644
--- a/tests/Titano.Core.Tests/Archiving/ManifestStoreTests.cs
+++ b/tests/Titano.Core.Tests/Archiving/ManifestStoreTests.cs
@@ -58,6 +58,34 @@ public sealed class ManifestStoreTests(TestContext context)
Assert.Contains(field, json);
}
+ [Test]
+ public void ASidecarBeingReadIsReplacedOnceTheReaderIsDone()
+ {
+ // A thumbnail pass reads a sidecar without a lease while an upload commits to it (owner review 1). Windows refuses
+ // to rename over a file someone has open, so the write tries again for a moment instead of failing the upload.
+ var path = SidecarPath();
+ var first = Sample();
+ ManifestStore.WriteSequence(path, first);
+ // The reader runs on its own thread, so it lets go on time however busy the thread pool is.
+ using var opened = new ManualResetEventSlim();
+ string? read = null;
+ var reader = new Thread(() =>
+ {
+ using var reading = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete);
+ opened.Set();
+ Thread.Sleep(150);
+ using var old = new StreamReader(reading);
+ read = old.ReadToEnd();
+ });
+ reader.Start();
+ Assert.True(opened.Wait(TimeSpan.FromSeconds(10)), "the reader opened the sidecar");
+ ManifestStore.WriteSequence(path, first with { Title = "Rooftop" });
+ reader.Join();
+ Assert.Contains("Harbour sunset", read);
+ Assert.Equal("Rooftop", ManifestStore.ReadSequence(path).Title);
+ Assert.Contains("Rooftop", AtomicFile.ReadAllText(path));
+ }
+
[Test]
public void UnknownFieldsAndEnumValuesFromANewerBuildAreReadAndKept()
{
diff --git a/tests/Titano.Core.Tests/Foundation/AtomicFileTests.cs b/tests/Titano.Core.Tests/Foundation/AtomicFileTests.cs
index 62d1d6fb..ebbd914e 100644
--- a/tests/Titano.Core.Tests/Foundation/AtomicFileTests.cs
+++ b/tests/Titano.Core.Tests/Foundation/AtomicFileTests.cs
@@ -16,6 +16,47 @@ public sealed class AtomicFileTests(TestContext context)
Assert.Count(1, Directory.GetFiles(dir));
}
+ [Test]
+ [WindowsOnly]
+ public void AReadOnlyTargetFailsAtOnceAndTheTemporaryFileGoes()
+ {
+ // Only a handle that holds the target clears by itself (owner review 1); a read-only file never does, so the write
+ // must not keep the caller waiting (a settings field on the UI thread, a pass over hundreds of sidecars).
+ var dir = context.CreateTempDirectory();
+ var path = Path.Combine(dir, "settings.json");
+ AtomicFile.WriteAllText(path, "stable");
+ File.SetAttributes(path, FileAttributes.ReadOnly);
+ try
+ {
+ var clock = System.Diagnostics.Stopwatch.StartNew();
+ Assert.Throws(() => AtomicFile.WriteAllText(path, "new"));
+ Assert.Less(clock.Elapsed.TotalMilliseconds, 500, "no retries");
+ Assert.Equal("stable", File.ReadAllText(path));
+ Assert.Count(1, Directory.GetFiles(dir));
+ }
+ finally
+ {
+ File.SetAttributes(path, FileAttributes.Normal);
+ }
+ }
+
+ [Test]
+ public void ReadsAreWholeAndShareTheFileWithOtherReaders()
+ {
+ var dir = context.CreateTempDirectory();
+ var path = Path.Combine(dir, "project.json");
+ File.WriteAllText(path, "é");
+ using (new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete))
+ {
+ // A byte order mark is read as File.ReadAllText reads it.
+ Assert.Equal("é", AtomicFile.ReadAllText(path));
+ Assert.SequenceEqual(File.ReadAllBytes(path), AtomicFile.ReadAllBytes(path));
+ }
+ File.WriteAllText(path, string.Empty);
+ Assert.Equal(string.Empty, AtomicFile.ReadAllText(path));
+ Assert.Empty(AtomicFile.ReadAllBytes(path));
+ }
+
[Test]
public async Task FailedWriteKeepsPreviousContent()
{
diff --git a/tests/Titano.Data.Tests/Archiving/OrderedWorkTests.cs b/tests/Titano.Data.Tests/Archiving/OrderedWorkTests.cs
new file mode 100644
index 00000000..b0e70175
--- /dev/null
+++ b/tests/Titano.Data.Tests/Archiving/OrderedWorkTests.cs
@@ -0,0 +1,132 @@
+using Titano.Data.Archiving;
+using Titano.Testing;
+
+namespace Titano.Data.Tests.Archiving;
+
+/// Several photos of a pass at once (owner review 1, R2), their results taken in order.
+public sealed class OrderedWorkTests
+{
+ [Test]
+ public async Task ResultsAreTakenInOrderWhateverFinishesFirst()
+ {
+ var taken = new List();
+ await using (var work = new OrderedWork(4, n =>
+ {
+ taken.Add(n);
+ return ValueTask.CompletedTask;
+ }, CancellationToken.None))
+ {
+ for (var i = 0; i < 24; i++)
+ {
+ var n = i;
+ // The later items are the quicker ones.
+ await work.AddAsync(_ =>
+ {
+ Thread.Sleep((24 - n) * 2);
+ return n;
+ });
+ if (n % 5 == 0) await work.AddAsync(1000 + n);
+ }
+ await work.CompleteAsync();
+ }
+ int[] expected = [.. Enumerable.Range(0, 24).SelectMany(n => n % 5 == 0 ? new[] { n, 1000 + n } : [n])];
+ Assert.SequenceEqual(expected, taken);
+ }
+
+ [Test]
+ public async Task ExactlyTheParallelismRunsAtOnceAndOneRunsInline()
+ {
+ var running = 0;
+ var peak = 0;
+ await using (var work = new OrderedWork(3, _ => ValueTask.CompletedTask, CancellationToken.None))
+ {
+ for (var i = 0; i < 12; i++)
+ {
+ await work.AddAsync(_ =>
+ {
+ var now = Interlocked.Increment(ref running);
+ int seen;
+ while ((seen = Volatile.Read(ref peak)) < now && Interlocked.CompareExchange(ref peak, now, seen) != seen)
+ {
+ }
+ // Each waits for two others, which proves three run together whatever the load (the pool adds threads).
+ SpinWait.SpinUntil(() => Volatile.Read(ref peak) >= 3, TimeSpan.FromSeconds(10));
+ Thread.Sleep(2);
+ Interlocked.Decrement(ref running);
+ return 0;
+ });
+ }
+ await work.CompleteAsync();
+ }
+ Assert.Equal(3, peak);
+
+ var caller = Environment.CurrentManagedThreadId;
+ var threads = new List();
+ await using (var single = new OrderedWork(1, _ => ValueTask.CompletedTask, CancellationToken.None))
+ {
+ for (var i = 0; i < 3; i++)
+ {
+ await single.AddAsync(_ =>
+ {
+ threads.Add(Environment.CurrentManagedThreadId);
+ return 0;
+ });
+ }
+ await single.CompleteAsync();
+ }
+ Assert.SequenceEqual([caller, caller, caller], threads);
+ }
+
+ [Test]
+ public async Task AFailureStopsTheWorkStillRunningBeforeThePassEnds()
+ {
+ var started = 0;
+ var finished = 0;
+ var stopped = 0;
+ var taken = new List();
+ var work = new OrderedWork(4, n =>
+ {
+ taken.Add(n);
+ return ValueTask.CompletedTask;
+ }, CancellationToken.None);
+ var failure = await Assert.ThrowsAsync(async () =>
+ {
+ try
+ {
+ for (var i = 0; i < 8; i++)
+ {
+ var n = i;
+ await work.AddAsync(ct =>
+ {
+ Interlocked.Increment(ref started);
+ try
+ {
+ if (n == 2) throw new InvalidDataException("damaged photo");
+ if (n > 2)
+ {
+ // The photos after the damaged one take long, until the pass stops them.
+ if (ct.WaitHandle.WaitOne(TimeSpan.FromSeconds(10))) Interlocked.Increment(ref stopped);
+ ct.ThrowIfCancellationRequested();
+ }
+ return n;
+ }
+ finally
+ {
+ Interlocked.Increment(ref finished);
+ }
+ });
+ }
+ await work.CompleteAsync();
+ }
+ finally
+ {
+ await work.DisposeAsync();
+ }
+ });
+ Assert.Equal("damaged photo", failure.Message);
+ Assert.SequenceEqual([0, 1], taken);
+ // Nothing the pass started outlives it.
+ Assert.Equal(Volatile.Read(ref started), Volatile.Read(ref finished));
+ Assert.Equal(Volatile.Read(ref started) - 3, Volatile.Read(ref stopped));
+ }
+}
diff --git a/tests/Titano.Data.Tests/Archiving/ParallelPassTests.cs b/tests/Titano.Data.Tests/Archiving/ParallelPassTests.cs
new file mode 100644
index 00000000..7a28c0c6
--- /dev/null
+++ b/tests/Titano.Data.Tests/Archiving/ParallelPassTests.cs
@@ -0,0 +1,97 @@
+using Titano.Core.Archiving;
+using Titano.Data.Archiving;
+using Titano.Testing;
+using static Titano.Data.Tests.Archiving.ArchiveFixture;
+
+namespace Titano.Data.Tests.Archiving;
+
+///
+/// Thumbnail and proxy passes working on several photos at once (owner review 1, R2: the parallelism decides how many
+/// photos the running job processes at once): the same images as one at a time, written in frame order, and checkpoints
+/// that still mean every frame before them is done.
+///
+[Category("archive")]
+public sealed class ParallelPassTests(TestContext context)
+{
+ [Test]
+ public async Task SeveralPhotosAtOnceDrawTheSameThumbnailsInFrameOrder()
+ {
+ await using var fx = await CreateAsync(context);
+ // The pass commits its pack and checkpoints every 200 frames.
+ Photos.WriteSeries(Path.Combine(fx.Paths.Root, "a"), Start, 430);
+ await fx.ScanAsync();
+ var location = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single();
+ var sequence = (await fx.Db.Sequences.GetAsync(location.Id))!;
+ var folder = fx.Paths.Sequence("a");
+
+ var parallel = new ArchiveService(fx.Db, new ArchiveServiceOptions { Owner = "test", Parallelism = () => 4 });
+ var checkpoints = new List<(int Next, int Total)>();
+ var result = await parallel.Images.BuildThumbnailsAsync(fx.Archive, sequence, false, 0, (next, total) =>
+ {
+ checkpoints.Add((next, total));
+ return ValueTask.CompletedTask;
+ }, fx.Ct);
+ Assert.Equal((430, 0, 0), (result.Built, result.Reused, result.Undecodable));
+ Assert.SequenceEqual([(200, 430), (400, 430)], checkpoints);
+
+ Dictionary drawn;
+ using (var pack = ThumbPackReader.Open(folder.ThumbsPackPath))
+ {
+ var entries = pack.Entries.Values.OrderBy(e => e.FrameIndex).ToList();
+ Assert.Count(430, entries);
+ // Appended in frame order, as one at a time.
+ Assert.SequenceEqual(entries.Select(e => e.Offset).Order(), entries.Select(e => e.Offset));
+ drawn = entries.ToDictionary(e => e.FrameIndex, e => pack.Read(e.FrameIndex)!);
+ }
+
+ var single = new ArchiveService(fx.Db, new ArchiveServiceOptions { Owner = "test", Parallelism = () => 1 });
+ await single.Images.BuildThumbnailsAsync(fx.Archive, sequence, force: true, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
+ using (var pack = ThumbPackReader.Open(folder.ThumbsPackPath))
+ {
+ foreach (var (index, jpeg) in drawn) Assert.SequenceEqual(jpeg, pack.Read(index)!);
+ }
+ }
+
+ [Test]
+ public async Task SeveralPhotosAtOnceWriteEveryProxyAndAResumedPassChecksWhatTheEarlierRunDid()
+ {
+ await using var fx = await CreateAsync(context);
+ Photos.WriteSeries(Path.Combine(fx.Paths.Root, "a"), Start, 24);
+ await fx.ScanAsync();
+ var location = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single();
+ var sequence = (await fx.Db.Sequences.GetAsync(location.Id))!;
+ var folder = fx.Paths.Sequence("a");
+ var parallel = new ArchiveService(fx.Db, new ArchiveServiceOptions { Owner = "test", Parallelism = () => 6 });
+
+ // Resumed at frame 10: it makes the proxies from there on, and those before it, which the earlier run should have
+ // made, are missing, so the sequence is not built.
+ var resumed = await parallel.Images.BuildProxiesAsync(fx.Archive, sequence, false, 10, (_, _) => ValueTask.CompletedTask, fx.Ct);
+ Assert.Equal((14, 0, 0), (resumed.Built, resumed.Reused, resumed.Undecodable));
+ Assert.FileMissing(folder.Proxy(0));
+ Assert.FileExists(folder.Proxy(10));
+ Assert.False((await fx.Db.Sequences.GetAsync(location.Id))!.Status.HasFlag(Titano.Core.Model.SequenceStatus.ProxiesBuilt), "not built");
+
+ var full = await parallel.Images.BuildProxiesAsync(fx.Archive, sequence, false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct);
+ Assert.Equal((10, 14), (full.Built, full.Reused));
+ foreach (var frame in ManifestStore.ReadSequence(folder.ManifestPath).Frames)
+ Assert.True(ArchiveImages.IsProxyOf(folder.Proxy(frame.I), frame.CapturedAt, frame.W, frame.H, null), $"proxy of {frame.File}");
+ Assert.True((await fx.Db.Sequences.GetAsync(location.Id))!.Status.HasFlag(Titano.Core.Model.SequenceStatus.ProxiesBuilt), "built");
+ }
+
+ [Test]
+ public void LargeOriginalsDecodeFewerAtOnceWithinTheMemoryBudget()
+ {
+ // 24 MP takes some 768 MB while it is decoded.
+ const long sensor24 = 6000 * 4000;
+ Assert.Equal(2, DecodeBudget.PhotosAtOnce(8, 2L << 30, sensor24));
+ Assert.Equal(1, DecodeBudget.PhotosAtOnce(8, 512L << 20, sensor24));
+ Assert.Equal(8, DecodeBudget.PhotosAtOnce(8, 64L << 30, sensor24));
+ Assert.Equal(1, DecodeBudget.PhotosAtOnce(1, 64L << 30, sensor24));
+ // Small photos all fit; an unknown size counts as 24 MP.
+ Assert.Equal(8, DecodeBudget.PhotosAtOnce(8, 2L << 30, 48 * 32));
+ Assert.Equal(2, DecodeBudget.PhotosAtOnce(8, 2L << 30, 0));
+ // The size a damaged file claims never overflows: one decode at a time.
+ Assert.Equal(1, DecodeBudget.PhotosAtOnce(8, 64L << 30, 1L << 59));
+ Assert.Equal(1, DecodeBudget.PhotosAtOnce(8, 64L << 30, long.MaxValue));
+ }
+}
diff --git a/tests/Titano.Data.Tests/Jobs/JobEngineTests.cs b/tests/Titano.Data.Tests/Jobs/JobEngineTests.cs
index 67169ad1..c3222b16 100644
--- a/tests/Titano.Data.Tests/Jobs/JobEngineTests.cs
+++ b/tests/Titano.Data.Tests/Jobs/JobEngineTests.cs
@@ -252,63 +252,186 @@ public sealed class JobEngineTests(TestContext context)
Assert.Empty((await db.Jobs.ListAsync(null, JobKind.LightAnalyse)).Items);
}
- [Test]
- public async Task TheNumberOfWorkersChangesWhileTheEngineRuns()
+ /// Waits until holds, failing with after ten seconds.
+ private static async Task UntilAsync(Func condition, string what)
{
- await using var db = await OpenAsync();
- var running = 0;
- var started = 0;
- var release = new SemaphoreSlim(0);
- var handler = new CallbackHandler(JobKind.Thumbs, async c =>
+ var until = DateTime.UtcNow.AddSeconds(10);
+ while (!condition())
{
- Interlocked.Increment(ref running);
- Interlocked.Increment(ref started);
+ Assert.True(DateTime.UtcNow < until, what);
+ await Task.Delay(20);
+ }
+ }
+
+ /// A handler whose jobs run until released, counting those running and started, by kind.
+ private sealed class GatedHandler(JobKind kind, SemaphoreSlim release) : IJobHandler
+ {
+ public int Running;
+ public int Started;
+
+ public JobKind Kind => kind;
+
+ public async Task RunAsync(JobContext c)
+ {
+ Interlocked.Increment(ref Running);
+ Interlocked.Increment(ref Started);
try
{
await release.WaitAsync(c.Cancellation);
}
finally
{
- Interlocked.Decrement(ref running);
- }
- });
- await using var engine = new JobEngine(db, [handler], new JobEngineOptions { Workers = 1, WorkerId = "pc", PollInterval = TimeSpan.FromMilliseconds(50) });
- await engine.StartAsync();
- var jobs = new List();
- for (var i = 0; i < 6; i++) jobs.Add(await engine.EnqueueAsync(JobKind.Thumbs, []));
-
- async Task UntilAsync(Func condition, string what)
- {
- var until = DateTime.UtcNow.AddSeconds(10);
- while (!condition())
- {
- Assert.True(DateTime.UtcNow < until, what);
- await Task.Delay(20);
+ Interlocked.Decrement(ref Running);
}
}
+ }
- // One worker: one job at a time.
- await UntilAsync(() => Volatile.Read(ref running) == 1, "the first job runs");
+ [Test]
+ public async Task JobsRunOneAtATimeAndPausingStartsTheNext()
+ {
+ // Owner review 1 (R2): one job at a time by default; pausing the running job lets the next one start.
+ await using var db = await OpenAsync();
+ var release = new SemaphoreSlim(0);
+ var handler = new GatedHandler(JobKind.Thumbs, release);
+ await using var engine = new JobEngine(db, [handler], new JobEngineOptions { WorkerId = "pc", PollInterval = TimeSpan.FromMilliseconds(50) });
+ Assert.Equal(1, engine.Options.Workers);
+ await engine.StartAsync();
+ var jobs = new List();
+ for (var i = 0; i < 3; i++) jobs.Add(await engine.EnqueueAsync(JobKind.Thumbs, []));
+
+ await UntilAsync(() => Volatile.Read(ref handler.Running) == 1, "the first job runs");
await Task.Delay(300);
- Assert.Equal(1, Volatile.Read(ref running));
+ Assert.Equal((1, 1), (Volatile.Read(ref handler.Running), Volatile.Read(ref handler.Started)));
+ Assert.Count(1, engine.RunningJobIds);
+ var first = engine.RunningJobIds.First();
+ Assert.Equal("pc", (await db.Jobs.GetAsync(first))!.NodeId);
- // Three workers: two more jobs start at once, and no fourth.
- engine.SetWorkers(3);
- Assert.Equal(3, engine.Workers);
- await UntilAsync(() => Volatile.Read(ref running) == 3, "three jobs run together");
+ await engine.PauseAsync(first);
+ await UntilAsync(() => Volatile.Read(ref handler.Started) == 2 && Volatile.Read(ref handler.Running) == 1, "the next job starts");
+ Assert.Equal(JobState.Paused, (await db.Jobs.GetAsync(first))!.State);
+
+ release.Release(10);
+ foreach (var job in jobs.Where(j => j.Id != first)) await WaitForStateAsync(db, job.Id, JobState.Done, TimeSpan.FromSeconds(10));
+ Assert.Equal(JobState.Paused, (await db.Jobs.GetAsync(first))!.State);
+ }
+
+ [Test]
+ public async Task WithoutThePriorityQueueARunningJobIsNeverInterrupted()
+ {
+ await using var db = await OpenAsync();
+ var release = new SemaphoreSlim(0);
+ var scan = new GatedHandler(JobKind.Scan, release);
+ var render = new GatedHandler(JobKind.Render, release);
+ await using var engine = new JobEngine(db, [scan, render], new JobEngineOptions { WorkerId = "pc", PollInterval = TimeSpan.FromMilliseconds(50) });
+ await engine.StartAsync();
+ var scanning = await engine.EnqueueAsync(JobKind.Scan, []);
+ await UntilAsync(() => Volatile.Read(ref scan.Running) == 1, "the scan runs");
+ var rendering = await engine.EnqueueAsync(JobKind.Render, [], priority: JobPriority.User);
+ await Task.Delay(400);
+ Assert.Equal((1, 0), (Volatile.Read(ref scan.Running), Volatile.Read(ref render.Started)));
+ Assert.Equal(JobState.Queued, (await db.Jobs.GetAsync(rendering.Id))!.State);
+
+ release.Release(10);
+ await WaitForStateAsync(db, scanning.Id, JobState.Done, TimeSpan.FromSeconds(10));
+ await WaitForStateAsync(db, rendering.Id, JobState.Done, TimeSpan.FromSeconds(10));
+ Assert.Equal(1, Volatile.Read(ref scan.Started));
+ }
+
+ [Test]
+ public async Task AHigherJobPausesTheRunningOneWhichResumesFromItsCheckpoint()
+ {
+ // Owner review 1 (R2): with render at the highest level, starting a render while a scan runs pauses the scan
+ // without asking, renders, and resumes the scan by itself where it was.
+ await using var db = await OpenAsync();
+ var checkpointed = new TaskCompletionSource();
+ var go = new SemaphoreSlim(0);
+ var scan = new CountingHandler(JobKind.Scan, 4, async (i, ct) =>
+ {
+ if (i < 2) return;
+ checkpointed.TrySetResult();
+ await go.WaitAsync(ct);
+ });
+ var renderRelease = new SemaphoreSlim(0);
+ var render = new GatedHandler(JobKind.Render, renderRelease);
+ var queue = new JobPriorityQueue { Enabled = true };
+ Assert.Equal((JobKindPriority.High, JobKindPriority.Normal), (queue.LevelOf(JobKind.Render), queue.LevelOf(JobKind.Scan)));
+ await using var engine = new JobEngine(db, [scan, render],
+ new JobEngineOptions { WorkerId = "pc", PollInterval = TimeSpan.FromMilliseconds(50), PriorityQueue = () => queue });
+ await engine.StartAsync();
+ var scanning = await engine.EnqueueAsync(JobKind.Scan, []);
+ await checkpointed.Task.WaitAsync(TimeSpan.FromSeconds(10));
+
+ var rendering = await engine.EnqueueAsync(JobKind.Render, [], priority: JobPriority.User);
+ await UntilAsync(() => Volatile.Read(ref render.Running) == 1, "the render starts at once");
+ var paused = await db.Jobs.GetAsync(scanning.Id);
+ Assert.Equal((JobState.Queued, "2", (string?)null), (paused!.State, paused.Checkpoint, paused.NodeId));
+ Assert.Contains(Titano.Core.Localization.Loc.L("Jobs.Kind.Render"), paused.LogTail);
+
+ // The scan waits while the render runs, then resumes from its checkpoint without being asked.
+ go.Release(10);
await Task.Delay(300);
- Assert.Equal(3, Volatile.Read(ref running));
+ Assert.Equal(JobState.Queued, (await db.Jobs.GetAsync(scanning.Id))!.State);
+ renderRelease.Release();
+ await WaitForStateAsync(db, rendering.Id, JobState.Done, TimeSpan.FromSeconds(10));
+ await WaitForStateAsync(db, scanning.Id, JobState.Done, TimeSpan.FromSeconds(10));
+ lock (scan.SeenCheckpoints) Assert.SequenceEqual([null, "2"], scan.SeenCheckpoints);
+ Assert.Equal(1, scan.Completed);
+ }
- // Back to one: the running jobs finish undisturbed, then a single worker carries on.
- engine.SetWorkers(1);
- release.Release(3);
- await UntilAsync(() => Volatile.Read(ref started) == 4 && Volatile.Read(ref running) == 1, "one job after the three");
- await Task.Delay(300);
- Assert.Equal((4, 1), (Volatile.Read(ref started), Volatile.Read(ref running)));
+ [Test]
+ public async Task OnlyAHigherLevelInterruptsAndNeverTheJobsOwnFollowUps()
+ {
+ await using var db = await OpenAsync();
+ var release = new SemaphoreSlim(0);
+ var queued = new TaskCompletionSource();
+ // The light analysis queues a deep analysis of the same sequence, which the user put higher.
+ var light = new CallbackHandler(JobKind.LightAnalyse, async c =>
+ {
+ queued.TrySetResult(await c.EnqueueFollowUpAsync(JobKind.DeepAnalyse, [Guid.NewGuid()], "Roof", null, JobPriority.Background));
+ await release.WaitAsync(c.Cancellation);
+ });
+ var deep = new GatedHandler(JobKind.DeepAnalyse, release);
+ var thumbs = new GatedHandler(JobKind.Thumbs, release);
+ var queue = new JobPriorityQueue
+ {
+ Enabled = true,
+ Levels = new Dictionary { [JobKind.DeepAnalyse] = JobKindPriority.High, [JobKind.Thumbs] = JobKindPriority.Normal },
+ };
+ await using var engine = new JobEngine(db, [light, deep, thumbs],
+ new JobEngineOptions { WorkerId = "pc", PollInterval = TimeSpan.FromMilliseconds(50), PriorityQueue = () => queue });
+ await engine.StartAsync();
+ var analysing = await engine.EnqueueAsync(JobKind.LightAnalyse, []);
+ var followUp = Assert.NotNull(await queued.Task.WaitAsync(TimeSpan.FromSeconds(10)));
+ // A job of the same level waits too.
+ var drawing = await engine.EnqueueAsync(JobKind.Thumbs, [], priority: JobPriority.User);
+ await Task.Delay(400);
+ Assert.Equal(JobState.Running, (await db.Jobs.GetAsync(analysing.Id))!.State);
+ Assert.Equal((0, 0), (Volatile.Read(ref deep.Started), Volatile.Read(ref thumbs.Started)));
+ Assert.False(await engine.ReviewAsync(context.Cancellation));
- release.Release(3);
- foreach (var job in jobs) await WaitForStateAsync(db, job.Id, JobState.Done, TimeSpan.FromSeconds(10));
- Assert.Throws(() => engine.SetWorkers(0));
+ // Another deep analysis, not its own, interrupts it.
+ var other = await engine.EnqueueAsync(JobKind.DeepAnalyse, [Guid.NewGuid()], priority: JobPriority.User);
+ await UntilAsync(() => Volatile.Read(ref deep.Running) == 1, "a higher job runs");
+ Assert.Equal(JobState.Queued, (await db.Jobs.GetAsync(analysing.Id))!.State);
+ release.Release(20);
+ foreach (var id in new[] { analysing.Id, followUp.Id, drawing.Id, other.Id }) await WaitForStateAsync(db, id, JobState.Done, TimeSpan.FromSeconds(10));
+ }
+
+ [Test]
+ public async Task RequeueingAYieldedJobLeavesPausesAndOtherNodesAlone()
+ {
+ await using var db = await OpenAsync();
+ var job = new Job { Id = Guid.CreateVersion7(), Kind = JobKind.Verify, State = JobState.Queued, Created = DateTimeOffset.UtcNow };
+ await db.Jobs.EnqueueAsync(job);
+ Assert.NotNull(await db.Jobs.ClaimNextAsync("pc"));
+ Assert.False(await db.Jobs.RequeueAsync(job.Id, "other"));
+ await db.Jobs.SetStateAsync(job.Id, JobState.Paused);
+ Assert.False(await db.Jobs.RequeueAsync(job.Id, "pc"));
+ Assert.Equal(JobState.Paused, (await db.Jobs.GetAsync(job.Id))!.State);
+ await db.Jobs.SetStateAsync(job.Id, JobState.Queued);
+ Assert.NotNull(await db.Jobs.ClaimNextAsync("pc"));
+ Assert.True(await db.Jobs.RequeueAsync(job.Id, "pc"));
+ Assert.Equal((JobState.Queued, (string?)null), ((await db.Jobs.GetAsync(job.Id))!.State, (await db.Jobs.GetAsync(job.Id))!.NodeId));
}
[Test]
diff --git a/tests/Titano.Data.Tests/Jobs/JobPriorityQueueTests.cs b/tests/Titano.Data.Tests/Jobs/JobPriorityQueueTests.cs
new file mode 100644
index 00000000..877cac3e
--- /dev/null
+++ b/tests/Titano.Data.Tests/Jobs/JobPriorityQueueTests.cs
@@ -0,0 +1,106 @@
+using Titano.Core.Model;
+using Titano.Data.Jobs;
+using Titano.Testing;
+
+namespace Titano.Data.Tests.Jobs;
+
+/// The priority queue of the settings (owner review 1, R2): levels per kind of job, the claim order and what interrupts.
+[Category("jobs")]
+public sealed class JobPriorityQueueTests
+{
+ private static readonly DateTimeOffset T0 = new(2026, 10, 16, 9, 0, 0, TimeSpan.Zero);
+
+ [Test]
+ public void RendersImportsAndDownloadsComeFirstAndTheLongResumablePassesLast()
+ {
+ var queue = new JobPriorityQueue();
+ Assert.Equal(JobKindPriority.High, queue.LevelOf(JobKind.Render));
+ Assert.Equal(JobKindPriority.High, queue.LevelOf(JobKind.Import));
+ Assert.Equal(JobKindPriority.High, queue.LevelOf(JobKind.Download));
+ Assert.Equal(JobKindPriority.Low, queue.LevelOf(JobKind.Verify));
+ Assert.Equal(JobKindPriority.Low, queue.LevelOf(JobKind.Proxies));
+ foreach (var kind in new[] { JobKind.Scan, JobKind.Thumbs, JobKind.LightAnalyse, JobKind.DeepAnalyse, JobKind.Maintenance })
+ Assert.Equal(JobKindPriority.Normal, queue.LevelOf(kind));
+ // A level the user set wins over the default.
+ Assert.Equal(JobKindPriority.Low, (queue with { Levels = new Dictionary { [JobKind.Render] = JobKindPriority.Low } }).LevelOf(JobKind.Render));
+ }
+
+ [Test]
+ public void OnlyAHigherLevelInterruptsAndOnlyWhileTheQueueIsOn()
+ {
+ var on = new JobPriorityQueue { Enabled = true };
+ Assert.True(on.Interrupts(JobKind.Render, JobKind.Scan));
+ Assert.True(on.Interrupts(JobKind.Scan, JobKind.Verify));
+ Assert.False(on.Interrupts(JobKind.Scan, JobKind.Render));
+ Assert.False(on.Interrupts(JobKind.Thumbs, JobKind.Scan));
+ Assert.False(on.Interrupts(JobKind.Import, JobKind.Render));
+ Assert.False(JobPriorityQueue.Off.Interrupts(JobKind.Render, JobKind.Verify));
+ }
+
+ [Test]
+ public void TheQueueOrdersByLevelThenPriorityThenAge()
+ {
+ var on = new JobPriorityQueue { Enabled = true };
+ var oldVerify = Job(JobKind.Verify, JobPriority.User, -30);
+ var backgroundScan = Job(JobKind.Scan, JobPriority.Background, -20);
+ var userThumbs = Job(JobKind.Thumbs, JobPriority.User, -10);
+ var render = Job(JobKind.Render, JobPriority.Normal, 0);
+ Job[] jobs = [oldVerify, backgroundScan, userThumbs, render];
+
+ Assert.SequenceEqual([render, userThumbs, backgroundScan, oldVerify], Sorted(jobs, on));
+ // Off, the strict queue of before: priority, then age.
+ Assert.SequenceEqual([oldVerify, userThumbs, render, backgroundScan], Sorted(jobs, JobPriorityQueue.Off));
+ // Two jobs alike fall back to the id.
+ var twin = render with { Id = Guid.CreateVersion7() };
+ Assert.True(on.ClaimsBefore(render, twin) != on.ClaimsBefore(twin, render));
+ }
+
+ [Test]
+ public void SettingsKeepOnlyTheLevelsThatDifferAndSkipWhatTheyCannotRead()
+ {
+ var queue = JobPriorityQueue.From(true, new Dictionary
+ {
+ ["deep-analyse"] = "high",
+ ["Scan"] = "LOW",
+ ["render"] = "high",
+ ["teleport"] = "high",
+ ["verify"] = "urgent",
+ ["99"] = "low",
+ });
+ Assert.True(queue.Enabled);
+ Assert.Equal((JobKindPriority.High, JobKindPriority.Low, JobKindPriority.Low), (queue.LevelOf(JobKind.DeepAnalyse), queue.LevelOf(JobKind.Scan), queue.LevelOf(JobKind.Verify)));
+
+ var stored = JobPriorityQueue.Changed(Enum.GetValues().ToDictionary(k => k, queue.LevelOf));
+ Assert.SequenceEqual(["deep-analyse", "scan"], stored.Keys.Order(StringComparer.Ordinal));
+ Assert.Equal(("high", "low"), (stored["deep-analyse"], stored["scan"]));
+ var reread = JobPriorityQueue.From(true, stored);
+ foreach (var kind in Enum.GetValues()) Assert.Equal(queue.LevelOf(kind), reread.LevelOf(kind));
+ Assert.Equal(JobKindPriority.High, JobPriorityQueue.From(false, stored).LevelOf(JobKind.DeepAnalyse));
+ Assert.False(JobPriorityQueue.From(false, null).Enabled);
+ }
+
+ [Test]
+ public void TheClaimOrderOfTheDatabaseListsOnlyTheLevelsOtherThanNormal()
+ {
+ var on = new JobPriorityQueue { Enabled = true };
+ Assert.Equal(string.Empty, JobPriorityQueue.Off.OrderSql([JobKind.Render, JobKind.Verify]));
+ Assert.Equal(string.Empty, on.OrderSql([JobKind.Scan, JobKind.Thumbs]));
+ Assert.Equal("CASE WHEN kind IN (7) THEN 2 WHEN kind IN (2) THEN 0 ELSE 1 END DESC, ", on.OrderSql([JobKind.Render, JobKind.Verify, JobKind.Scan, JobKind.Render]));
+ }
+
+ private static Job[] Sorted(Job[] jobs, JobPriorityQueue queue)
+ {
+ var list = jobs.ToList();
+ list.Sort((a, b) => a.Id == b.Id ? 0 : queue.ClaimsBefore(a, b) ? -1 : 1);
+ return [.. list];
+ }
+
+ private static Job Job(JobKind kind, JobPriority priority, int minutes) => new()
+ {
+ Id = Guid.CreateVersion7(),
+ Kind = kind,
+ Priority = priority,
+ State = JobState.Queued,
+ Created = T0.AddMinutes(minutes),
+ };
+}
diff --git a/tests/Titano.Data.Tests/Processing/DecodeParallelismTests.cs b/tests/Titano.Data.Tests/Processing/DecodeParallelismTests.cs
new file mode 100644
index 00000000..1e49c38b
--- /dev/null
+++ b/tests/Titano.Data.Tests/Processing/DecodeParallelismTests.cs
@@ -0,0 +1,176 @@
+using Titano.Core.Analysis.Deep;
+using Titano.Core.Archiving;
+using Titano.Core.Imaging;
+using Titano.Core.Imaging.Decoders;
+using Titano.Core.Model;
+using Titano.Data.Archiving;
+using Titano.Data.Processing;
+using Titano.Testing;
+using SequenceFrames = Titano.Data.Processing.SequenceFrames;
+
+namespace Titano.Data.Tests.Processing;
+
+///
+/// Photos decoded at once (owner review 1, R2): the whole parallelism, unless developing large originals whole would
+/// exceed the memory budget. Only the decodes that develop an original count: a JPEG at least twice the size asked for
+/// decodes scaled down, and a frame read through its proxy or an embedded preview of that size needs no development.
+///
+public sealed class DecodeParallelismTests
+{
+ /// 2 GiB: two 24 MP developments (some 768 MB each), one of 45 MP.
+ private const long Budget = 2L << 30;
+
+ private static readonly ImageDecoder Server = DecoderFactory.CreateServer();
+
+ [Test]
+ public void WhatADecodeDevelopsWholeDependsOnTheFormatTheSizeAskedForAndThePreview()
+ {
+ const int w = 6000, h = 4000;
+ Assert.Equal(0, DecodeBudget.Developed(ImageFileFormat.Jpeg, w, h, 0, 320, preferPreview: false));
+ Assert.Equal(0, DecodeBudget.Developed(ImageFileFormat.Jpeg, w, h, 0, 3000, preferPreview: false));
+ // A 4K render of a 24 MP JPEG decodes it nearly whole; full size (0) always does.
+ Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Jpeg, w, h, 0, 3840, preferPreview: false));
+ Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Jpeg, w, h, 0, 0, preferPreview: true));
+ // A preview reaching the size serves only decodes that prefer previews; a render develops the RAW.
+ Assert.Equal(0, DecodeBudget.Developed(ImageFileFormat.Cr2, w, h, 1620, 512, preferPreview: true));
+ Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Cr2, w, h, 1620, 512, preferPreview: false));
+ Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Cr2, w, h, 1620, 2048, preferPreview: true));
+ Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Tiff, w, h, 0, 320, preferPreview: true));
+ // Only RAW decoders take a preview by choice: a TIFF's or a HEIF's is never a shortcut.
+ Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Tiff, w, h, w, 512, preferPreview: true));
+ Assert.Equal(24_000_000, DecodeBudget.Developed(ImageFileFormat.Heif, w, h, 512, 512, preferPreview: true));
+ Assert.Equal(0, DecodeBudget.Developed(ImageFileFormat.Dng, w, h, 1024, 512, preferPreview: true));
+ Assert.Equal(DecodeBudget.UnknownPixels, DecodeBudget.Developed(ImageFileFormat.Png, 0, 0, 0, 320, preferPreview: true));
+ }
+
+ [Test]
+ public void ThumbnailAndProxyPassesHoldBackOnlyForTheOriginalsTheyDevelop()
+ {
+ static SidecarFrame Frame(ImageFileFormat format, int width, int height, int? preview = null) =>
+ new() { Id = Guid.NewGuid(), File = "f", Format = format, W = width, H = height, PreviewW = preview, PreviewH = preview * 2 / 3 };
+ static int Thumbnails(params SidecarFrame[] frames) => ArchiveImages.PassParallelism(8, Budget, frames, 320, preferPreview: true, Server);
+ static int Proxies(int edge, params SidecarFrame[] frames) => ArchiveImages.PassParallelism(8, Budget, frames, edge, preferPreview: false, Server);
+
+ Assert.Equal(8, Thumbnails(Frame(ImageFileFormat.Jpeg, 8192, 5464), Frame(ImageFileFormat.Cr2, 8192, 5464, preview: 1620)));
+ // A HEIF the server reads through its proxy or preview develops nothing either.
+ Assert.Equal(8, Thumbnails(Frame(ImageFileFormat.Heif, 8192, 5464)));
+ // A 45 MP TIFF is decoded whole: one at a time within 2 GiB, whatever the JPEGs beside it.
+ Assert.Equal(1, Thumbnails(Frame(ImageFileFormat.Jpeg, 8192, 5464), Frame(ImageFileFormat.Tiff, 8192, 5464)));
+ // A RAW whose preview is smaller than the thumbnail is developed; a size nobody knows counts as 24 MP.
+ Assert.Equal(2, Thumbnails(Frame(ImageFileFormat.Cr2, 6000, 4000, preview: 160)));
+ Assert.Equal(2, Thumbnails(Frame(ImageFileFormat.Png, 0, 0)));
+ Assert.Equal(1, ArchiveImages.PassParallelism(0, Budget, [Frame(ImageFileFormat.Jpeg, 48, 32)], 320, preferPreview: true, Server));
+ // Proxies of 2048 px scale JPEGs down but develop every RAW, preview or not; full-size proxies decode JPEGs whole.
+ Assert.Equal(8, Proxies(2048, Frame(ImageFileFormat.Jpeg, 8192, 5464)));
+ Assert.Equal(2, Proxies(2048, Frame(ImageFileFormat.Cr2, 6000, 4000, preview: 6000)));
+ Assert.Equal(2, Proxies(0, Frame(ImageFileFormat.Jpeg, 6000, 4000)));
+ }
+
+ [Test]
+ public void ADeepAnalysisHoldsBackOnlyForTheOriginalsItDevelops()
+ {
+ var analysis = DeepAnalysisOptions.Default with { MaxDegreeOfParallelism = 8 };
+ Assert.Equal(8, SequenceAnalyser.AnalysisParallelism(analysis, Frames((ImageFileFormat.Jpeg, 8192, 5464, 0)), Budget));
+ // A DNG whose embedded preview reaches the analysis size (half the long edge, at most 2048 px) develops nothing.
+ Assert.Equal(8, SequenceAnalyser.AnalysisParallelism(analysis, Frames((ImageFileFormat.Dng, 5568, 4872, 2784)), Budget));
+ Assert.Equal(1, SequenceAnalyser.AnalysisParallelism(analysis, Frames((ImageFileFormat.Dng, 5568, 4872, 1024), (ImageFileFormat.Tiff, 8192, 5464, 0)), Budget));
+ // Frames read through their proxies decode as JPEGs.
+ Assert.Equal(8, SequenceAnalyser.AnalysisParallelism(analysis, Frames((ImageFileFormat.Tiff, 8192, 5464, 0)) with { Proxied = [true] }, Budget));
+ }
+
+ [Test]
+ public async Task AWindowHoldsBackWheneverItsDecodesDevelopOriginals()
+ {
+ var window = new FrameWindowOptions { MemoryBudget = Budget, MaxConcurrentLoads = 8, Ahead = 8 };
+ var tiffs = Frames((ImageFileFormat.Tiff, 8192, 5464, 0), (ImageFileFormat.Tiff, 8192, 5464, 0));
+ // A render node renders downloaded originals without counting what they decode (Originals unset): still held back.
+ await using (var node = new SequenceFrameSource(tiffs, tiffs.LightFrames(), Server, window))
+ {
+ Assert.Equal(1, node.WindowOptions(1920, DecodePurpose.Render).MaxConcurrentLoads);
+ // A preparation pass decodes TIFFs whole too.
+ Assert.Equal(1, node.WindowOptions(512, DecodePurpose.Analysis).MaxConcurrentLoads);
+ Assert.Equal((Budget, 8), (node.WindowOptions(1920, DecodePurpose.Render).MemoryBudget, node.WindowOptions(1920, DecodePurpose.Render).Ahead));
+ }
+ // RAWs whose previews reach the preparation's size: the preparation runs them all, the render develops them.
+ var raws = Frames((ImageFileFormat.Cr2, 6000, 4000, 1620), (ImageFileFormat.Cr2, 6000, 4000, 1620));
+ await using (var raw = new SequenceFrameSource(raws, raws.LightFrames(), Server, window) { Originals = true })
+ {
+ Assert.Equal(8, raw.WindowOptions(512, DecodePurpose.Analysis).MaxConcurrentLoads);
+ Assert.Equal(2, raw.WindowOptions(1920, DecodePurpose.Render).MaxConcurrentLoads);
+ }
+ // Proxies decode as JPEGs, flagged or found in a proxy cache's .jpg files.
+ await using (var proxied = new SequenceFrameSource(tiffs with { Proxied = [true, true] }, tiffs.LightFrames(), Server, window))
+ Assert.Equal(8, proxied.WindowOptions(1920, DecodePurpose.Render).MaxConcurrentLoads);
+ var cached = raws with { Files = ["cache/0001.jpg", "cache/0002.JPG"] };
+ await using var cache = new SequenceFrameSource(cached, cached.LightFrames(), Server, window);
+ Assert.Equal(8, cache.WindowOptions(1920, DecodePurpose.Render).MaxConcurrentLoads);
+ }
+
+ [Test]
+ public async Task ADemandLoadWaitsForTheReadAheadOfADevelopingWindow()
+ {
+ // The window caps its read-ahead only: a render that skips frames (a speed-up) asks for a frame the read-ahead is
+ // not decoding while it develops another, and both would run. One development at a time fits this budget.
+ var decoder = new CountingDecoder();
+ var tiffs = Frames([.. Enumerable.Repeat((ImageFileFormat.Tiff, 8192, 5464, 0), 8)]);
+ var frames = tiffs with { Files = [.. tiffs.Files.Select(f => f + ".tif")] };
+ var window = new FrameWindowOptions { MemoryBudget = Budget, MaxConcurrentLoads = 8, Ahead = 8 };
+ await using (var source = new SequenceFrameSource(frames, frames.LightFrames(), new ImageDecoder([decoder]), window))
+ {
+ Assert.Equal(1, source.DevelopingLimit(1920, DecodePurpose.Render));
+ foreach (var index in new[] { 0, 2, 4, 6 })
+ using (await source.GetFrameAsync(index, 1920, DecodePurpose.Render, CancellationToken.None)) { }
+ }
+ Assert.Equal(1, decoder.Peak);
+ Assert.Greater(decoder.Decodes, 4);
+ }
+
+ /// Decodes anything as a tiny frame, slowly, and counts how many decodes run at once.
+ private sealed class CountingDecoder : IImageDecoderProvider
+ {
+ private int _running;
+ private int _peak;
+ private int _decodes;
+
+ public int Peak => Volatile.Read(ref _peak);
+
+ public int Decodes => Volatile.Read(ref _decodes);
+
+ public string Name => "counting";
+
+ public DecodeTier? TierFor(ImageFileFormat format) => DecodeTier.Native;
+
+ public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
+ {
+ var now = Interlocked.Increment(ref _running);
+ int seen;
+ while ((seen = Volatile.Read(ref _peak)) < now && Interlocked.CompareExchange(ref _peak, now, seen) != seen)
+ {
+ }
+ Interlocked.Increment(ref _decodes);
+ Thread.Sleep(40);
+ Interlocked.Decrement(ref _running);
+ return new DecodedImage(new ImageBuffer(8, 6), DecodeTier.Native, Name, 8192, 5464);
+ }
+ }
+
+ private static SequenceFrames Frames(params (ImageFileFormat Format, int Width, int Height, int Preview)[] specs)
+ {
+ var sequence = new Sequence { Id = Guid.NewGuid(), ArchiveId = Guid.NewGuid(), Folder = "a" };
+ var frames = specs.Select((s, i) => new Frame
+ {
+ Id = Guid.NewGuid(),
+ SequenceId = sequence.Id,
+ Index = i,
+ RelativePath = $"a/IMG_{i:0000}",
+ FileName = $"IMG_{i:0000}",
+ CapturedAt = new DateTime(2026, 10, 16, 6, 0, 0).AddSeconds(10 * i),
+ Width = s.Width,
+ Height = s.Height,
+ Format = s.Format,
+ PreviewWidth = s.Preview,
+ PreviewHeight = s.Preview * 2 / 3,
+ }).ToList();
+ return new SequenceFrames(sequence, frames, [.. frames.Select(f => (string?)f.RelativePath)]);
+ }
+}
diff --git a/tests/Titano.Data.Tests/Repositories/JobRepositoryTests.cs b/tests/Titano.Data.Tests/Repositories/JobRepositoryTests.cs
index 1ead5d8f..5ad26e3f 100644
--- a/tests/Titano.Data.Tests/Repositories/JobRepositoryTests.cs
+++ b/tests/Titano.Data.Tests/Repositories/JobRepositoryTests.cs
@@ -36,6 +36,27 @@ public abstract class JobRepositoryTests(TestContext context) : RepositoryTest(c
Assert.Equal(job with { TargetIds = read.TargetIds }, read);
}
+ [Test]
+ public async Task ThePriorityQueueClaimsTheHigherKindsFirst()
+ {
+ // Owner review 1 (R2): on, the level of the kind comes before priority and age; off, the strict queue stays.
+ var db = await OpenTitanoAsync();
+ var oldVerify = TestData.Job(JobKind.Verify, JobPriority.User, TestData.Now.AddMinutes(-30));
+ var backgroundScan = TestData.Job(JobKind.Scan, JobPriority.Background, TestData.Now.AddMinutes(-20));
+ var userThumbs = TestData.Job(JobKind.Thumbs, JobPriority.User, TestData.Now.AddMinutes(-10));
+ var render = TestData.Job(JobKind.Render, JobPriority.Normal, TestData.Now);
+ foreach (var job in new[] { oldVerify, backgroundScan, userThumbs, render }) await db.Jobs.EnqueueAsync(job, Ct);
+ var queue = new Titano.Data.Jobs.JobPriorityQueue { Enabled = true };
+ JobKind[] kinds = [JobKind.Verify, JobKind.Scan, JobKind.Thumbs, JobKind.Render];
+
+ Assert.SequenceEqual([render.Id, userThumbs.Id, backgroundScan.Id, oldVerify.Id], (await db.Jobs.ListQueuedAsync(kinds, 10, queue, Ct)).Select(j => j.Id));
+ Assert.SequenceEqual([oldVerify.Id, userThumbs.Id, render.Id, backgroundScan.Id], (await db.Jobs.ListQueuedAsync(kinds, 10, Ct)).Select(j => j.Id));
+ Assert.Equal(userThumbs.Id, (await db.Jobs.ClaimNextAsync("server", [JobKind.Scan, JobKind.Thumbs, JobKind.Verify], queue, Ct))?.Id);
+ var order = new List();
+ while (await db.Jobs.ClaimNextAsync("server", null, queue, Ct) is { } claimed) order.Add(claimed.Id);
+ Assert.SequenceEqual([render.Id, backgroundScan.Id, oldVerify.Id], order);
+ }
+
[Test]
public async Task ClaimsByPriorityThenAgeAndKind()
{
diff --git a/tests/Titano.Server.Tests/Api/AdminTests.cs b/tests/Titano.Server.Tests/Api/AdminTests.cs
index 744d20a2..66841c4f 100644
--- a/tests/Titano.Server.Tests/Api/AdminTests.cs
+++ b/tests/Titano.Server.Tests/Api/AdminTests.cs
@@ -3,7 +3,10 @@ using System.Net.Http.Json;
using System.Text.Json.Nodes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
+using Titano.Core.Model;
+using Titano.Data.Jobs;
using Titano.Protocol;
+using Titano.Server.Configuration;
using Titano.Server.Hosting;
using Titano.Testing;
@@ -53,6 +56,59 @@ public sealed class AdminTests(TestContext context)
Assert.Equal("Language", (await invalid.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_setting")).Details);
}
+ [Test]
+ public async Task TheParallelismAndThePriorityQueueApplyAtOnce()
+ {
+ // Owner review 1 (R2): jobs run one at a time; the parallelism (photos at once) and the priority queue need no restart.
+ await using var server = await TestServer.StartAsync(context);
+ var admin = await server.AdminAsync();
+ using var read = await admin.GetAsync("/api/v1/server/settings");
+ var settings = await read.ReadAsync(ProtocolJson.Default.ServerSettingsDto);
+ Assert.Equal((2, false), (settings.Parallelism, settings.PriorityQueue));
+ Assert.Equal(ServerSettingsStore.Kinds.Count, settings.JobPriorities.Count);
+ Assert.Equal(("render", "high"), (settings.JobPriorities[0].Kind, settings.JobPriorities[0].Level));
+ Assert.Equal("low", settings.JobPriorities.Single(p => p.Kind == "verify").Level);
+ Assert.DoesNotContain("download", settings.JobPriorities.Select(p => p.Kind));
+
+ // The test server listens on ports the system chose (0), which no request may set.
+ var edited = settings with
+ {
+ HttpPort = 18080,
+ HttpsPort = 18443,
+ Parallelism = 3,
+ PriorityQueue = true,
+ JobPriorities = [.. settings.JobPriorities.Select(p => p.Kind == "scan" ? p with { Level = "high" } : p)],
+ };
+ using (var saved = await admin.PutAsJsonAsync("/api/v1/server/settings", edited, ProtocolJson.Default.ServerSettingsDto))
+ await saved.ExpectAsync(HttpStatusCode.OK);
+ // Applied at once, while the ports wait for the restart.
+ Assert.Equal((3, true, 0), (server.Options.Parallelism, server.Options.PriorityQueue, server.Options.HttpPort));
+ var queue = ServerSettingsStore.PriorityQueue(server.Options);
+ Assert.True(queue.Enabled);
+ Assert.Equal((JobKindPriority.High, JobKindPriority.High), (queue.LevelOf(JobKind.Scan), queue.LevelOf(JobKind.Render)));
+ // Only the level that differs from the defaults is stored.
+ var file = JsonNode.Parse(await File.ReadAllTextAsync(server.Options.SettingsFile))!.AsObject();
+ Assert.Equal((3, true), (file["Parallelism"]!.GetValue(), file["PriorityQueue"]!.GetValue()));
+ Assert.Equal("""{"scan":"high"}""", file["JobPriorities"]!.ToJsonString());
+ Assert.False(file.ContainsKey("Workers"));
+
+ using (var dashboard = await admin.GetAsync("/api/v1/server/dashboard"))
+ {
+ var d = await dashboard.ReadAsync(ProtocolJson.Default.DashboardDto);
+ Assert.Equal((3, true), (d.Parallelism, d.PriorityQueue));
+ }
+ foreach (var (invalid, setting) in new (ServerSettingsDto, string)[]
+ {
+ (edited with { Parallelism = 0 }, "Parallelism"),
+ (edited with { JobPriorities = [new JobPriorityDto("download", "high")] }, "JobPriorities"),
+ (edited with { JobPriorities = [new JobPriorityDto("scan", "urgent")] }, "JobPriorities"),
+ })
+ {
+ using var refused = await admin.PutAsJsonAsync("/api/v1/server/settings", invalid, ProtocolJson.Default.ServerSettingsDto);
+ Assert.Equal(setting, (await refused.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_setting")).Details);
+ }
+ }
+
[Test]
public async Task LogsAreReadableButOnlyByName()
{
diff --git a/tests/Titano.Server.Tests/Hosting/StartupTests.cs b/tests/Titano.Server.Tests/Hosting/StartupTests.cs
index 82475e78..5a8f1553 100644
--- a/tests/Titano.Server.Tests/Hosting/StartupTests.cs
+++ b/tests/Titano.Server.Tests/Hosting/StartupTests.cs
@@ -75,6 +75,37 @@ public sealed class StartupTests(TestContext context)
Assert.Throws(() => ServerOptions.Load(new Dictionary { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_WORKERS"] = "many" }));
}
+ [Test]
+ public void TheFormerWorkersSettingStillSetsTheParallelism()
+ {
+ // Owner review 1: "workers" (jobs at once) became the parallelism (photos the running job processes at once).
+ var root = context.CreateTempDirectory("workers");
+ File.WriteAllText(Path.Combine(root, "server.json"), """{ "Workers": 6 }""");
+ Assert.Equal(6, ServerOptions.Load(new Dictionary { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres" }).Parallelism);
+
+ var former = ServerOptions.Load(new Dictionary { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_WORKERS"] = "5" });
+ Assert.Equal((5, true), (former.Parallelism, former.FormerWorkersVariable));
+ Assert.SequenceEqual(["Database", "Parallelism"], former.Locked);
+ var both = ServerOptions.Load(new Dictionary
+ {
+ ["TITANO_DATA_PATH"] = root,
+ ["TITANO_DB"] = "postgres",
+ ["TITANO_WORKERS"] = "5",
+ ["TITANO_PARALLELISM"] = "7",
+ ["TITANO_PRIORITY_QUEUE"] = "true",
+ });
+ Assert.Equal((7, true), (both.Parallelism, both.PriorityQueue));
+ Assert.SequenceEqual(["Database", "Parallelism", "PriorityQueue"], both.Locked);
+ Assert.False(ServerOptions.Load(new Dictionary { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_PARALLELISM"] = "2" })
+ .FormerWorkersVariable);
+ Assert.Throws(() => ServerOptions.Load(new Dictionary { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_PRIORITY_QUEUE"] = "often" }));
+ // A value the admin panel would refuse is brought within its range, so the panel can still save the other settings.
+ Assert.Equal(ServerOptions.MaxParallelism,
+ ServerOptions.Load(new Dictionary { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_PARALLELISM"] = "96" }).Parallelism);
+ Assert.Equal(1, ServerOptions.Load(new Dictionary { ["TITANO_DATA_PATH"] = root, ["TITANO_DB"] = "postgres", ["TITANO_WORKERS"] = "0" }).Parallelism);
+ Assert.InRange(new ServerOptions().Parallelism, 1, ServerOptions.MaxParallelism);
+ }
+
[Test]
public void TrustedProxiesAreAddressesOrNetworks()
{
diff --git a/tests/Titano.Server.Tests/Jobs/JobApiTests.cs b/tests/Titano.Server.Tests/Jobs/JobApiTests.cs
index 2957d42c..6adaeea0 100644
--- a/tests/Titano.Server.Tests/Jobs/JobApiTests.cs
+++ b/tests/Titano.Server.Tests/Jobs/JobApiTests.cs
@@ -64,7 +64,7 @@ public sealed class JobApiTests(TestContext context)
public async Task WaitingJobsChangePriorityAndRightsAreChecked()
{
var handler = new BlockingJobHandler(JobKind.Thumbs);
- await using var server = await TestServer.StartAsync(context, o => o.Workers = 1, s => s.AddSingleton(handler));
+ await using var server = await TestServer.StartAsync(context, o => o.Parallelism = 1, s => s.AddSingleton(handler));
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var (viewer, _, _) = await server.UserAsync("marco", "viewer");
var sequence = await CreateSequenceAsync(editor);
diff --git a/tests/Titano.Server.Tests/Storage/ProcessingModuleTests.cs b/tests/Titano.Server.Tests/Storage/ProcessingModuleTests.cs
index 32516306..54f00f4e 100644
--- a/tests/Titano.Server.Tests/Storage/ProcessingModuleTests.cs
+++ b/tests/Titano.Server.Tests/Storage/ProcessingModuleTests.cs
@@ -35,6 +35,21 @@ public sealed class ProcessingModuleTests(TestContext context)
ProcessingModule.Register(services);
}
+ [Test]
+ public async Task TheRunningJobHasTheParallelismAndTheHostsMemoryBudget()
+ {
+ // Owner review 1: one job at a time, the parallelism working inside it, its frame window sized to the host.
+ await using var server = await TestServer.StartAsync(context, o => o.Parallelism = 3, Production);
+ var processing = server.Services.GetRequiredService();
+ var tuned = processing.Options.Tune!(processing.Options);
+ Assert.Equal((3, 3, 3), (tuned.DeepAnalysis.MaxDegreeOfParallelism, tuned.FrameWindow.MaxConcurrentLoads, processing.Options.Archive.Parallelism()));
+ Assert.Equal(processing.Options.Archive.MemoryBudget(), tuned.FrameWindow.MemoryBudget);
+ Assert.True(tuned.FrameWindow.MemoryBudget >= 1L << 30, "at least the window's own default");
+ // A change in the admin panel applies to the next job.
+ server.Options.Parallelism = 5;
+ Assert.Equal(5, processing.Options.Tune!(processing.Options).FrameWindow.MaxConcurrentLoads);
+ }
+
[Test]
public async Task UploadedFramesAreLightAnalysedAndAdvised()
{
diff --git a/tests/Titano.Server.Tests/TestServer.cs b/tests/Titano.Server.Tests/TestServer.cs
index a69b456d..6511d370 100644
--- a/tests/Titano.Server.Tests/TestServer.cs
+++ b/tests/Titano.Server.Tests/TestServer.cs
@@ -61,7 +61,7 @@ public sealed class TestServer : IAsyncDisposable
TlsMode = TlsMode.Auto,
Database = DatabaseKind.Sqlite,
Discovery = false,
- Workers = 2,
+ Parallelism = 2,
AdminUser = "admin",
AdminPassword = AdminPassword,
// Tests sign in many times from one address; the rate-limit test sets the production value.
diff --git a/tests/Titano.Tools.Tests/I18nCommandTests.cs b/tests/Titano.Tools.Tests/I18nCommandTests.cs
index 78fcf99b..34f69e8e 100644
--- a/tests/Titano.Tools.Tests/I18nCommandTests.cs
+++ b/tests/Titano.Tools.Tests/I18nCommandTests.cs
@@ -6,7 +6,7 @@ namespace Titano.Tools.Tests;
/// i18n check : English texts that count something need plural forms (BRIEF §16), and every error code
/// of the server has its message, whose key is built at run time.
-public sealed class I18nCommandTests
+public sealed class I18nCommandTests(TestContext context)
{
[Test]
public void ANumberInFrontOfACountedNounNeedsPluralForms()
@@ -59,6 +59,32 @@ public sealed class I18nCommandTests
Assert.Null(I18nCommand.CountedTally("Import.Result.Skipped.other", "{0:N0} copied, {1:N0} skipped"));
}
+ [Test]
+ public void PruneRemovesOnlyTheKeysEnglishNoLongerHas()
+ {
+ var dir = context.CreateTempDirectory();
+ void Write(string culture, string file, params (string Key, string Value)[] entries)
+ {
+ Directory.CreateDirectory(Path.Combine(dir, culture));
+ var data = string.Concat(entries.Select(e => $" {e.Value} \n"));
+ File.WriteAllText(Path.Combine(dir, culture, file), $"\n\n{data} \n");
+ }
+ Write("en", "Jobs.resx", ("Jobs.Kind.Render", "Render"), ("Jobs.Parallelism.one", "{0} photo at once"), ("Jobs.Parallelism.other", "{0} photos at once"));
+ Write("it", "Jobs.it.resx", ("Jobs.Kind.Render", "Render"), ("Jobs.Workers", "Processi {0} / {1}"), ("Jobs.Parallelism.one", "{0} foto alla volta"));
+ // Polish adds plural forms English has not: they stay.
+ Write("pl", "Jobs.pl.resx", ("Jobs.Parallelism.few", "{0} zdjęcia naraz"), ("Jobs.Parallelism.many", "{0} zdjęć naraz"));
+ var untouched = File.ReadAllText(Path.Combine(dir, "pl", "Jobs.pl.resx"));
+
+ Assert.Equal(1, I18nCommand.PruneOrphans(dir, I18nCommand.Catalog.Load(dir)));
+
+ var italian = I18nCommand.Catalog.Load(dir).Cultures["it"];
+ Assert.SequenceEqual(["Jobs.Kind.Render", "Jobs.Parallelism.one"], italian.Keys.Order(StringComparer.Ordinal));
+ Assert.Equal("{0} foto alla volta", italian["Jobs.Parallelism.one"].Value);
+ Assert.Equal(untouched, File.ReadAllText(Path.Combine(dir, "pl", "Jobs.pl.resx")));
+ Assert.Equal(3, I18nCommand.Catalog.Load(dir).Cultures["en"].Count);
+ Assert.Equal(0, I18nCommand.PruneOrphans(dir, I18nCommand.Catalog.Load(dir)));
+ }
+
[Test]
public void ErrorCodesAreTheLiteralsPassedToTheServerErrorHelpers()
{