Files
Chronos/tests/Chronos.Server.Tests/Api/PlanCacheTests.cs
T
Alby96 c669d0504d fix(convert): the job converts exactly the photos its preview listed, once
A conversion job could still do something its confirmed dry run did not show,
or refuse what it should have converted:

- An upload landing between the confirmation and the sequence's turn made the
  sequence Changed at every retry. Each ConvertSequence now names the photos
  the plan converts (Photos, their frame ids); the job takes those from its
  re-plan, checks their content against the confirmed digest and leaves photos
  that came in since as they are. A photo the plan did not list is never
  converted, whatever changed on disk or in the index since the dry run.
- A file another program held during the dry run was planned as Missing and
  then converted or not depending on the re-plan. It is now its own reason
  (inaccessible, "held open or cannot be opened"); a truncated file reads as
  undecodable. At job time a confirmed photo that cannot be opened leaves the
  sequence Blocked for the retry; a held photo the job does not convert does
  not hide a real change.
- The same sequences could be converted twice (two dialogs or tabs, a lost
  answer, no dry run at all). MassConverter.QueueAsync checks the queued,
  running and paused conversions and queues under one lock, in the caller's
  context (the desktop's enqueue shows a toast on the UI thread); a job's own
  re-plan treats sequences of conversions queued before it as busy.
- The per-photo space check reserved a full batch of photos even when fewer
  were left, so a job whose preview had the space could stop at every retry.
  It now reserves the photo and those after it that may convert at once.
- New sequence now says in the dry run, the panel's totals and confirmation,
  and a new desktop confirmation, how many photos each new sequence lacks;
  only sequences that are copied use those words.
- On the server, a confirmation takes only the plan kept for the index as it
  is now (503 plan_dropped otherwise, never a plan made inside the request),
  and busy sequences and free space are as they are at every request. The
  panel's Apply asks the dry run again first, with Close still available,
  and confirms only the same fingerprint.
- Maintenance keeps another host's conversion staging as long as its journal
  and purges both together; the unstaged-copy failure and blocked sequences
  are logged in catalog sentences.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit fefd9c34ebfeed1f9e84161610ef286026d38355)
2026-11-02 10:33:34 +01:00

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);
}
}
}