CI / Build and test (push) Failing after 3m1s
CI / PostgreSQL 16 integration (push) Skipped
CI / PostgreSQL 17 integration (push) Skipped
CI / Docker image (push) Skipped
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 10s
Release / Build, test and publish (push) Successful in 15m31s
Release / Server image (push) Successful in 1m13s
Convert photos already in the archive to JPEG, TIFF-16 or Linear DNG, from the panel's Library and the desktop's Archive and Library. Each run asks Replace (same frames, originals to the trash, recoverable) or New sequence. A dry run lists what converts, what is skipped and why, size before and after, and free space; the job converts exactly the confirmed photos, resumes, and commits all or nothing per sequence. A conversion never changes what Chronos reads from a photo. Replaced originals are erased only on the archive's own word that their conversion is gone. Desktop TIFFs that WIC showed black or as noise now go to the managed reader; the Edit picker no longer spins a core. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
146 lines
8.3 KiB
C#
146 lines
8.3 KiB
C#
using System.Collections.Concurrent;
|
|
using Chronos.Core.Archiving;
|
|
using Chronos.Core.Conversion;
|
|
using Chronos.Server.Api;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Server.Tests.Api;
|
|
|
|
/// <summary>
|
|
/// The dry runs the server keeps (<see cref="PlanCache"/>, owner review 2, R7) as Convert's endpoint uses them: what a
|
|
/// conversion plan is kept by (<see cref="ConvertPlanKey"/>), what refuses it, and the turns and room it shares with
|
|
/// Reorganize's plans. The rest of what the cache does is in <see cref="ReorganizeTests"/>.
|
|
/// </summary>
|
|
[Category("server")]
|
|
public sealed class PlanCacheTests
|
|
{
|
|
private static readonly TimeSpan Wait = TimeSpan.FromSeconds(TestTiming.Budget(30));
|
|
|
|
[Test]
|
|
public void AConversionPlanIsKeptByWhatConvertsNotByTheMode()
|
|
{
|
|
var archive = Guid.NewGuid();
|
|
var (x, y) = (Guid.NewGuid(), Guid.NewGuid());
|
|
var tiff = ConvertPlanKey.Of(archive, "stamp", [x, y], new ConvertOptions(ConvertTarget.Tiff16, 80));
|
|
// The mode is not part of it, and neither is the quality of a lossless target; a JPEG's is.
|
|
Assert.Equal(tiff, ConvertPlanKey.Of(archive, "stamp", [x, y], new ConvertOptions(ConvertTarget.Tiff16)));
|
|
Assert.Equal(ConvertPlanKey.Of(archive, "stamp", [x], new ConvertOptions(ConvertTarget.Jpeg, 80)),
|
|
ConvertPlanKey.Of(archive, "stamp", [x], new ConvertOptions(ConvertTarget.Jpeg, 80)));
|
|
Assert.NotEqual(ConvertPlanKey.Of(archive, "stamp", [x], new ConvertOptions(ConvertTarget.Jpeg, 80)),
|
|
ConvertPlanKey.Of(archive, "stamp", [x], new ConvertOptions(ConvertTarget.Jpeg, 95)));
|
|
// Another target, order of the sequences, every sequence, stamp or archive is another plan.
|
|
Assert.NotEqual(tiff, ConvertPlanKey.Of(archive, "stamp", [x, y], new ConvertOptions(ConvertTarget.LinearDng)));
|
|
Assert.NotEqual(tiff, ConvertPlanKey.Of(archive, "stamp", [y, x], new ConvertOptions(ConvertTarget.Tiff16)));
|
|
Assert.NotEqual(tiff, ConvertPlanKey.Of(archive, "stamp", null, new ConvertOptions(ConvertTarget.Tiff16)));
|
|
Assert.NotEqual(tiff, ConvertPlanKey.Of(archive, "other", [x, y], new ConvertOptions(ConvertTarget.Tiff16)));
|
|
Assert.NotEqual(tiff, ConvertPlanKey.Of(Guid.NewGuid(), "stamp", [x, y], new ConvertOptions(ConvertTarget.Tiff16)));
|
|
// A Reorganize of the same sequences is never the same plan.
|
|
Assert.NotEqual<PlanKey>(ConvertPlanKey.Of(archive, "stamp", null, new ConvertOptions(ConvertTarget.Tiff16)),
|
|
ReorganizePlanKey.Of(archive, "stamp", null, new RenameTemplates(Folder: "moved/{date}")));
|
|
|
|
// A sequence not in the archive refuses it; a failure (a sidecar unreadable, a bug) does not.
|
|
Assert.True(tiff.Refuses(new ArgumentException("Not in the archive.")));
|
|
foreach (var failure in new Exception[] { new ArgumentNullException("plan"), new IOException("The disk went away."), new NestedRenameException("a/b", "a") })
|
|
Assert.False(tiff.Refuses(failure), failure.GetType().Name);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ConversionAndReorganizePlansShareTheTurnsAndTheRoom()
|
|
{
|
|
var clock = new SteppedTimeProvider();
|
|
using var plans = new PlanCache(clock);
|
|
var archive = Guid.NewGuid();
|
|
var templates = new RenameTemplates(Folder: "moved/{date}");
|
|
var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
var started = new ConcurrentDictionary<string, bool>();
|
|
Func<CancellationToken, Task<RenamePlan>> Rename(int i) => async stop =>
|
|
{
|
|
started["reorganize" + i] = true;
|
|
await release.Task.WaitAsync(stop);
|
|
return new RenamePlan(Guid.NewGuid(), archive, []);
|
|
};
|
|
var made = 0;
|
|
Task<ConvertPlan> Convert(CancellationToken stop)
|
|
{
|
|
started["convert"] = true;
|
|
Interlocked.Increment(ref made);
|
|
return Task.FromResult(new ConvertPlan(archive, new ConvertOptions(ConvertTarget.LinearDng), ConvertMode.Replace, [], new ConvertSpace(0, null)));
|
|
}
|
|
|
|
// Every turn taken by a Reorganize: a conversion waits for one.
|
|
var renames = Enumerable.Range(0, PlanCache.Concurrency)
|
|
.Select(i => plans.GetOrPlanAsync(ReorganizePlanKey.Of(archive, "stamp", [new Guid(i, 0, 0, new byte[8])], templates), Rename(i))).ToList();
|
|
await EventuallyAsync(() => started.Count == PlanCache.Concurrency);
|
|
var key = ConvertPlanKey.Of(archive, "stamp", null, new ConvertOptions(ConvertTarget.LinearDng));
|
|
var converting = plans.GetOrPlanAsync(key, Convert);
|
|
await Task.Delay(200);
|
|
Assert.False(started.ContainsKey("convert"), "no turn while Reorganize holds them");
|
|
release.SetResult();
|
|
var plan = await converting;
|
|
foreach (var rename in renames) await rename;
|
|
Assert.True(ReferenceEquals(plan, await plans.GetOrPlanAsync(key, Convert)), "kept");
|
|
Assert.Equal(1, made);
|
|
Assert.Equal(PlanCache.Concurrency + 1, plans.Count);
|
|
|
|
// A refusal is kept as a plan is, a failure is not; the room is shared: the oldest of either kind goes first.
|
|
var unknown = ConvertPlanKey.Of(archive, "stamp", [Guid.NewGuid()], new ConvertOptions(ConvertTarget.LinearDng));
|
|
await Assert.ThrowsAsync<ArgumentException>(() => plans.GetOrPlanAsync(unknown, _ => throw new ArgumentException("Not in the archive.")));
|
|
await Assert.ThrowsAsync<ArgumentException>(() => plans.GetOrPlanAsync(unknown, Convert));
|
|
var offline = ConvertPlanKey.Of(archive, "stamp", [Guid.NewGuid()], new ConvertOptions(ConvertTarget.Tiff16));
|
|
await Assert.ThrowsAsync<ArchiveOfflineException>(() => plans.GetOrPlanAsync(offline, _ => throw new ArchiveOfflineException("/archive", ArchiveAvailability.Offline)));
|
|
await plans.GetOrPlanAsync(offline, Convert);
|
|
Assert.Equal(2, made);
|
|
for (var i = 0; i < PlanCache.Capacity; i++)
|
|
{
|
|
clock.Advance(TimeSpan.FromSeconds(1));
|
|
await plans.GetOrPlanAsync(ConvertPlanKey.Of(archive, "stamp", [new Guid(i, 0, 0, new byte[8])], new ConvertOptions(ConvertTarget.Jpeg)), Convert);
|
|
}
|
|
Assert.Equal(PlanCache.Capacity, plans.Count);
|
|
await plans.GetOrPlanAsync(key, Convert);
|
|
Assert.Equal(3 + PlanCache.Capacity, made);
|
|
}
|
|
|
|
[Test]
|
|
public async Task AConfirmationTakesOnlyAPlanKeptWithinItsLifetime()
|
|
{
|
|
var clock = new SteppedTimeProvider();
|
|
using var plans = new PlanCache(clock);
|
|
var archive = Guid.NewGuid();
|
|
var key = ConvertPlanKey.Of(archive, "stamp", null, new ConvertOptions(ConvertTarget.LinearDng));
|
|
var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
var made = new ConvertPlan(archive, new ConvertOptions(ConvertTarget.LinearDng), ConvertMode.Replace, [], new ConvertSpace(0, null));
|
|
async Task<ConvertPlan> Plan(CancellationToken stop)
|
|
{
|
|
await release.Task.WaitAsync(stop);
|
|
return made;
|
|
}
|
|
|
|
// Never asked, then still being made: nothing to confirm, and nothing is planned for the asking.
|
|
Assert.Null(plans.Kept(key));
|
|
var making = plans.GetOrPlanAsync(key, Plan);
|
|
Assert.Null(plans.Kept(key));
|
|
release.SetResult();
|
|
await making;
|
|
Assert.True(ReferenceEquals(made, await plans.Kept(key)!), "kept");
|
|
// Past its lifetime: let go, whatever its timer.
|
|
clock.Advance(PlanCache.Lifetime);
|
|
Assert.Null(plans.Kept(key));
|
|
Assert.Equal(0, plans.Count);
|
|
// A refusal kept is a refusal for the confirmation too.
|
|
var unknown = ConvertPlanKey.Of(archive, "stamp", [Guid.NewGuid()], new ConvertOptions(ConvertTarget.LinearDng));
|
|
await Assert.ThrowsAsync<ArgumentException>(() => plans.GetOrPlanAsync(unknown, _ => throw new ArgumentException("Not in the archive.")));
|
|
await Assert.ThrowsAsync<ArgumentException>(() => plans.Kept(unknown)!);
|
|
}
|
|
|
|
/// <summary>Waits until <paramref name="condition"/> holds.</summary>
|
|
private static async Task EventuallyAsync(Func<bool> condition, [System.Runtime.CompilerServices.CallerArgumentExpression(nameof(condition))] string? expression = null)
|
|
{
|
|
var until = DateTime.UtcNow + Wait;
|
|
while (!condition())
|
|
{
|
|
Assert.True(DateTime.UtcNow < until, expression);
|
|
await Task.Delay(10);
|
|
}
|
|
}
|
|
}
|