- A take of the library's values cut past its commit point settles its conflict (replay journal marks Settles); a later stale choice finds nothing to settle, and one cut before it keeps the conflict so the next choice is applied. - A local version that changes nothing once replayed on the head is not sent nor written as a Merge; its file stays in versions/.replaced. - Save both journals its intent (name, base, time) before the home is asked, so a cut after the copy was made reuses that copy on retry. - A project deleted at the home reads as Gone from pull, push, resolve and save both, logged once, without retry loops; versions and working state stay. - Save both keeps the restore recorded in sync.json. - The copy job writes a photo's new bytes under a name of its own while the editor's media still name the old file, switching at the commit; a cancelled first copy removes its own half media under the lease (VM hook removed). - Status reads the version files once, re-reads the home's frames only when its stamp moved, reports damage only when confirmed under the lease, and waits once for a busy lease before calling the media outdated. - versions/.replaced is pruned after a sync that ended well (30 days, newest 20 kept, never a file a journal names). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
229 lines
12 KiB
C#
229 lines
12 KiB
C#
using Chronos.Core.Model;
|
|
using Chronos.Data.Archiving;
|
|
using Chronos.Data.Jobs;
|
|
using Chronos.Data.Projects;
|
|
using Chronos.Data.Tests.Archiving;
|
|
using Chronos.Data.Tests.Projects;
|
|
using Chronos.Data.Workspaces;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Data.Tests.Workspaces;
|
|
|
|
/// <summary>An archive with an uploaded sequence as the projects' home, and workspaces that edit it (owner review 2, R8).</summary>
|
|
internal sealed class WorkspaceFixture : IAsyncDisposable
|
|
{
|
|
private readonly TestContext _context;
|
|
private JobEngine? _engine;
|
|
|
|
private WorkspaceFixture(TestContext context, ArchiveFixture archive, Sequence sequence, List<Frame> frames)
|
|
{
|
|
_context = context;
|
|
Archive = archive;
|
|
Sequence = sequence;
|
|
Frames = frames;
|
|
Home = new ArchiveProjectHome(archive.Db, archive.Service.Projects, sequence.Id);
|
|
}
|
|
|
|
public ArchiveFixture Archive { get; }
|
|
public Sequence Sequence { get; set; }
|
|
public List<Frame> Frames { get; }
|
|
public ArchiveProjectHome Home { get; }
|
|
public ChronosDatabase Db => Archive.Db;
|
|
public CancellationToken Ct => Archive.Ct;
|
|
|
|
public static async Task<WorkspaceFixture> CreateAsync(TestContext context, ArchiveFixture archive, int frames = 5)
|
|
{
|
|
var (sequence, list) = await ProjectServiceTests.UploadedAsync(archive, frames);
|
|
return new WorkspaceFixture(context, archive, sequence, list);
|
|
}
|
|
|
|
/// <summary>A workspace of its own (another PC, or another folder of this one) and its sync.</summary>
|
|
public (WorkspaceStore Store, WorkspaceSync Sync) Workspace(string name = "workspace")
|
|
{
|
|
var store = new WorkspaceStore(Path.Combine(_context.CreateTempDirectory(name), "workspace"));
|
|
return (store, new WorkspaceSync(store, "Chronos on " + name) { LockWait = TimeSpan.FromSeconds(TestTiming.Budget(10)) });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Another sync of <paramref name="store"/> that stops, as a crash would, at the <paramref name="occurrence"/>-th time it
|
|
/// reaches step <paramref name="step"/> (<see cref="WorkspaceSync.Cut"/>), throwing <see cref="CutException"/>.
|
|
/// </summary>
|
|
public static WorkspaceSync Cutting(WorkspaceStore store, string step, int occurrence = 1)
|
|
{
|
|
var seen = 0;
|
|
return new WorkspaceSync(store, "Chronos cut")
|
|
{
|
|
LockWait = TimeSpan.FromSeconds(TestTiming.Budget(10)),
|
|
Cut = name =>
|
|
{
|
|
if (name == step && ++seen == occurrence) throw new CutException(step);
|
|
},
|
|
};
|
|
}
|
|
|
|
/// <summary>Main of the sequence checked out into <paramref name="sync"/>'s workspace, its media copied.</summary>
|
|
public async Task<WorkspaceProject> CheckoutAsync(WorkspaceSync sync, Guid? projectId = null)
|
|
{
|
|
var id = projectId ?? (await Archive.Service.Projects.MainAsync(Sequence.Id, Ct)).Id;
|
|
var project = await sync.CheckoutAsync(Home, id, cancellationToken: Ct);
|
|
await CopyAsync(sync.Store, id);
|
|
return project;
|
|
}
|
|
|
|
/// <summary>Runs a workspace copy job to its end.</summary>
|
|
public Task<Job> CopyAsync(WorkspaceStore store, Guid projectId, JobState expected = JobState.Done, string? checkpoint = null, string media = "proxies") =>
|
|
CopyAsync(store.Root, projectId, expected, checkpoint, media);
|
|
|
|
/// <summary>Runs a workspace copy job into the workspace at <paramref name="root"/> to its end.</summary>
|
|
public async Task<Job> CopyAsync(string root, Guid projectId, JobState expected = JobState.Done, string? checkpoint = null, string media = "proxies")
|
|
{
|
|
var engine = await EngineAsync();
|
|
var request = WorkspaceJobs.Copy(Sequence, root, projectId, media);
|
|
var job = checkpoint is null ? await engine.EnqueueAsync(request, cancellationToken: Ct) : await Archive.EnqueueWithCheckpointAsync(engine, request, checkpoint);
|
|
return await Archive.WaitForAsync(job.Id, expected, (int)TestTiming.Budget(60));
|
|
}
|
|
|
|
/// <summary>Queues a workspace copy job and returns at once, while it runs.</summary>
|
|
public async Task<Job> StartCopyAsync(WorkspaceStore store, Guid projectId)
|
|
{
|
|
var engine = await EngineAsync();
|
|
return await engine.EnqueueAsync(WorkspaceJobs.Copy(Sequence, store.Root, projectId), cancellationToken: Ct);
|
|
}
|
|
|
|
/// <summary>Cancels a job, as the user does from the jobs list.</summary>
|
|
public async Task CancelAsync(Guid jobId) => await (await EngineAsync()).CancelAsync(jobId, Ct);
|
|
|
|
/// <summary>Pauses a job, as the user does from the jobs list.</summary>
|
|
public async Task PauseAsync(Guid jobId) => await (await EngineAsync()).PauseAsync(jobId, Ct);
|
|
|
|
private async Task<JobEngine> EngineAsync()
|
|
{
|
|
if (_engine is not null) return _engine;
|
|
_engine = new JobEngine(Db, [.. Archive.Service.CreateJobHandlers(), .. WorkspaceJobs.CreateHandlers(Db, Archive.Service)],
|
|
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50), CheckpointInterval = TimeSpan.FromMilliseconds(50) });
|
|
await _engine.StartAsync(Ct);
|
|
return _engine;
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
if (_engine is not null) await _engine.DisposeAsync();
|
|
}
|
|
}
|
|
|
|
/// <summary>A crash between two writes of a sync (<see cref="WorkspaceFixture.Cutting"/>).</summary>
|
|
internal sealed class CutException(string step) : Exception($"Cut at {step}.");
|
|
|
|
/// <summary>
|
|
/// A home whose pushes can be cut: one refused as out of reach before the home sees it, or one stored whose answer is lost
|
|
/// on the way back (the program killed, the network dropped).
|
|
/// </summary>
|
|
internal sealed class CuttingHome(IProjectHome home) : IProjectHome
|
|
{
|
|
public int Pushes { get; private set; }
|
|
|
|
/// <summary>The push (counted from 1) refused as out of reach.</summary>
|
|
public int? Refuse { get; set; }
|
|
|
|
/// <summary>The push (counted from 1) the home stores and whose answer is lost.</summary>
|
|
public int? LoseAnswer { get; set; }
|
|
|
|
/// <summary>What the home answers every push with, storing nothing (a home with a smaller limit, say); null to store.</summary>
|
|
public PushOutcome? Answer { get; set; }
|
|
|
|
/// <summary>The home answers it holds no versions after any number (an index taken back from an older backup).</summary>
|
|
public bool Forgets { get; set; }
|
|
|
|
public async Task<PushResult> PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken)
|
|
{
|
|
var push = ++Pushes;
|
|
if (push == Refuse) throw new HomeUnavailableException("The home cannot be reached.");
|
|
if (Answer is { } answer) return new PushResult(answer, null, await home.HeadAsync(projectId, cancellationToken));
|
|
var result = await home.PushAsync(projectId, version, cancellationToken);
|
|
if (push == LoseAnswer) throw new HomeUnavailableException("The answer was lost.");
|
|
return result;
|
|
}
|
|
|
|
public Task<HomeSequence> SequenceAsync(CancellationToken cancellationToken) => home.SequenceAsync(cancellationToken);
|
|
public Task<IReadOnlyList<SequenceProject>> ListAsync(CancellationToken cancellationToken) => home.ListAsync(cancellationToken);
|
|
|
|
public Task<SequenceProject> CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken) =>
|
|
home.CreateAsync(name, fromVersionId, author, cancellationToken);
|
|
|
|
public Task<ProjectVersion> HeadAsync(Guid projectId, CancellationToken cancellationToken) => home.HeadAsync(projectId, cancellationToken);
|
|
|
|
public Task<ProjectVersion?> GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken) =>
|
|
home.GetVersionAsync(projectId, versionId, cancellationToken);
|
|
|
|
public Task<IReadOnlyList<ProjectVersion>> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) =>
|
|
Forgets ? Task.FromResult<IReadOnlyList<ProjectVersion>>([]) : home.VersionsAfterAsync(projectId, afterNumber, limit, cancellationToken);
|
|
|
|
public Task<IReadOnlyList<ProjectVersion>> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken) =>
|
|
home.HistoryAsync(projectId, beforeNumber, limit, cancellationToken);
|
|
|
|
public Task<HomeFrames> FramesAsync(CancellationToken cancellationToken) => home.FramesAsync(cancellationToken);
|
|
}
|
|
|
|
/// <summary>A home that counts its reads of the frames, its stamp of them passed through.</summary>
|
|
internal sealed class CountingHome(IProjectHome home) : IProjectHome
|
|
{
|
|
public int FramesReads { get; private set; }
|
|
|
|
public Task<HomeFrames> FramesAsync(CancellationToken cancellationToken)
|
|
{
|
|
FramesReads++;
|
|
return home.FramesAsync(cancellationToken);
|
|
}
|
|
|
|
public Task<string?> FramesStampAsync(CancellationToken cancellationToken) => home.FramesStampAsync(cancellationToken);
|
|
|
|
public Task<HomeSequence> SequenceAsync(CancellationToken cancellationToken) => home.SequenceAsync(cancellationToken);
|
|
public Task<IReadOnlyList<SequenceProject>> ListAsync(CancellationToken cancellationToken) => home.ListAsync(cancellationToken);
|
|
|
|
public Task<SequenceProject> CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken) =>
|
|
home.CreateAsync(name, fromVersionId, author, cancellationToken);
|
|
|
|
public Task<ProjectVersion> HeadAsync(Guid projectId, CancellationToken cancellationToken) => home.HeadAsync(projectId, cancellationToken);
|
|
|
|
public Task<ProjectVersion?> GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken) =>
|
|
home.GetVersionAsync(projectId, versionId, cancellationToken);
|
|
|
|
public Task<IReadOnlyList<ProjectVersion>> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) =>
|
|
home.VersionsAfterAsync(projectId, afterNumber, limit, cancellationToken);
|
|
|
|
public Task<IReadOnlyList<ProjectVersion>> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken) =>
|
|
home.HistoryAsync(projectId, beforeNumber, limit, cancellationToken);
|
|
|
|
public Task<PushResult> PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) => home.PushAsync(projectId, version, cancellationToken);
|
|
}
|
|
|
|
/// <summary>A home that cannot be reached, standing for a server out of reach or an archive offline.</summary>
|
|
internal sealed class UnreachableHome(IProjectHome home) : IProjectHome
|
|
{
|
|
public bool Reachable { get; set; }
|
|
|
|
private T Reach<T>(Func<T> call) => Reachable ? call() : throw new HomeUnavailableException("The home cannot be reached.");
|
|
|
|
public Task<HomeSequence> SequenceAsync(CancellationToken cancellationToken) => Reach(() => home.SequenceAsync(cancellationToken));
|
|
public Task<IReadOnlyList<SequenceProject>> ListAsync(CancellationToken cancellationToken) => Reach(() => home.ListAsync(cancellationToken));
|
|
|
|
public Task<SequenceProject> CreateAsync(string name, Guid? fromVersionId, string? author, CancellationToken cancellationToken) =>
|
|
Reach(() => home.CreateAsync(name, fromVersionId, author, cancellationToken));
|
|
|
|
public Task<ProjectVersion> HeadAsync(Guid projectId, CancellationToken cancellationToken) => Reach(() => home.HeadAsync(projectId, cancellationToken));
|
|
|
|
public Task<ProjectVersion?> GetVersionAsync(Guid projectId, Guid versionId, CancellationToken cancellationToken) =>
|
|
Reach(() => home.GetVersionAsync(projectId, versionId, cancellationToken));
|
|
|
|
public Task<IReadOnlyList<ProjectVersion>> VersionsAfterAsync(Guid projectId, int afterNumber, int limit, CancellationToken cancellationToken) =>
|
|
Reach(() => home.VersionsAfterAsync(projectId, afterNumber, limit, cancellationToken));
|
|
|
|
public Task<IReadOnlyList<ProjectVersion>> HistoryAsync(Guid projectId, int? beforeNumber, int limit, CancellationToken cancellationToken) =>
|
|
Reach(() => home.HistoryAsync(projectId, beforeNumber, limit, cancellationToken));
|
|
|
|
public Task<PushResult> PushAsync(Guid projectId, ProjectVersion version, CancellationToken cancellationToken) =>
|
|
Reach(() => home.PushAsync(projectId, version, cancellationToken));
|
|
|
|
public Task<HomeFrames> FramesAsync(CancellationToken cancellationToken) => Reach(() => home.FramesAsync(cancellationToken));
|
|
}
|