fix(archive): erase replaced originals only on the archive's own word
A conversion counted as deleted when no frame of the sidecar held it, no tombstone ended its chain and every frame was hashed. A sidecar rebuilt by adoption meets that after a routine verify although it never knew the conversion, so the originals were erased, the dry run calling them the last copy, while the damaged conversion was still in the folder. A sequence found nowhere and missing from this index counted as gone in the same way, even when the walk had only skipped its folder (photos without a sidecar), and its live conversion was never hashed. A conversion is now deleted only on positive evidence: a Deleted tombstone ending its chain in the sequence's sidecar, the sequence in the trash, or the trash's record of erasing it (.chronos/trash/.erased/<id>.json, written before a deleted sequence's folder goes while a replaced entry of it stays, swept once none does). Anything else keeps the entry, and its job log line says what settles it: verify the sequence (a frame never hashed), scan the archive (a sidecar missing or unreadable), rebuild the index (a row the archive does not bear out), or restore the entry to keep its photos. Also: - a dry run or an emptying walks the archive once for every sequence that moved, and again only when a folder it found no longer holds it; - a download queues the passes a frame swap cleared whatever stops the job (JobContext.EnqueueFollowUpAsync with a token), as a restore does; - the confirmation's detail takes Tab and scrolls from the keyboard when it is longer than its card. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8ea25228eb
commit
5012c3bbd2
12 files changed
+657
-108
No files matched your search
@@ -439,9 +439,19 @@ public abstract class FrameTrashTests(TestContext context) : DatabaseTest(contex
|
||||
var (sequence, frames) = await UploadedAsync(fx, 2);
|
||||
var replacement = frames[1];
|
||||
var good = WriteReplaced(fx, sequence, "20261027100000", replacement.Sha256!);
|
||||
// A replacement no frame of the sequence holds any more (its conversion deleted and erased): the dry run says so, and
|
||||
// the emptying confirmed with it erases the originals (owner review 2, R7).
|
||||
var gone = WriteReplaced(fx, sequence, "20261027110000", new string('a', 64));
|
||||
// A replacement no frame of the sequence holds any more, and whose deletion the sidecar records (its conversion deleted
|
||||
// and erased): the dry run says so, and the emptying confirmed with it erases the originals (owner review 2, R7).
|
||||
var deleted = new string('a', 64);
|
||||
var gone = WriteReplaced(fx, sequence, "20261027110000", deleted);
|
||||
var sidecar = fx.Paths.Sequence(sequence.Folder).ManifestPath;
|
||||
var manifest = ManifestStore.ReadSequence(sidecar);
|
||||
ManifestStore.WriteSequence(sidecar, manifest with
|
||||
{
|
||||
Removed = [.. (manifest.Removed ?? []), new RemovedPhoto { Sha256 = deleted, Reason = RemovalReason.Deleted, At = DateTimeOffset.UtcNow }],
|
||||
});
|
||||
// One that nothing in the archive says was deleted stays: the sidecar holds no frame of it and no tombstone of it, as a
|
||||
// sidecar rebuilt from the photos shows a conversion it never knew.
|
||||
var untraced = WriteReplaced(fx, sequence, "20261027113000", new string('b', 64));
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var listed = await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct);
|
||||
Assert.True(listed.All(e => e.Kind == TrashKind.Replaced));
|
||||
@@ -449,9 +459,11 @@ public abstract class FrameTrashTests(TestContext context) : DatabaseTest(contex
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.True(plan.Entries.Single(e => e.Name == FrameTrash.EntryPrefix + gone).ConversionDeleted);
|
||||
Assert.False(plan.Entries.Single(e => e.Name == FrameTrash.EntryPrefix + good).ConversionDeleted);
|
||||
Assert.False(plan.Entries.Single(e => e.Name == FrameTrash.EntryPrefix + untraced).ConversionDeleted, "the archive does not say it was deleted");
|
||||
var job = await engine.EnqueueAsync(ArchiveJobs.EmptyTrash(fx.Archive, plan), cancellationToken: fx.Ct);
|
||||
await fx.WaitForAsync(job.Id, JobState.Done);
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
var kept = await fx.WaitForAsync(job.Id, JobState.Failed);
|
||||
Assert.Contains(Chronos.Core.Localization.Loc.L("Archive.Log.TrashKeptUnknown", sequence.DisplayTitle), kept.LogTail);
|
||||
Assert.Equal(FrameTrash.EntryPrefix + untraced, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Name, "the others are erased");
|
||||
|
||||
// A replacement whose file no longer hashes to it keeps its originals.
|
||||
var other = WriteReplaced(fx, sequence, "20261027120000", replacement.Sha256!);
|
||||
|
||||
@@ -98,6 +98,10 @@ public sealed class ReplacedTrashTests(TestContext context)
|
||||
return await fx.WaitForAsync(job.Id, state);
|
||||
}
|
||||
|
||||
/// <summary>What the emptying job of <paramref name="plan"/> is given, for a test that runs the emptying itself.</summary>
|
||||
private static EmptyTrashParameters Parameters(TrashEmptyPlan plan) =>
|
||||
new([.. plan.Entries.Select(e => new EmptyTrashItem(e.Name, SequenceTrash.EntryFingerprint(e)))], plan.Fingerprint);
|
||||
|
||||
[Test]
|
||||
public async Task AReplacementThatCouldNotBeUndoneIsUndoneBeforeAnythingElseIsFiled()
|
||||
{
|
||||
@@ -349,39 +353,211 @@ public sealed class ReplacedTrashTests(TestContext context)
|
||||
public async Task OriginalsStayWhileTheArchiveCannotTellWhereTheirConversionIs()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var (id, photos) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500);
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
var manifest = Sidecar(fx, sequence);
|
||||
var unknown = Loc.L("Archive.Log.TrashKeptUnknown", replaced.Title);
|
||||
|
||||
// A sidecar rebuilt from the photos, as a scan adopts a folder that lost its own: new frame ids, nothing hashed, no
|
||||
// tombstones. It no longer says what became of the conversion.
|
||||
ManifestStore.WriteSequence(folder.ManifestPath, manifest with
|
||||
{
|
||||
Frames = [.. manifest.Frames.Select(f => f with { Id = Guid.CreateVersion7(), Sha256 = null })],
|
||||
Removed = null,
|
||||
});
|
||||
// A sidecar that cannot be read: a scan rebuilds it, as the line says.
|
||||
File.WriteAllText(folder.ManifestPath, "{ \"damaged");
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted);
|
||||
Assert.Contains(unknown, (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnreadable", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
|
||||
// A sidecar that cannot be read.
|
||||
File.WriteAllText(folder.ManifestPath, "{ \"damaged");
|
||||
Assert.Contains(unknown, (await EmptyAsync(fx, engine, await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct), JobState.Failed)).LogTail);
|
||||
// A sidecar rebuilt from the photos, as a scan adopts a folder that lost its own: new frame ids, nothing hashed, no
|
||||
// tombstones. A verify of the sequence, as the line says, may find the conversion in one of its frames.
|
||||
var rebuilt = manifest with { Frames = [.. manifest.Frames.Select(f => f with { Id = Guid.CreateVersion7(), Sha256 = null })], Removed = null };
|
||||
ManifestStore.WriteSequence(folder.ManifestPath, rebuilt);
|
||||
plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnhashed", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
// Unless their files are missing: there is nothing a verify could hash.
|
||||
ManifestStore.WriteSequence(folder.ManifestPath, rebuilt with { Frames = [.. rebuilt.Frames.Select(f => f with { Flags = f.Flags | FrameFlags.Missing })] });
|
||||
plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnknown", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
|
||||
// The sequence gone from the archive (deleted and erased by another host) while this index still has it: kept until a
|
||||
// scan settles the index. Then the dry run says the conversion was deleted, and the emptying confirmed with it erases.
|
||||
Directory.Delete(folder.Folder, recursive: true);
|
||||
Assert.Contains(unknown, (await EmptyAsync(fx, engine, await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct), JobState.Failed)).LogTail);
|
||||
Assert.Equal(replaced.Name, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Name);
|
||||
// Hashed, the conversion damaged meanwhile: no frame holds it and no tombstone says it was deleted. The originals stay,
|
||||
// and the dry run never calls them the last copy: only the archive's own word makes a conversion deleted.
|
||||
Damage(folder.File(manifest.Frames[0].File));
|
||||
ManifestStore.WriteSequence(folder.ManifestPath, rebuilt with { Frames = [.. rebuilt.Frames.Select(f => f with { Sha256 = Sha(folder.File(f.File)) })] });
|
||||
plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted, "nothing in the archive says the conversion was deleted");
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnknown", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
|
||||
// Restored, as the line says, the original comes back whole, as a sequence of its own.
|
||||
var restored = await fx.Service.RestoreEntryAsync(fx.Archive.Id, replaced.Name, null, fx.Ct);
|
||||
Assert.True(restored.NewSequence);
|
||||
Assert.SequenceEqual([Sha(photos[0])], (await fx.Db.Frames.ListBySequenceAsync(restored.Sequence.Id)).Select(f => f.Sha256!));
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AConversionAnotherHostMadeIsNotTakenForDeletedOnceItsSidecarIsRebuilt()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, photos) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
// A desktop sharing the archive converts the first photo: this index never sees the converted file.
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500, by: await fx.OtherWriterAsync());
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
var folder = fx.Paths.Sequence(sequence.Folder);
|
||||
// The conversion is damaged and the sidecar lost since: this index's scan rebuilds the sidecar from the photos with what
|
||||
// it knew, which is nothing of the conversion (the converted file gets a new frame id, no tombstone names it).
|
||||
Damage(folder.File(Sidecar(fx, sequence).Frames[0].File));
|
||||
File.Delete(folder.ManifestPath);
|
||||
await fx.ScanAsync();
|
||||
Assert.Null(Sidecar(fx, sequence).Removed);
|
||||
var before = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(before.Entries.Single().ConversionDeleted);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnhashed", replaced.Title), (await EmptyAsync(fx, engine, before, JobState.Failed)).LogTail);
|
||||
|
||||
// A routine verify hashes every photo, the damaged conversion too: still nothing says the conversion was deleted, so the
|
||||
// originals, the only good copy of that photo, are neither called the last copy nor erased.
|
||||
await fx.Service.Verifier.VerifyAsync(fx.Archive, [id], default, (_, _, _) => ValueTask.CompletedTask, cancellationToken: fx.Ct);
|
||||
Assert.True(Sidecar(fx, sequence).Frames.All(f => f.Sha256 is not null), "verified");
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted, "the archive does not say the conversion was deleted");
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnknown", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
var record = Assert.NotNull(FrameTrash.ReadEntry(fx.Paths, replaced.Name));
|
||||
Assert.Equal(Sha(photos[0]), Sha(Path.Combine(FrameTrash.Location(fx.Paths, replaced.Name).Folder, record.Photos[0].Files[0].Path)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OriginalsOfASequenceAnotherHostDeletedAndErasedGoAsTheDryRunSays()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0, 1], 500);
|
||||
// A desktop sharing the archive deletes the converted sequence and erases its entry alone; this index still lists it.
|
||||
var (other, archive) = await fx.OtherWriterAsync();
|
||||
await other.Trash.DeleteAsync(await other.Trash.PlanAsync([id], null, fx.Ct), null, null, fx.Ct);
|
||||
var deleted = (await other.ListTrashAsync(archive.Id, fx.Ct)).Single(e => e.Kind == TrashKind.Sequence);
|
||||
var erased = await other.Trash.EmptyAsync(archive, Parameters(await other.Trash.PlanEmptyAsync(archive, [deleted.Name], fx.Ct)), 0,
|
||||
(_, _) => ValueTask.CompletedTask, fx.Ct);
|
||||
Assert.Equal((1, 0), (erased.Erased, erased.Refused.Count));
|
||||
Assert.NotNull(await fx.Db.Sequences.GetAsync(id));
|
||||
var erasures = Path.Combine(SequenceTrash.TrashDirectory(fx.Paths), ".erased");
|
||||
Assert.FileExists(Path.Combine(erasures, $"{id:N}.json"));
|
||||
|
||||
// The trash's record of that erasing is the archive's word that the conversions went with the sequence, whatever this
|
||||
// index still lists: the dry run says so, and the emptying confirmed with it erases.
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.True(plan.Entries.Single().ConversionDeleted, "the trash erased the sequence that held the conversions");
|
||||
await EmptyAsync(fx, engine, plan, JobState.Done);
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
Assert.False(Directory.Exists(erasures), "no entry needs the record any more");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OriginalsOfASequenceGoneWithoutTheTrashErasingItStay()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, photos) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500);
|
||||
var sequence = (await fx.Db.Sequences.GetAsync(id))!;
|
||||
|
||||
// The sequence's folder removed outside Chronos while this index still lists it: kept, and the line says what drops the row.
|
||||
Directory.Delete(fx.Paths.Sequence(sequence.Folder).Folder, recursive: true);
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptStaleIndex", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
|
||||
// Rebuilt, the index lists it no more; still nothing in the archive says the conversion was deleted, so the originals,
|
||||
// the only copy of that photo there is, stay.
|
||||
await fx.ScanAsync(ScanMode.Rebuild);
|
||||
Assert.Null(await fx.Db.Sequences.GetAsync(id));
|
||||
var gone = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.True(gone.Entries.Single().ConversionDeleted, "no sequence of that id anywhere");
|
||||
await EmptyAsync(fx, engine, gone, JobState.Done);
|
||||
plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted, "no sequence of that id anywhere, and no record of erasing it");
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnknown", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
|
||||
// Restored, as the line says, it comes back as a sequence of its own.
|
||||
var restored = await fx.Service.RestoreEntryAsync(fx.Archive.Id, replaced.Name, null, fx.Ct);
|
||||
Assert.True(restored.NewSequence);
|
||||
Assert.SequenceEqual([Sha(photos[0])], (await fx.Db.Frames.ListBySequenceAsync(restored.Sequence.Id)).Select(f => f.Sha256!));
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OriginalsStayWhileTheirSequenceIsInAFolderWithoutSidecar()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500);
|
||||
// Another index moves the sequence, its sidecar is lost there, and this index, which never saw the move, lists it no more.
|
||||
var (other, archive) = await fx.OtherWriterAsync();
|
||||
await other.ApplyRenameAsync(await other.PlanRenameAsync(archive.Id, [id], "moved/{title}", null, fx.Ct), null, fx.Ct);
|
||||
var moved = ArchiveScanner.FindSequenceFolders(fx.Paths, fx.Ct).Select(s => fx.Paths.Sequence(s.RelativeFolder)).Single();
|
||||
Assert.True(moved.RelativeFolder.StartsWith("moved", StringComparison.Ordinal), moved.RelativeFolder);
|
||||
File.Delete(moved.ManifestPath);
|
||||
await fx.Db.Sequences.DeleteAsync(id, fx.Ct);
|
||||
|
||||
// The walk of the archive cannot tell whose photos those are, and the trash never erased the sequence: its conversion,
|
||||
// alive there, is not taken for deleted, and the originals stay without it being hashed.
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.False(plan.Entries.Single().ConversionDeleted);
|
||||
Assert.Contains(Loc.L("Archive.Log.TrashKeptUnknown", replaced.Title), (await EmptyAsync(fx, engine, plan, JobState.Failed)).LogTail);
|
||||
Assert.Equal(replaced.Name, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Name);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task OriginalsOfAConvertedSequenceErasedBeforeThemGoAsTheDryRunSays()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var (second, _) = await UploadedAsync(fx, 2, "upload-2");
|
||||
var engine = await fx.StartEngineAsync();
|
||||
var replaced = await ReplaceAsync(fx, id, [0, 1], 500);
|
||||
var together = await ReplaceAsync(fx, second, [0], 600);
|
||||
await fx.Service.Trash.DeleteAsync(await fx.Service.Trash.PlanAsync([id, second], null, fx.Ct), null, null, fx.Ct);
|
||||
var erasures = Path.Combine(SequenceTrash.TrashDirectory(fx.Paths), ".erased");
|
||||
|
||||
// The first sequence's entry erased alone: the trash records erasing it, for the originals it leaves there.
|
||||
var deleted = (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single(e => e.Kind == TrashKind.Sequence && e.SequenceId == id);
|
||||
await EmptyAsync(fx, engine, await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, [deleted.Name], fx.Ct), JobState.Done);
|
||||
Assert.Equal($"{id:N}.json", Path.GetFileName(Directory.EnumerateFiles(erasures).Single()));
|
||||
|
||||
// Then everything in one run: the first one's originals as that record says, the second sequence, then its originals
|
||||
// as the record its erasing leaves says. No record is left once no entry needs one.
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.Count(3, plan.Entries);
|
||||
Assert.True(plan.Entries.Single(e => e.Name == replaced.Name).ConversionDeleted, "the trash erased the sequence that held the conversions");
|
||||
Assert.True(plan.Entries.Single(e => e.Name == together.Name).ConversionDeleted, "its sequence is in the trash");
|
||||
await EmptyAsync(fx, engine, plan, JobState.Done);
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
Assert.False(Directory.Exists(erasures), "no entry needs a record of erasing any more");
|
||||
Assert.Empty(Directory.EnumerateFiles(SequenceTrash.TrashDirectory(fx.Paths), "*", SearchOption.AllDirectories));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnEmptyingWalksTheArchiveAgainForASequenceThatMovedSinceItsWalk()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
var (id, _) = await UploadedAsync(fx, 2);
|
||||
var (second, _) = await UploadedAsync(fx, 3, "upload-2");
|
||||
var replaced = await ReplaceAsync(fx, id, [0], 500);
|
||||
// Photos deleted from another sequence since: the newer entry, erased first.
|
||||
var row = (await fx.Db.Sequences.GetAsync(second))!;
|
||||
var deletion = await fx.Service.FrameTrash.DeleteAsync(
|
||||
await fx.Service.FrameTrash.PlanDeleteAsync(second, [(await fx.Db.Frames.ListBySequenceAsync(second))[0].Id], row.FramesVersion, fx.Ct), null, null, fx.Ct);
|
||||
// Another index moved the converted sequence: this one finds it by walking the archive, once for the whole plan.
|
||||
var (other, archive) = await fx.OtherWriterAsync();
|
||||
await other.ApplyRenameAsync(await other.PlanRenameAsync(archive.Id, [id], "moved/{title}", null, fx.Ct), null, fx.Ct);
|
||||
var plan = await fx.Service.PlanEmptyTrashAsync(fx.Archive.Id, null, fx.Ct);
|
||||
Assert.SequenceEqual([deletion.Entry.Name, replaced.Name], plan.Entries.Select(e => e.Name));
|
||||
Assert.False(plan.Entries.Single(e => e.Name == replaced.Name).ConversionDeleted, "found where it is now");
|
||||
|
||||
// It moves again while the emptying erases the other entry: the folder that walk found no longer holds it, so the archive
|
||||
// is walked again, and the conversion is hashed where it is now.
|
||||
var result = await fx.Service.Trash.EmptyAsync(fx.Archive, Parameters(plan), 0, async (done, _) =>
|
||||
{
|
||||
if (done == 1) await other.ApplyRenameAsync(await other.PlanRenameAsync(archive.Id, [id], "again/{title}", null, fx.Ct), null, fx.Ct);
|
||||
}, fx.Ct);
|
||||
Assert.Equal((2, 0), (result.Erased, result.Refused.Count));
|
||||
Assert.Empty(await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
}
|
||||
|
||||
|
||||
@@ -252,6 +252,51 @@ public sealed class JobEngineTests(TestContext context)
|
||||
Assert.Empty((await db.Jobs.ListAsync(null, JobKind.LightAnalyse)).Items);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A follow-up of work already done (a sync download's frame swapped to another encoding, whose thumbnails it cleared) is
|
||||
/// queued whatever stops its job meanwhile: a cancel arriving right after the work leaves it queued, and it runs.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public async Task AFollowUpQueuedWhateverStopsItsJobOutlivesACancel()
|
||||
{
|
||||
await using var db = await OpenAsync();
|
||||
var target = Guid.CreateVersion7();
|
||||
var started = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var queued = new TaskCompletionSource<Job?>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var download = new CallbackHandler(JobKind.Scan, async c =>
|
||||
{
|
||||
started.TrySetResult();
|
||||
try
|
||||
{
|
||||
await Task.Delay(Timeout.InfiniteTimeSpan, c.Cancellation);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
// The work is done; the cancel came before its follow-up was queued.
|
||||
}
|
||||
try
|
||||
{
|
||||
queued.TrySetResult(await c.EnqueueFollowUpAsync(JobKind.Thumbs, [target], "Roof", null, JobPriority.Background, CancellationToken.None));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
queued.TrySetException(ex);
|
||||
}
|
||||
c.Cancellation.ThrowIfCancellationRequested();
|
||||
});
|
||||
await using var engine = new JobEngine(db, [download, new CountingHandler(JobKind.Thumbs, 1)],
|
||||
new JobEngineOptions { Workers = 1, WorkerId = "t", PollInterval = TimeSpan.FromMilliseconds(50) });
|
||||
await engine.StartAsync();
|
||||
var job = await engine.EnqueueAsync(JobKind.Scan, []);
|
||||
await started.Task.WaitAsync(TimeSpan.FromSeconds(10));
|
||||
|
||||
await engine.CancelAsync(job.Id);
|
||||
|
||||
var followUp = Assert.NotNull(await queued.Task.WaitAsync(TimeSpan.FromSeconds(10)), "queued although its job was cancelled");
|
||||
await WaitForStateAsync(db, job.Id, JobState.Cancelled, TimeSpan.FromSeconds(5));
|
||||
Assert.SequenceEqual([target], (await WaitForStateAsync(db, followUp.Id, JobState.Done, TimeSpan.FromSeconds(10))).TargetIds);
|
||||
}
|
||||
|
||||
/// <summary>Waits until <paramref name="condition"/> holds, failing with <paramref name="what"/> after ten seconds.</summary>
|
||||
private static async Task UntilAsync(Func<bool> condition, string what)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user