Files
Chronos/tests/Chronos.Server.Tests/Api/PlanCacheTests.cs
T
Alby96 8174852a2c fix(server): keep convert dry runs like Reorganize's, say what a new sequence lacks
The convert endpoint planned the whole selection inside the HTTP request, again for
every mode, quality keystroke and confirmation, with no limit: behind a proxy an
all-sequence conversion could never get past its dry run or its apply.

- ReorganizePlanCache becomes PlanCache, shared by Reorganize and Convert: one plan
  per key made apart from the request, two at once across both kinds, eight kept,
  15-minute timeout, 5-minute lifetime, 503 plan_dropped. Keys are typed records
  (ReorganizePlanKey, ConvertPlanKey) that also say which exceptions are refusals.
- A conversion plan is keyed by archive, a convert stamp of the index
  (SequenceRepository.GetConvertStampAsync: the Reorganize stamp plus, per planned
  sequence, the photos' count, sizes, formats, pixel sizes and missing, corrupt,
  preview-only and undecodable flags), the sequences and the target with the
  quality that counts. Not the mode: the request's mode and options are set on the
  kept plan, and the free space is measured again at every request through the
  engine. A plan made while the volume was away is not kept (503 archive_offline).
- New sequence holds the converted copies alone: the dry run says the skipped
  photos are not copied into it (Archive.Convert.NotCopied.*, Server.Convert.OnPcNew),
  the panel's totals, column and confirmation give the count, and so does the
  desktop's summary.
- Panel: the preview is asked in Replace's neutral words; a dry run cut off by a
  proxy is asked again while the totals show how long it takes, plan_dropped is
  retried once; an apply the server let go plans again, a changed one shows
  Admin.Convert.PlanChanged, and one with no answer is not resent (its job may be
  queued).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit f0b1e379ce7c08b6d3459ab25ca6214c8f366f6c)
2026-10-27 22:15:51 +01:00

114 lines
6.7 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);
}
/// <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);
}
}
}