feat(copies): engine composition, automatic photos, metered hold and App wiring (owner review 6, P5)
CopyEngine puts the planners together behind ICopyEngine so the App can find, compare, integrate, merge, link, unlink and keep pairs apart. It also moves photos that one side deleted to the other side's trash. Each trash step re-reads both sides when applied, and nothing moves if any photo was restored since the dry run. The sync agent now queues the new photos of pairs that were integrated with automatic photos, both ways, at background priority. Failures back off, and three in a row are reported so the pair can show "not sent". On a metered connection, integrations are held like other server transfers. AppServices builds the engine and registers the Integrate and Merge handlers. A CopyWatch looks for local copies after start and after jobs that change photos. Merges wait for workspace work that has not been sent home. Refs #73 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
461dfcf4a3
commit
96b5b4078b
11 files changed
+1377
-8
No files matched your search
@@ -0,0 +1,114 @@
|
||||
using Chronos.Core.Model;
|
||||
using Chronos.Data.Copies;
|
||||
using Chronos.Data.Jobs;
|
||||
using Chronos.Data.Sync;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Data.Tests.Sync;
|
||||
|
||||
/// <summary>
|
||||
/// The new photos of a pair integrated with automatic photos (owner review 6, #73; design §7.3): after a comparison the
|
||||
/// agent queues them both ways in the background; never for a pair only linked or integrated without automatic photos; a
|
||||
/// failed transfer counts, waits for its backoff, and three in a row are told.
|
||||
/// </summary>
|
||||
[Category("sync")]
|
||||
public sealed class SyncAgentCopyTests(TestContext context)
|
||||
{
|
||||
private static readonly SyncAgentOptions Quick = new()
|
||||
{
|
||||
RefreshInterval = TimeSpan.FromHours(1),
|
||||
EventDelay = TimeSpan.FromMilliseconds(20),
|
||||
MinBackoff = TimeSpan.FromMilliseconds(20),
|
||||
MaxBackoff = TimeSpan.FromMilliseconds(200),
|
||||
};
|
||||
|
||||
/// <summary>An engine that queues but never runs, so the queued jobs can be read and settled by the test.</summary>
|
||||
private static JobEngine Queue(SyncFixture f) => new(f.Db, [.. f.Sync.CreateJobHandlers(new SyncFixture.Connector(f.Remote))],
|
||||
new JobEngineOptions { Workers = 1, WorkerId = "test", PollInterval = TimeSpan.FromMilliseconds(50) });
|
||||
|
||||
/// <summary>A local sequence and a server sequence sharing two photos, each with one the other lacks, linked, in <paramref name="state"/>.</summary>
|
||||
private static async Task<(Sequence Local, FakeSequence Remote)> PairAsync(SyncFixture f, string title, CopyState state, bool autoPhotos)
|
||||
{
|
||||
var photos = f.NewPhotos(4);
|
||||
var local = await f.LocalAsync(title, photos.Take(3).ToList());
|
||||
var remote = f.Remote.Add(title + " NAS", [photos[0], photos[1], photos[3]]);
|
||||
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
var row = await f.Sync.Copies.EnsureLinkedAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
if (state != CopyState.Linked)
|
||||
Assert.True(await f.Sync.Copies.SetStateAsync(row.Id, null, state, new CopyAnswer(null, null, null, autoPhotos, DateTimeOffset.UtcNow), f.Ct));
|
||||
return (local, remote);
|
||||
}
|
||||
|
||||
private static async Task<List<Job>> QueuedAsync(SyncFixture f, JobKind kind) =>
|
||||
[.. (await f.Db.Jobs.ListAsync(JobState.Queued, kind, 50, cancellationToken: f.Ct)).Items];
|
||||
|
||||
[Test]
|
||||
public async Task AnIntegratedPairWithAutomaticPhotosQueuesBothWaysInTheBackground()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
var (local, remote) = await PairAsync(f, "Comet", CopyState.Integrated, autoPhotos: true);
|
||||
await using var engine = Queue(f);
|
||||
await using var agent = new SyncAgent(f.Sync, f.Remote, () => new SyncPolicy(), engine, Quick);
|
||||
|
||||
await agent.RefreshAsync(f.Ct);
|
||||
await agent.RefreshAsync(f.Ct);
|
||||
|
||||
var upload = (await QueuedAsync(f, JobKind.UploadCommit)).Single();
|
||||
var download = (await QueuedAsync(f, JobKind.Download)).Single();
|
||||
Assert.Equal((local.Id, JobPriority.Background), (upload.TargetIds.Single(), upload.Priority));
|
||||
Assert.Equal((remote.Id, JobPriority.Background), (download.TargetIds.Single(), download.Priority));
|
||||
Assert.Contains(local.ArchiveId.ToString(), download.ParametersJson ?? string.Empty);
|
||||
Assert.Count(0, agent.PhotoFailures);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task APairOnlyLinkedOrWithoutAutomaticPhotosQueuesNothing()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
await PairAsync(f, "Linked", CopyState.Linked, autoPhotos: false);
|
||||
await PairAsync(f, "Manual", CopyState.Integrated, autoPhotos: false);
|
||||
await using var engine = Queue(f);
|
||||
await using var agent = new SyncAgent(f.Sync, f.Remote, () => new SyncPolicy(), engine, Quick);
|
||||
|
||||
await agent.RefreshAsync(f.Ct);
|
||||
|
||||
Assert.Count(0, await QueuedAsync(f, JobKind.UploadCommit));
|
||||
Assert.Count(0, await QueuedAsync(f, JobKind.Download));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FailedTransfersCountWaitForTheirBackoffAndThreeInARowAreTold()
|
||||
{
|
||||
await using var f = await SyncFixture.CreateAsync(context);
|
||||
var photos = f.NewPhotos(3);
|
||||
var local = await f.LocalAsync("Comet", photos);
|
||||
var remote = f.Remote.Add("Comet NAS", photos.Take(2));
|
||||
await f.Sync.LinkAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
var row = await f.Sync.Copies.EnsureLinkedAsync(f.Server, remote.Id, local.Id, f.Ct);
|
||||
await f.Sync.Copies.SetStateAsync(row.Id, null, CopyState.Integrated, new CopyAnswer(null, null, null, true, DateTimeOffset.UtcNow), f.Ct);
|
||||
await using var engine = Queue(f);
|
||||
await using var agent = new SyncAgent(f.Sync, f.Remote, () => new SyncPolicy(), engine, Quick);
|
||||
var told = new List<(Guid, AutoPhotoFailure)>();
|
||||
agent.PhotosNotSent += (id, failure) => told.Add((id, failure));
|
||||
|
||||
for (var attempt = 1; attempt <= SyncAgent.NotSentAfter; attempt++)
|
||||
{
|
||||
await agent.RefreshAsync(f.Ct);
|
||||
var upload = (await QueuedAsync(f, JobKind.UploadCommit)).Single();
|
||||
await f.Db.Jobs.SetStateAsync(upload.Id, JobState.Failed, "Quota reached", f.Ct);
|
||||
|
||||
await agent.RefreshAsync(f.Ct);
|
||||
Assert.Equal(new AutoPhotoFailure(attempt, "Quota reached"), agent.PhotoFailures[local.Id]);
|
||||
// Within its backoff nothing new is queued.
|
||||
Assert.Count(0, await QueuedAsync(f, JobKind.UploadCommit));
|
||||
await Task.Delay(Quick.MaxBackoff + TimeSpan.FromMilliseconds(50), f.Ct);
|
||||
}
|
||||
Assert.Equal((local.Id, SyncAgent.NotSentAfter), (told.Single().Item1, told.Single().Item2.Count));
|
||||
|
||||
await agent.RefreshAsync(f.Ct);
|
||||
var last = (await QueuedAsync(f, JobKind.UploadCommit)).Single();
|
||||
await f.Db.Jobs.SetStateAsync(last.Id, JobState.Done, null, f.Ct);
|
||||
await agent.RefreshAsync(f.Ct);
|
||||
Assert.Count(0, agent.PhotoFailures);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user