feat(uploader): upload engine with queue retries and verified-only source deletion
Two parallel files: hash, resumable upload through a transport, then verified or already-on-server. Failed items retry after 30 s up to 3 times on top of the client's transient retries; an unreachable server requeues without spending a retry. A new-sequence batch creates its sequence once. "Delete after upload" removes only files the server confirmed and that still match the hashed size and write time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
4 files changed
+676
No files matched your search
@@ -0,0 +1,41 @@
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Titano.App.Tests.Uploader;
|
||||
|
||||
/// <summary>Runs async test code on one thread, as the UI dispatcher does for the upload engine.</summary>
|
||||
internal sealed class SingleThreadedContext : SynchronizationContext
|
||||
{
|
||||
private readonly BlockingCollection<(SendOrPostCallback Callback, object? State)> _queue = [];
|
||||
|
||||
public override void Post(SendOrPostCallback d, object? state)
|
||||
{
|
||||
try
|
||||
{
|
||||
_queue.Add((d, state));
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
// Late progress reports after the test body finished are dropped.
|
||||
}
|
||||
}
|
||||
|
||||
public override void Send(SendOrPostCallback d, object? state) => throw new NotSupportedException();
|
||||
|
||||
public static void Run(Func<Task> body)
|
||||
{
|
||||
var previous = Current;
|
||||
var context = new SingleThreadedContext();
|
||||
SetSynchronizationContext(context);
|
||||
try
|
||||
{
|
||||
var task = body();
|
||||
task.ContinueWith(_ => context._queue.CompleteAdding(), TaskScheduler.Default);
|
||||
foreach (var (callback, state) in context._queue.GetConsumingEnumerable()) callback(state);
|
||||
task.GetAwaiter().GetResult();
|
||||
}
|
||||
finally
|
||||
{
|
||||
SetSynchronizationContext(previous);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Security.Cryptography;
|
||||
using Titano.Protocol;
|
||||
using Titano.Protocol.Client;
|
||||
using Titano.Testing;
|
||||
using Titano.Uploader.Uploads;
|
||||
|
||||
namespace Titano.App.Tests.Uploader;
|
||||
|
||||
public sealed class UploadEngineTests(TestContext context)
|
||||
{
|
||||
private readonly ManualTime _time = new();
|
||||
|
||||
[Test]
|
||||
public void VerifiedUploadsKeepTheSourceByDefault() => SingleThreadedContext.Run(async () =>
|
||||
{
|
||||
var queue = Queue(UploadTargetMode.Sessions, 2);
|
||||
var transport = new FakeTransport();
|
||||
using var engine = new UploadEngine(queue, transport, () => false, _time);
|
||||
var confirmed = new List<UploadItem>();
|
||||
engine.ItemConfirmed += (_, item) => confirmed.Add(item);
|
||||
|
||||
engine.Pump();
|
||||
await Until(() => queue.Items.All(i => i.IsDone));
|
||||
|
||||
Assert.True(queue.Items.All(i => i.State == UploadItemState.Verified && i.FrameId is not null));
|
||||
Assert.True(queue.Items.All(i => File.Exists(i.Path)));
|
||||
Assert.Equal(Sha256(queue.Items[0].Path), transport.Calls[0].Sha256);
|
||||
Assert.Equal(2, confirmed.Count);
|
||||
});
|
||||
|
||||
[Test]
|
||||
public void DeleteAfterUploadRemovesOnlyConfirmedUnchangedFiles() => SingleThreadedContext.Run(async () =>
|
||||
{
|
||||
var queue = Queue(UploadTargetMode.Sessions, 3);
|
||||
var (verified, existing, changed) = (queue.Items[0], queue.Items[1], queue.Items[2]);
|
||||
var transport = new FakeTransport
|
||||
{
|
||||
Respond = item =>
|
||||
{
|
||||
if (item == existing) return (UploadOutcome.AlreadyOnServer, null);
|
||||
// Written to after it was hashed: the bytes on disk are no longer the verified ones.
|
||||
if (item == changed) File.AppendAllText(item.Path, "more");
|
||||
return (UploadOutcome.Verified, Guid.CreateVersion7());
|
||||
},
|
||||
};
|
||||
using var engine = new UploadEngine(queue, transport, () => true, _time);
|
||||
|
||||
engine.Pump();
|
||||
await Until(() => queue.Items.All(i => i.IsDone));
|
||||
|
||||
Assert.Equal(UploadItemState.Skipped, existing.State);
|
||||
Assert.False(File.Exists(verified.Path));
|
||||
Assert.False(File.Exists(existing.Path));
|
||||
Assert.True(File.Exists(changed.Path), "a file changed after verification is kept");
|
||||
});
|
||||
|
||||
[Test]
|
||||
public void RejectedFileIsRetriedAfterThirtySeconds() => SingleThreadedContext.Run(async () =>
|
||||
{
|
||||
var queue = Queue(UploadTargetMode.Sessions, 1);
|
||||
var item = queue.Items[0];
|
||||
var attempts = 0;
|
||||
var transport = new FakeTransport
|
||||
{
|
||||
Respond = _ => ++attempts == 1
|
||||
? throw new TitanoApiException(HttpStatusCode.UnprocessableEntity, new ApiError("hash_mismatch", "Hash mismatch"))
|
||||
: (UploadOutcome.Verified, Guid.CreateVersion7()),
|
||||
};
|
||||
using var engine = new UploadEngine(queue, transport, () => false, _time);
|
||||
|
||||
engine.Pump();
|
||||
await Until(() => item.State == UploadItemState.Failed);
|
||||
Assert.Equal(UploadFailure.ServerRejected, item.Failure);
|
||||
Assert.Equal("Hash mismatch", item.FailureDetail);
|
||||
Assert.Equal(_time.UtcNow + UploadQueue.RetryDelay, item.NextRetryUtc);
|
||||
|
||||
_time.Advance(TimeSpan.FromSeconds(29));
|
||||
engine.Pump();
|
||||
Assert.Equal(UploadItemState.Failed, item.State);
|
||||
|
||||
_time.Advance(TimeSpan.FromSeconds(1));
|
||||
engine.Pump();
|
||||
await Until(() => item.IsDone);
|
||||
Assert.Equal(1, item.Retries);
|
||||
Assert.Equal(UploadFailure.None, item.Failure);
|
||||
});
|
||||
|
||||
[Test]
|
||||
public void UnreachableServerRequeuesWithoutSpendingARetry() => SingleThreadedContext.Run(async () =>
|
||||
{
|
||||
var queue = Queue(UploadTargetMode.Sessions, 1);
|
||||
var item = queue.Items[0];
|
||||
var transport = new FakeTransport();
|
||||
transport.Respond = _ =>
|
||||
{
|
||||
transport.SetReady(false);
|
||||
throw new ServerUnavailableException(ServerProblem.Unreachable);
|
||||
};
|
||||
using var engine = new UploadEngine(queue, transport, () => false, _time);
|
||||
|
||||
engine.Pump();
|
||||
await Until(() => item.State == UploadItemState.Queued && transport.Calls.Count == 1);
|
||||
Assert.Equal(0, item.Retries);
|
||||
Assert.NotNull(item.Sha256);
|
||||
Assert.Equal(EngineStatus.WaitingForServer, engine.Status);
|
||||
|
||||
transport.Respond = _ => (UploadOutcome.Verified, Guid.CreateVersion7());
|
||||
transport.SetReady(true);
|
||||
await Until(() => item.IsDone);
|
||||
});
|
||||
|
||||
[Test]
|
||||
public void NewSequenceIsCreatedOnceForTheBatch() => SingleThreadedContext.Run(async () =>
|
||||
{
|
||||
var queue = Queue(UploadTargetMode.NewSequence, 3);
|
||||
var transport = new FakeTransport();
|
||||
using var engine = new UploadEngine(queue, transport, () => false, _time);
|
||||
|
||||
engine.Pump();
|
||||
await Until(() => queue.Items.All(i => i.IsDone));
|
||||
|
||||
Assert.Equal(1, transport.SequencesCreated);
|
||||
Assert.True(transport.Calls.All(c => c.SequenceId == FakeTransport.CreatedSequence));
|
||||
Assert.Equal(FakeTransport.CreatedSequence, queue.Batches.Single().SequenceId);
|
||||
});
|
||||
|
||||
[Test]
|
||||
public void AtMostTwoFilesAreInFlightAndPauseRequeuesThem() => SingleThreadedContext.Run(async () =>
|
||||
{
|
||||
var queue = Queue(UploadTargetMode.Sessions, 4);
|
||||
var transport = new FakeTransport { Block = true };
|
||||
using var engine = new UploadEngine(queue, transport, () => false, _time);
|
||||
|
||||
engine.Pump();
|
||||
await Until(() => queue.Items.Count(i => i.State == UploadItemState.Uploading) == UploadEngine.Parallelism);
|
||||
Assert.Equal(UploadEngine.Parallelism, queue.Items.Count(i => i.IsActive));
|
||||
|
||||
engine.Pause();
|
||||
await Until(() => queue.Items.All(i => i.State == UploadItemState.Queued));
|
||||
Assert.Equal(EngineStatus.Paused, engine.Status);
|
||||
|
||||
transport.Block = false;
|
||||
engine.Resume();
|
||||
await Until(() => queue.Items.All(i => i.IsDone));
|
||||
Assert.False(queue.IsPaused);
|
||||
});
|
||||
|
||||
private UploadQueue Queue(UploadTargetMode mode, int count)
|
||||
{
|
||||
var dir = context.CreateTempDirectory();
|
||||
var queue = new UploadQueue();
|
||||
var entries = Enumerable.Range(0, count).Select(i =>
|
||||
{
|
||||
var path = Path.Combine(dir, $"IMG_{i:0000}.JPG");
|
||||
File.WriteAllBytes(path, RandomNumberGenerator.GetBytes(4096 + i));
|
||||
var info = new FileInfo(path);
|
||||
return new QueueEntry(path, info.Length, info.LastWriteTimeUtc, info.LastWriteTime, "k", null);
|
||||
}).ToList();
|
||||
queue.Add(new UploadBatch { Id = Guid.CreateVersion7(), Mode = mode, Title = mode == UploadTargetMode.NewSequence ? "Roof" : null }, entries);
|
||||
return queue;
|
||||
}
|
||||
|
||||
private static string Sha256(string path) => Convert.ToHexStringLower(SHA256.HashData(File.ReadAllBytes(path)));
|
||||
|
||||
private static async Task Until(Func<bool> condition)
|
||||
{
|
||||
var clock = Stopwatch.StartNew();
|
||||
while (!condition())
|
||||
{
|
||||
if (clock.Elapsed > TimeSpan.FromSeconds(15)) throw new TimeoutException("Condition not reached");
|
||||
await Task.Delay(5);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class ManualTime : TimeProvider
|
||||
{
|
||||
private DateTimeOffset _now = new(2026, 9, 28, 18, 0, 0, TimeSpan.Zero);
|
||||
|
||||
public DateTime UtcNow => _now.UtcDateTime;
|
||||
|
||||
public void Advance(TimeSpan by) => _now += by;
|
||||
|
||||
public override DateTimeOffset GetUtcNow() => _now;
|
||||
}
|
||||
|
||||
private sealed class FakeTransport : IUploadTransport
|
||||
{
|
||||
public static readonly Guid CreatedSequence = Guid.CreateVersion7();
|
||||
|
||||
public event EventHandler? ReadyChanged;
|
||||
|
||||
public bool IsReady { get; private set; } = true;
|
||||
public bool Block { get; set; }
|
||||
public int SequencesCreated { get; private set; }
|
||||
public List<(string Path, Guid? SequenceId, string Sha256)> Calls { get; } = [];
|
||||
public Func<UploadItem, (UploadOutcome, Guid?)> Respond { get; set; } = _ => (UploadOutcome.Verified, Guid.CreateVersion7());
|
||||
|
||||
public void SetReady(bool ready)
|
||||
{
|
||||
IsReady = ready;
|
||||
ReadyChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public async Task<(UploadOutcome Outcome, Guid? FrameId)> UploadAsync(UploadItem item, Guid? sequenceId, string sha256, IProgress<UploadProgress> progress, CancellationToken ct)
|
||||
{
|
||||
Calls.Add((item.Path, sequenceId, sha256));
|
||||
progress.Report(new UploadProgress(0, item.Size));
|
||||
while (Block) await Task.Delay(5, ct);
|
||||
await Task.Yield();
|
||||
progress.Report(new UploadProgress(item.Size, item.Size));
|
||||
return Respond(item);
|
||||
}
|
||||
|
||||
public Task<Guid> CreateSequenceAsync(string? title, CancellationToken ct)
|
||||
{
|
||||
SequencesCreated++;
|
||||
return Task.FromResult(CreatedSequence);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user