using System.Collections.Concurrent; using Chronos.Core.Archiving; using Chronos.Core.Conversion; using Chronos.Server.Api; using Chronos.Testing; namespace Chronos.Server.Tests.Api; /// /// The dry runs the server keeps (, owner review 2, R7) as Convert's endpoint uses them: what a /// conversion plan is kept by (), what refuses it, and the turns and room it shares with /// Reorganize's plans. The rest of what the cache does is in . /// [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(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(); Func> Rename(int i) => async stop => { started["reorganize" + i] = true; await release.Task.WaitAsync(stop); return new RenamePlan(Guid.NewGuid(), archive, []); }; var made = 0; Task 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(() => plans.GetOrPlanAsync(unknown, _ => throw new ArgumentException("Not in the archive."))); await Assert.ThrowsAsync(() => plans.GetOrPlanAsync(unknown, Convert)); var offline = ConvertPlanKey.Of(archive, "stamp", [Guid.NewGuid()], new ConvertOptions(ConvertTarget.Tiff16)); await Assert.ThrowsAsync(() => 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); } /// Waits until holds. private static async Task EventuallyAsync(Func 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); } } }