CI / Build and test (push) Successful in 9m26s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 8s
Release / Build, test and publish (push) Successful in 14m31s
CI / PostgreSQL 16 integration (push) Successful in 9m53s
CI / PostgreSQL 17 integration (push) Successful in 9m22s
CI / Docker image (push) Successful in 44s
Release / Server image (push) Successful in 1m36s
Originals in the workspace, local render and send home with provenance, the workspace clean-up, size limit and trash, moving the workspace folder, and the complete Settings › Workspace page (owner review 2, R8, phase 5c). One adversarial review round (35 findings, 32 fixed), a copy-cancel race fixed, 184 strings in all 38 languages. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
562 lines
29 KiB
C#
562 lines
29 KiB
C#
using System.Globalization;
|
|
using System.Security.Cryptography;
|
|
using Chronos.Core.Localization;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Data.Archiving;
|
|
using Chronos.Data.Jobs;
|
|
using Chronos.Data.Processing;
|
|
using Chronos.Data.Workspaces;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Data.Tests.Workspaces;
|
|
|
|
/// <summary>
|
|
/// Moving the workspace folder with its projects (phase 5c, P6): renamed on one volume, copied and hash-verified across
|
|
/// volumes (one disk told it is two), a damaged copy that never costs the source, a move stopped and taken up from its
|
|
/// journal, the refusals of the plan, a failure undone, and the job that carries it out.
|
|
/// </summary>
|
|
[Category("workspace")]
|
|
public sealed class WorkspaceMoveTests(TestContext context)
|
|
{
|
|
static WorkspaceMoveTests() => Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
|
|
|
|
[Test]
|
|
public async Task OnOneVolumeEveryProjectIsRenamedAndTheDesktopToldOnce()
|
|
{
|
|
var (from, ids) = Workspace(projects: 2);
|
|
File.WriteAllText(Path.Combine(from, "notes.txt"), "the user's own");
|
|
var before = Contents(from);
|
|
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
|
var mover = new WorkspaceMover(from, to);
|
|
var survey = mover.Survey();
|
|
Assert.True(survey.CanMove, survey.RefusalText);
|
|
Assert.True(survey.SameVolume, "both folders lie in the test's own temporary folder");
|
|
Assert.SequenceEqual([.. ids.Select(i => i.ToString("N")).Order(StringComparer.Ordinal), WorkspaceStore.ManifestFile, ".trash"], survey.Entries);
|
|
Assert.Equal(before.Keys.Count(k => k != "notes.txt" && !IsLease(k)), survey.Files, "the leases and the user's file are not counted");
|
|
Assert.True(survey.Projects.All(p => p.IsProject));
|
|
|
|
var told = new List<(string, string)>();
|
|
var log = new List<string>();
|
|
var (_, resumed) = await mover.MoveAsync(survey.Fingerprint, (f, t) =>
|
|
{
|
|
told.Add((f, t));
|
|
return Task.CompletedTask;
|
|
}, log: log.Add, cancellationToken: context.Cancellation);
|
|
|
|
Assert.False(resumed);
|
|
Assert.SequenceEqual([(mover.From, mover.To)], told);
|
|
// Everything of the workspace is at the target as it was, the lease with its project; the user's file stays.
|
|
var after = Contents(to);
|
|
foreach (var (name, sha) in before.Where(b => b.Key != "notes.txt")) Assert.Equal(sha, after.GetValueOrDefault(name), name);
|
|
Assert.SequenceEqual(["notes.txt"], Directory.EnumerateFileSystemEntries(from).Select(Path.GetFileName).ToList());
|
|
Assert.False(WorkspaceMover.HasJournal(from));
|
|
Assert.Contains(Loc.L("Archive.Log.WorkspaceMoved", mover.To, 2, survey.Files), log);
|
|
Assert.Equal(ids.Count, new WorkspaceStore(to).List().Count);
|
|
}
|
|
|
|
[Test]
|
|
public async Task AcrossVolumesEveryFileIsCopiedAndCheckedBeforeTheSourceGoes()
|
|
{
|
|
var (from, ids) = Workspace(projects: 3);
|
|
var before = Contents(from);
|
|
var times = File.GetLastWriteTimeUtc(Path.Combine(from, ids[0].ToString("N"), WorkspaceProject.BundleFile));
|
|
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
|
var atSwitch = (Source: new Dictionary<string, string>(), Target: new Dictionary<string, string>());
|
|
var mover = new WorkspaceMover(from, to)
|
|
{
|
|
SameVolume = false,
|
|
Cut = (step, _) =>
|
|
{
|
|
if (step != "switched") return;
|
|
atSwitch = (Contents(from), Contents(to));
|
|
},
|
|
};
|
|
var survey = mover.Survey();
|
|
Assert.False(survey.SameVolume);
|
|
var told = 0;
|
|
await mover.MoveAsync(survey.Fingerprint, (_, _) =>
|
|
{
|
|
told++;
|
|
// Told only once everything is at the target: the source is still whole.
|
|
Assert.Equal(before.Count, Contents(from).Count);
|
|
return Task.CompletedTask;
|
|
}, cancellationToken: context.Cancellation);
|
|
|
|
Assert.Equal(1, told);
|
|
// At the switch both sides held everything (the lease is the project holder's: not copied).
|
|
Assert.Equal(before.Count, atSwitch.Source.Count);
|
|
foreach (var (name, sha) in before.Where(b => !IsLease(b.Key))) Assert.Equal(sha, atSwitch.Target.GetValueOrDefault(name), name);
|
|
// Then the source went, the journal last, and the folder with them.
|
|
Assert.False(Directory.Exists(from));
|
|
Assert.Equal(times, File.GetLastWriteTimeUtc(Path.Combine(to, ids[0].ToString("N"), WorkspaceProject.BundleFile)), "the copy keeps the file's time");
|
|
Assert.Empty(Directory.EnumerateFiles(to, "*" + WorkspaceMover.PartSuffix, SearchOption.AllDirectories));
|
|
Assert.True(Directory.Exists(Path.Combine(to, ids[0].ToString("N"), "media", "empty")), "an empty folder comes along");
|
|
Assert.Equal(ids.Count, new WorkspaceStore(to).List().Count);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ACopyThatDoesNotMatchItsSourceNeverCostsTheSource()
|
|
{
|
|
var (from, _) = Workspace(projects: 2);
|
|
var before = Contents(from);
|
|
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
|
var copies = 0;
|
|
var mover = new WorkspaceMover(from, to)
|
|
{
|
|
SameVolume = false,
|
|
Cut = (step, path) =>
|
|
{
|
|
// The fourth file is damaged on its way to the target disk.
|
|
if (step != "verify" || ++copies != 4) return;
|
|
var bytes = File.ReadAllBytes(path);
|
|
bytes[0] ^= 0xFF;
|
|
File.WriteAllBytes(path, bytes);
|
|
},
|
|
};
|
|
var told = 0;
|
|
var failed = await Assert.ThrowsAsync<InvalidOperationException>(() => mover.MoveAsync(mover.Survey().Fingerprint, (_, _) =>
|
|
{
|
|
told++;
|
|
return Task.CompletedTask;
|
|
}, cancellationToken: context.Cancellation));
|
|
|
|
Assert.True(failed.Message.StartsWith(Loc.L("Errors.Workspace.MoveVerify", "§").Split('§')[0], StringComparison.Ordinal), failed.Message);
|
|
Assert.Equal(0, told);
|
|
Same(before, Contents(from));
|
|
Assert.False(WorkspaceMover.HasJournal(from), "the move was undone");
|
|
Assert.False(Directory.Exists(to) && Directory.EnumerateFileSystemEntries(to).Any(), "no copy is left at the target");
|
|
}
|
|
|
|
[Test]
|
|
public async Task AMoveStoppedAcrossVolumesGoesOnFromItsJournalWithoutCopyingTwice()
|
|
{
|
|
var (from, _) = Workspace(projects: 3);
|
|
var before = Contents(from);
|
|
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
|
using var stop = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation);
|
|
var copied = 0;
|
|
var first = new WorkspaceMover(from, to)
|
|
{
|
|
SameVolume = false,
|
|
Cut = (step, _) =>
|
|
{
|
|
// The program closes after the fifth file reached the target.
|
|
if (step == "copied" && ++copied == 5) stop.Cancel();
|
|
},
|
|
};
|
|
var fingerprint = first.Survey().Fingerprint;
|
|
await Assert.ThrowsAsync<OperationCanceledException>(() => first.MoveAsync(fingerprint, cancellationToken: stop.Token));
|
|
Assert.True(WorkspaceMover.HasJournal(from), "the journal stays for the next run");
|
|
Same(before, Contents(from));
|
|
Assert.Equal(WorkspaceMoveStage.Moving, WorkspaceMover.ReadJournal(from)!.Stage);
|
|
|
|
// The workspace changed since (a lease taken and let go does not count): the move taken up does not ask the plan again.
|
|
var again = 0;
|
|
var second = new WorkspaceMover(from, to)
|
|
{
|
|
SameVolume = false,
|
|
Cut = (step, _) =>
|
|
{
|
|
if (step == "copied") again++;
|
|
},
|
|
};
|
|
var told = 0;
|
|
var log = new List<string>();
|
|
var (_, resumed) = await second.MoveAsync("another plan", (_, _) =>
|
|
{
|
|
told++;
|
|
return Task.CompletedTask;
|
|
}, log: log.Add, cancellationToken: context.Cancellation);
|
|
|
|
Assert.True(resumed);
|
|
Assert.Equal(1, told);
|
|
var copiedFiles = before.Keys.Count(k => !IsLease(k));
|
|
Assert.Equal(copiedFiles - 5, again, "the files verified before the stop are not copied again");
|
|
Assert.True(log.Any(l => l.StartsWith(Loc.L("Archive.Log.WorkspaceMoveResumed", "§").Split('§')[0], StringComparison.Ordinal)), string.Join("; ", log));
|
|
var after = Contents(to);
|
|
foreach (var (name, sha) in before.Where(b => !IsLease(b.Key))) Assert.Equal(sha, after.GetValueOrDefault(name), name);
|
|
Assert.False(Directory.Exists(from));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AMoveStoppedOnOneVolumeGoesOnFromItsJournal()
|
|
{
|
|
var (from, ids) = Workspace(projects: 3);
|
|
var before = Contents(from);
|
|
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
|
using var stop = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation);
|
|
var first = new WorkspaceMover(from, to)
|
|
{
|
|
SameVolume = true,
|
|
Cut = (step, _) =>
|
|
{
|
|
if (step == "moved") stop.Cancel();
|
|
},
|
|
};
|
|
await Assert.ThrowsAsync<OperationCanceledException>(() => first.MoveAsync(first.Survey().Fingerprint, cancellationToken: stop.Token));
|
|
Assert.True(WorkspaceMover.HasJournal(from));
|
|
Assert.True(Directory.Exists(Path.Combine(to, ids.Select(i => i.ToString("N")).Order(StringComparer.Ordinal).First())), "the first project went");
|
|
|
|
var (_, resumed) = await new WorkspaceMover(from, to) { SameVolume = true }.MoveAsync("whatever", cancellationToken: context.Cancellation);
|
|
Assert.True(resumed);
|
|
Same(before, Contents(to));
|
|
Assert.False(Directory.Exists(from));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AMoveThatFailsOnOneVolumeIsUndone()
|
|
{
|
|
var (from, _) = Workspace(projects: 3);
|
|
var before = Contents(from);
|
|
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
|
var moved = 0;
|
|
var mover = new WorkspaceMover(from, to)
|
|
{
|
|
SameVolume = true,
|
|
Cut = (step, _) =>
|
|
{
|
|
// The second project's rename meets a file someone holds open.
|
|
if (step == "moved" && ++moved == 2) throw new IOException("The file is in use.");
|
|
},
|
|
};
|
|
await Assert.ThrowsAsync<IOException>(() => mover.MoveAsync(mover.Survey().Fingerprint, cancellationToken: context.Cancellation));
|
|
|
|
Same(before, Contents(from));
|
|
Assert.False(WorkspaceMover.HasJournal(from));
|
|
Assert.False(Directory.Exists(to), "the target made for the move went with it");
|
|
}
|
|
|
|
[Test]
|
|
public async Task APlanIsRefusedForTheFolderItselfAFullTargetNoRoomAndAChangedWorkspace()
|
|
{
|
|
var (from, ids) = Workspace(projects: 1);
|
|
Assert.Equal(WorkspaceMoveRefusal.SameFolder, new WorkspaceMover(from, from + Path.DirectorySeparatorChar).Survey().Refusal);
|
|
var inside = Path.Combine(from, "inner");
|
|
var refused = new WorkspaceMover(from, inside).Survey();
|
|
Assert.Equal(WorkspaceMoveRefusal.SameFolder, refused.Refusal);
|
|
Assert.Equal(Loc.L("Errors.Workspace.MoveSameFolder", refused.To), refused.RefusalText);
|
|
|
|
// A target that is a workspace already, or holds a project of this one.
|
|
var other = context.CreateTempDirectory("other");
|
|
_ = new WorkspaceStore(other);
|
|
Assert.Equal(WorkspaceMoveRefusal.TargetNotEmpty, new WorkspaceMover(from, other).Survey().Refusal);
|
|
var holding = context.CreateTempDirectory("holding");
|
|
Directory.CreateDirectory(Path.Combine(holding, ids[0].ToString("N")));
|
|
var full = new WorkspaceMover(from, holding).Survey();
|
|
Assert.Equal(Loc.L("Errors.Workspace.MoveTargetNotEmpty", full.To), full.RefusalText);
|
|
// Unrelated files do not refuse it.
|
|
var used = context.CreateTempDirectory("used");
|
|
File.WriteAllText(Path.Combine(used, "readme.txt"), "x");
|
|
Assert.True(new WorkspaceMover(from, used).Survey().CanMove);
|
|
|
|
// Across volumes the target's drive must hold the workspace and the reserve; a rename needs no room.
|
|
var small = new WorkspaceMover(from, Path.Combine(used, "w")) { SameVolume = false, FreeSpace = _ => 1000, Reserve = 0 }.Survey();
|
|
Assert.Equal((WorkspaceMoveRefusal?)WorkspaceMoveRefusal.NoRoom, small.Refusal);
|
|
Assert.Equal(small.Bytes - 1000, small.Missing);
|
|
Assert.True(new WorkspaceMover(from, Path.Combine(used, "w")) { SameVolume = true, FreeSpace = _ => 1000 }.Survey().CanMove);
|
|
|
|
// A file written after the plan: the move refuses it and touches nothing.
|
|
var to = Path.Combine(used, "moved");
|
|
var mover = new WorkspaceMover(from, to);
|
|
var fingerprint = mover.Survey().Fingerprint;
|
|
Assert.Equal(fingerprint, mover.Survey().Fingerprint, "the plan reads the same twice");
|
|
File.WriteAllText(Path.Combine(from, ids[0].ToString("N"), "versions", "late.json"), "{}");
|
|
var changed = await Assert.ThrowsAsync<InvalidOperationException>(() => mover.MoveAsync(fingerprint, cancellationToken: context.Cancellation));
|
|
Assert.Equal(Loc.L("Errors.Workspace.MoveChanged"), changed.Message);
|
|
Assert.False(Directory.Exists(to));
|
|
Assert.False(WorkspaceMover.HasJournal(from));
|
|
}
|
|
|
|
[Test]
|
|
public async Task TheJobMovesTheWorkspaceAndTellsTheDesktop()
|
|
{
|
|
var (from, ids) = Workspace(projects: 2);
|
|
var before = Contents(from);
|
|
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
|
await using var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("db"), "chronos.db")), context.Cancellation);
|
|
var told = new List<(string, string)>();
|
|
var options = new WorkspaceJobOptions(new ProcessingOptions(), () => false, (f, t) =>
|
|
{
|
|
told.Add((f, t));
|
|
return Task.CompletedTask;
|
|
});
|
|
var handler = new WorkspaceMoveJobHandler(db, options) { Movers = p => new WorkspaceMover(p.From, p.To) { SameVolume = false } };
|
|
await using var engine = new JobEngine(db, [handler],
|
|
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
|
|
|
|
// Another job of the workspace waits (nothing here runs it): the move is refused.
|
|
var copy = await engine.EnqueueAsync(JobKind.WorkspaceCopy, [Guid.CreateVersion7()], "copy", null, cancellationToken: context.Cancellation);
|
|
var survey = new WorkspaceMover(from, to).Survey();
|
|
await engine.StartAsync(context.Cancellation);
|
|
var refused = await engine.EnqueueAsync(WorkspaceJobs.Move(from, to, survey.Fingerprint), cancellationToken: context.Cancellation);
|
|
var failed = await WaitAsync(db, refused.Id);
|
|
Assert.Equal(JobState.Failed, failed.State);
|
|
Assert.Equal(Loc.L("Errors.Workspace.JobsRunning"), failed.Error);
|
|
Same(before, Contents(from));
|
|
|
|
await engine.CancelAsync(copy.Id, context.Cancellation);
|
|
var job = await engine.EnqueueAsync(WorkspaceJobs.Move(from, to, survey.Fingerprint), cancellationToken: context.Cancellation);
|
|
var done = await WaitAsync(db, job.Id);
|
|
Assert.Equal(JobState.Done, done.State, done.Error ?? done.LogTail);
|
|
Assert.SequenceEqual([(survey.From, survey.To)], told);
|
|
Assert.Equal(ids.Count, new WorkspaceStore(to).List().Count);
|
|
Assert.False(Directory.Exists(from));
|
|
Assert.Contains(Loc.L("Archive.Log.WorkspaceMoved", survey.To, 2, survey.Files), done.LogTail);
|
|
}
|
|
|
|
[Test]
|
|
public async Task AMoveCancelledWhilePausedIsUndone()
|
|
{
|
|
var (from, _) = Workspace(projects: 2);
|
|
var before = Contents(from);
|
|
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
|
// A move stopped half way, as a pause leaves it.
|
|
using var stop = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation);
|
|
var mover = new WorkspaceMover(from, to) { SameVolume = true, Cut = (step, _) => stop.Cancel() };
|
|
var fingerprint = mover.Survey().Fingerprint;
|
|
await Assert.ThrowsAsync<OperationCanceledException>(() => mover.MoveAsync(fingerprint, cancellationToken: stop.Token));
|
|
Assert.NotEqual(before.Count, Contents(from).Count);
|
|
|
|
await using var db = await ChronosDatabase.OpenAsync(DatabaseOptions.Sqlite(Path.Combine(context.CreateTempDirectory("db"), "chronos.db")), context.Cancellation);
|
|
var handler = new WorkspaceMoveJobHandler(db, new WorkspaceJobOptions(new ProcessingOptions(), () => false))
|
|
{
|
|
Movers = p => new WorkspaceMover(p.From, p.To) { SameVolume = true },
|
|
};
|
|
var request = WorkspaceJobs.Move(from, to, fingerprint);
|
|
await using var engine = new JobEngine(db, [handler]);
|
|
var job = await engine.EnqueueAsync(request, cancellationToken: context.Cancellation) with { State = JobState.Cancelled };
|
|
await handler.CancelledAsync(job, context.Cancellation);
|
|
var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(TestTiming.Budget(20));
|
|
while (WorkspaceMover.HasJournal(from) && DateTime.UtcNow < deadline) await Task.Delay(20, context.Cancellation);
|
|
|
|
Assert.False(WorkspaceMover.HasJournal(from));
|
|
Same(before, Contents(from));
|
|
Assert.False(Directory.Exists(to));
|
|
}
|
|
|
|
[Test]
|
|
public async Task TwoVolumesUnderOneFolderAreCopiedWhenTheFirstRenameIsRefused()
|
|
{
|
|
var (from, _) = Workspace(projects: 2);
|
|
var before = Contents(from);
|
|
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
|
var renames = 0;
|
|
var copied = 0;
|
|
var mover = new WorkspaceMover(from, to)
|
|
{
|
|
// Both under one drive letter, the target a volume mounted in a folder: the system refuses the rename.
|
|
SameVolume = true,
|
|
Cut = (step, _) =>
|
|
{
|
|
if (step == "rename" && ++renames == 1)
|
|
throw new IOException("The system cannot move the file to a different disk drive.", OperatingSystem.IsWindows() ? unchecked((int)0x80070011) : 18);
|
|
if (step == "copied") copied++;
|
|
},
|
|
};
|
|
var (survey, resumed) = await mover.MoveAsync(mover.Survey().Fingerprint, cancellationToken: context.Cancellation);
|
|
|
|
Assert.False(resumed);
|
|
Assert.True(survey.SameVolume, "planned as one volume");
|
|
Assert.Equal(1, renames);
|
|
Assert.Equal(before.Keys.Count(k => !IsLease(k)), copied, "every file copied and verified instead");
|
|
var after = Contents(to);
|
|
foreach (var (name, sha) in before.Where(b => !IsLease(b.Key))) Assert.Equal(sha, after.GetValueOrDefault(name), name);
|
|
Assert.False(Directory.Exists(from));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AnEntryThatChangedAfterItsCopyIsKeptAndTheMoveEndsWell()
|
|
{
|
|
var (from, ids) = Workspace(projects: 2);
|
|
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
|
var changed = Path.Combine(from, ids[0].ToString("N"), "versions", "0001.json");
|
|
var mover = new WorkspaceMover(from, to)
|
|
{
|
|
SameVolume = false,
|
|
Cut = (step, _) =>
|
|
{
|
|
// Written after its copy was verified, before the source is cleared.
|
|
if (step == "switched") File.AppendAllText(changed, " ");
|
|
},
|
|
};
|
|
var log = new List<string>();
|
|
var survey = mover.Survey();
|
|
await mover.MoveAsync(survey.Fingerprint, log: log.Add, cancellationToken: context.Cancellation);
|
|
|
|
Assert.Equal("{} ", File.ReadAllText(changed), "never deleted");
|
|
Assert.False(Directory.Exists(Path.Combine(from, ids[1].ToString("N"))), "the other project went");
|
|
Assert.Contains(Loc.L("Archive.Log.WorkspaceMoveLeft", mover.From, 1), log);
|
|
Assert.Contains(Loc.L("Archive.Log.WorkspaceMoved", mover.To, 2, survey.Files), log);
|
|
Assert.Equal(2, new WorkspaceStore(to).List().Count);
|
|
}
|
|
|
|
[Test]
|
|
[WindowsOnly]
|
|
public async Task AnEntryHeldOpenAfterTheSwitchStaysInTheJournalAndIsClearedWhenTakenUp()
|
|
{
|
|
var (from, ids) = Workspace(projects: 2);
|
|
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
|
var heldFile = Path.Combine(from, ids[1].ToString("N"), WorkspaceProject.BundleFile);
|
|
FileStream? held = null;
|
|
var mover = new WorkspaceMover(from, to)
|
|
{
|
|
SameVolume = false,
|
|
Cut = (step, _) =>
|
|
{
|
|
// An editor elsewhere holds a file of the old folder open: it cannot be deleted.
|
|
if (step == "switched") held = new FileStream(heldFile, FileMode.Open, FileAccess.Read, FileShare.Read);
|
|
},
|
|
};
|
|
var log = new List<string>();
|
|
try
|
|
{
|
|
// The move ends well: the target is the workspace, the left entry is a notice.
|
|
await mover.MoveAsync(mover.Survey().Fingerprint, log: log.Add, cancellationToken: context.Cancellation);
|
|
}
|
|
finally
|
|
{
|
|
held?.Dispose();
|
|
}
|
|
Assert.Contains(Loc.L("Archive.Log.WorkspaceMoveLeft", mover.From, 1), log);
|
|
var journal = Assert.NotNull(WorkspaceMover.ReadJournal(from));
|
|
Assert.Equal(WorkspaceMoveStage.Switched, journal.Stage);
|
|
Assert.SequenceEqual([ids[1].ToString("N")], journal.Entries);
|
|
Assert.Equal(2, new WorkspaceStore(to).List().Count);
|
|
|
|
// Taken up once let go: told again, the entry cleared, the journal and the old folder gone.
|
|
var told = 0;
|
|
var (_, resumed) = await new WorkspaceMover(from, to) { SameVolume = false }.MoveAsync("whatever", (_, _) =>
|
|
{
|
|
told++;
|
|
return Task.CompletedTask;
|
|
}, cancellationToken: context.Cancellation);
|
|
Assert.True(resumed);
|
|
Assert.Equal(1, told);
|
|
Assert.False(Directory.Exists(from));
|
|
Assert.Equal(2, new WorkspaceStore(to).List().Count);
|
|
}
|
|
|
|
[Test]
|
|
public async Task AMoveCutInsideAnEntryLeavingAPartFinishesWithEveryFileWhole()
|
|
{
|
|
var (from, ids) = Workspace(projects: 2);
|
|
var before = Contents(from);
|
|
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
|
using var stop = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation);
|
|
var copied = 0;
|
|
var first = new WorkspaceMover(from, to)
|
|
{
|
|
SameVolume = false,
|
|
Cut = (step, _) =>
|
|
{
|
|
// The second file of an entry reached the target: the entry is not verified yet.
|
|
if (step == "copied" && ++copied == 2) stop.Cancel();
|
|
},
|
|
};
|
|
var fingerprint = first.Survey().Fingerprint;
|
|
await Assert.ThrowsAsync<OperationCanceledException>(() => first.MoveAsync(fingerprint, cancellationToken: stop.Token));
|
|
Assert.Empty(WorkspaceMover.ReadJournal(from)!.Verified);
|
|
// The program was killed while it wrote the next file: half of it is left as a part beside its name.
|
|
var proxies = Path.Combine(to, ids.Select(i => i.ToString("N")).Order(StringComparer.Ordinal).First(), "media", "proxies");
|
|
Directory.CreateDirectory(proxies);
|
|
File.WriteAllBytes(Path.Combine(proxies, "2.jpg" + WorkspaceMover.PartSuffix), new byte[100]);
|
|
|
|
var (_, resumed) = await new WorkspaceMover(from, to) { SameVolume = false }.MoveAsync("whatever", cancellationToken: context.Cancellation);
|
|
|
|
Assert.True(resumed);
|
|
var after = Contents(to);
|
|
foreach (var (name, sha) in before.Where(b => !IsLease(b.Key))) Assert.Equal(sha, after.GetValueOrDefault(name), name);
|
|
Assert.Empty(Directory.EnumerateFiles(to, "*" + WorkspaceMover.PartSuffix, SearchOption.AllDirectories));
|
|
Assert.False(Directory.Exists(from));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AJobFindsItsProjectWhereTheWorkspaceIsNowAndWaitsWhileItIsMoved()
|
|
{
|
|
var (from, ids) = Workspace(projects: 1);
|
|
var id = ids[0];
|
|
const string missing = "Errors.Workspace.NotCheckedOut";
|
|
Assert.Equal(WorkspaceStore.FullRoot(from), WorkspaceJobRoots.Resolve(from, id, null, missing));
|
|
var other = Assert.Throws<InvalidOperationException>(() => WorkspaceJobRoots.Resolve(from, Guid.CreateVersion7(), null, missing));
|
|
Assert.Equal(Loc.L(missing), other.Message);
|
|
|
|
// A move under way (before its switch): every job of either folder waits for it.
|
|
var to = Path.Combine(context.CreateTempDirectory("target"), "moved");
|
|
using (var stop = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation))
|
|
{
|
|
var moving = new WorkspaceMover(from, to) { SameVolume = false, Cut = (step, _) => { if (step == "copied") stop.Cancel(); } };
|
|
await Assert.ThrowsAsync<OperationCanceledException>(() => moving.MoveAsync(moving.Survey().Fingerprint, cancellationToken: stop.Token));
|
|
}
|
|
Assert.Equal(Loc.L("Errors.Workspace.Moving"), Assert.Throws<InvalidOperationException>(() => WorkspaceJobRoots.Resolve(from, id, null, missing)).Message);
|
|
Assert.Equal(Loc.L("Errors.Workspace.Moving"), Assert.Throws<InvalidOperationException>(() => WorkspaceJobRoots.Resolve(to, id, () => from, missing)).Message);
|
|
|
|
// Past its switch, the source not cleared yet: a job queued for the old folder works in the new one.
|
|
var switched = new WorkspaceMover(from, to) { SameVolume = false, Cut = (step, _) => { if (step == "switched") throw new IOException("closed at the switch"); } };
|
|
await Assert.ThrowsAsync<IOException>(() => switched.MoveAsync("whatever", cancellationToken: context.Cancellation));
|
|
Assert.Equal(WorkspaceMoveStage.Switched, WorkspaceMover.ReadJournal(from)!.Stage);
|
|
Assert.Equal(WorkspaceStore.FullRoot(to), WorkspaceJobRoots.Resolve(from, id, null, missing));
|
|
|
|
// Once ended: the old folder holds nothing; the folder in use does.
|
|
await new WorkspaceMover(from, to) { SameVolume = false }.MoveAsync("whatever", cancellationToken: context.Cancellation);
|
|
Assert.False(Directory.Exists(from));
|
|
Assert.Equal(WorkspaceStore.FullRoot(to), WorkspaceJobRoots.Resolve(from, id, () => to, missing));
|
|
Assert.Equal(Loc.L(missing), Assert.Throws<InvalidOperationException>(() => WorkspaceJobRoots.Resolve(from, id, null, missing)).Message);
|
|
}
|
|
|
|
/// <summary>
|
|
/// A workspace with <paramref name="projects"/> projects (a working state, media with an empty folder, versions, the
|
|
/// lease), its manifest and a trash entry.
|
|
/// </summary>
|
|
private (string Root, List<Guid> Ids) Workspace(int projects)
|
|
{
|
|
var root = Path.Combine(context.CreateTempDirectory("ws"), "workspace");
|
|
_ = new WorkspaceStore(root);
|
|
var ids = new List<Guid>();
|
|
for (var i = 0; i < projects; i++)
|
|
{
|
|
var id = Guid.CreateVersion7();
|
|
ids.Add(id);
|
|
var folder = Path.Combine(root, id.ToString("N"));
|
|
Directory.CreateDirectory(Path.Combine(folder, "media", "proxies"));
|
|
Directory.CreateDirectory(Path.Combine(folder, "media", "empty"));
|
|
Directory.CreateDirectory(Path.Combine(folder, "versions"));
|
|
File.WriteAllText(Path.Combine(folder, WorkspaceProject.BundleFile), "{\"project\": " + i + "}");
|
|
for (var f = 0; f < 3; f++) File.WriteAllBytes(Path.Combine(folder, "media", "proxies", $"{f}.jpg"), RandomNumberGenerator.GetBytes(4096 + f * 1000));
|
|
File.WriteAllText(Path.Combine(folder, "versions", "0001.json"), "{}");
|
|
File.WriteAllText(Path.Combine(folder, ".lock.lease"), "held by nobody");
|
|
}
|
|
var trash = Path.Combine(root, ".trash", "removed-1");
|
|
Directory.CreateDirectory(trash);
|
|
File.WriteAllText(Path.Combine(trash, "kept.json"), "{}");
|
|
return (root, ids);
|
|
}
|
|
|
|
/// <summary>Every file under <paramref name="root"/> by its relative name, with its SHA-256; a move's journal is not counted.</summary>
|
|
private static Dictionary<string, string> Contents(string root) =>
|
|
Directory.Exists(root)
|
|
? Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories).Where(f => Path.GetFileName(f) != WorkspaceMover.JournalFile)
|
|
.ToDictionary(f => Path.GetRelativePath(root, f).Replace('\\', '/'), f => Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(f))))
|
|
: [];
|
|
|
|
/// <summary>Both hold the same files with the same contents.</summary>
|
|
private static void Same(Dictionary<string, string> expected, Dictionary<string, string> actual)
|
|
{
|
|
Assert.SequenceEqual(expected.Keys.Order(StringComparer.Ordinal), actual.Keys.Order(StringComparer.Ordinal));
|
|
foreach (var (name, sha) in expected) Assert.Equal(sha, actual[name], name);
|
|
}
|
|
|
|
private static bool IsLease(string name) => name.EndsWith(".lease", StringComparison.Ordinal);
|
|
|
|
private async Task<Job> WaitAsync(ChronosDatabase db, Guid jobId)
|
|
{
|
|
var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(TestTiming.Budget(60));
|
|
while (true)
|
|
{
|
|
var job = await db.Jobs.GetAsync(jobId, context.Cancellation);
|
|
if (job is { State: JobState.Done or JobState.Failed or JobState.Cancelled }) return job;
|
|
if (DateTime.UtcNow > deadline) Assert.Fail($"Job {jobId} still {job?.State}");
|
|
await Task.Delay(20, context.Cancellation);
|
|
}
|
|
}
|
|
}
|