diff --git a/src/Febe.Core/Export/ExportCopier.cs b/src/Febe.Core/Export/ExportCopier.cs index c46e862..08f275f 100644 --- a/src/Febe.Core/Export/ExportCopier.cs +++ b/src/Febe.Core/Export/ExportCopier.cs @@ -591,7 +591,13 @@ public sealed class ExportCopier(ExportEnvironment env) chosen = at; } - batch = batch.Select(i => string.Equals(i.SourceSha256, sha, StringComparison.Ordinal) ? i : i with { SourceSha256 = sha }).ToList(); + if (mode != FrameMode.Still) + { + // A «Still» item keeps the SHA-256 its instant was chosen on: a job that pauses once the item is written again + // (the disk went in the middle of the file) must still see that the file changed. + batch = batch.Select(i => string.Equals(i.SourceSha256, sha, StringComparison.Ordinal) ? i : i with { SourceSha256 = sha }).ToList(); + } + var format = batch[0].Format; var firstId = mode == FrameMode.Count ? await exports.FirstItemIdAsync(x.Run.Id, library.Media.Id, ExportVariant.Frame, format, ct).ConfigureAwait(false) ?? batch[0].Id diff --git a/tests/Febe.Tests/Export/ExportFramesTests.cs b/tests/Febe.Tests/Export/ExportFramesTests.cs index ba2cb94..4c3b5af 100644 --- a/tests/Febe.Tests/Export/ExportFramesTests.cs +++ b/tests/Febe.Tests/Export/ExportFramesTests.cs @@ -346,37 +346,70 @@ public sealed class ExportFramesTests } [TestMethod] - [DataRow(null)] - [DataRow(VideoAnalysisOutcome.Moving)] - [DataRow(VideoAnalysisOutcome.Still)] - public async Task AFrameWaitingForAFileThatChangedIsTheOneOfTheNewAnalysisOrNone(VideoAnalysisOutcome? now) + [DataRow(null, false, false)] + [DataRow(VideoAnalysisOutcome.Moving, false, false)] + [DataRow(VideoAnalysisOutcome.Still, false, false)] + [DataRow(VideoAnalysisOutcome.Still, true, false)] + [DataRow(VideoAnalysisOutcome.Still, false, true)] + [DataRow(VideoAnalysisOutcome.Still, true, true)] + [DataRow(VideoAnalysisOutcome.Moving, true, true)] + public async Task AFrameWaitingForAFileThatChangedIsTheOneOfTheNewAnalysisOrNone(VideoAnalysisOutcome? now, bool pausedWhileWriting, bool changedTwice) { // The job waited (disk full) while the file was replaced: the instant was chosen on the old one, so it would save - // any frame of the new one, whatever it is. + // any frame of the new one, whatever it is. Then the disk may go in the middle of the writing, once the item is + // recorded again: resumed, the job must still know that the file changed (and it may change once more). var ct = TestContext.CancellationToken; - await using var bed = await ExportTestBed.CreateAsync(); + var files = new AwayWhileHashing(); + await using var bed = await ExportTestBed.CreateAsync(e => e with { FileSystem = files }); var video = (await SaveVideoAsync(bed, bed.Marta, "984", Mp4, ".mp4")).Media[0]; var usb = await bed.GeneralAsync(ct: ct); await bed.Db.VideoAnalyses.SetAsync(new VideoAnalysis(video.Sha256!, 1, VideoAnalysisOutcome.Still, Make.T0, TimeSpan.FromMilliseconds(400)), ct); bed.Disk.Free = 0; var start = await bed.CopyAsync(Frames(video, new ExportFramesRequest(FrameMode.Still, null, null, null, ExportFormat.Png)), ct); - byte[] other = [.. Mp4, 0, 0, 0, 8, (byte)'f', (byte)'r', (byte)'e', (byte)'e']; - var sha = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(other)); - await File.WriteAllBytesAsync(bed.Library.Layout.FullPath(bed.Marta, video.RelativePath!), other, ct); - await bed.Db.Media.UpdateAsync((await bed.Db.Media.GetAsync(video.Id, ct))! with { Sha256 = sha, Bytes = other.Length }, ct); - var at = TimeSpan.FromMilliseconds(100); - if (now is { } outcome) + async Task ReplaceAsync(byte[] file, VideoAnalysisOutcome? outcome, TimeSpan instant) { - await bed.Db.VideoAnalyses.SetAsync(new VideoAnalysis(sha, 1, outcome, Make.T0, outcome == VideoAnalysisOutcome.Still ? at : null), ct); + var hash = Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(file)); + await File.WriteAllBytesAsync(bed.Library.Layout.FullPath(bed.Marta, video.RelativePath!), file, ct); + await bed.Db.Media.UpdateAsync((await bed.Db.Media.GetAsync(video.Id, ct))! with { Sha256 = hash, Bytes = file.Length }, ct); + if (outcome is { } found) + { + await bed.Db.VideoAnalyses.SetAsync(new VideoAnalysis(hash, 1, found, Make.T0, found == VideoAnalysisOutcome.Still ? instant : null), ct); + } + + return hash; } + byte[] other = [.. Mp4, 0, 0, 0, 8, (byte)'f', (byte)'r', (byte)'e', (byte)'e']; + var at = TimeSpan.FromMilliseconds(100); + var sha = await ReplaceAsync(other, pausedWhileWriting ? VideoAnalysisOutcome.Still : now, at); bed.Disk.Free = null; - await bed.Service.ResumeAsync([start.Runs.Single().Id], ct); + var runId = start.Runs.Single().Id; + if (pausedWhileWriting) + { + files.OnHash = () => Directory.Move(bed.Usb, bed.Usb + "-via"); + await bed.Service.ResumeAsync([runId], ct); + await bed.Service.WaitAsync(ct); + files.OnHash = null; + Directory.Move(bed.Usb + "-via", bed.Usb); + Assert.AreEqual(ExportRunState.Paused, (await bed.RunAsync(runId, ct)).State); + var waiting = (await bed.ItemsAsync(runId, ct)).Single(); + Assert.AreEqual(ExportItemState.Pending, waiting.State); + Assert.IsNotNull(waiting.RelativePath, "fermo dopo l'annuncio del file da scrivere"); + } + + if (changedTwice) + { + other = [.. other, 0, 0, 0, 8, (byte)'f', (byte)'r', (byte)'e', (byte)'e']; + at = TimeSpan.FromMilliseconds(200); + sha = await ReplaceAsync(other, now, at); + } + + await bed.Service.ResumeAsync([runId], ct); await bed.Service.WaitAsync(ct); - var run = await bed.RunAsync(start.Runs.Single().Id, ct); + var run = await bed.RunAsync(runId, ct); Assert.AreEqual(ExportRunState.Completed, run.State, run.LastError); - var item = (await bed.ItemsAsync(start.Runs.Single().Id, ct)).Single(); + var item = (await bed.ItemsAsync(runId, ct)).Single(); if (now == VideoAnalysisOutcome.Still) { var copy = (await bed.Db.Exports.ListCopiesAsync(usb.Id, ct: ct)).Single(); @@ -500,4 +533,24 @@ public sealed class ExportFramesTests public bool InSession => Interlocked.Decrement(ref _left) >= 0; } + + /// The real files, telling when a file is hashed: the moment the disk of the copies goes. + private sealed class AwayWhileHashing : IExportFileSystem + { + public Action? OnHash { get; set; } + + public Stream OpenRead(string path) => ExportFileSystem.Instance.OpenRead(path); + + public Stream OpenRandomAccess(string path) => ExportFileSystem.Instance.OpenRandomAccess(path); + + public Task ComputeSha256Async(string path, CancellationToken ct) + { + OnHash?.Invoke(); + return ExportFileSystem.Instance.ComputeSha256Async(path, ct); + } + + public bool FileExists(string path) => ExportFileSystem.Instance.FileExists(path); + + public long? FileLength(string path) => ExportFileSystem.Instance.FileLength(path); + } }