feat(server): publish card imports as cardEvent
BRIEF §14.3 lists cardEvent among the WebSocket events, but the server had none. A client importing a card into the server now reports its start, progress (at most once a second, a report still waiting replaced by the next) and end to the new POST /api/v1/uploads/cards (Upload permission; import id, card name, state, done, total, failed), and the server publishes a CardEvent naming the account and client to every connected client; the protocol client parses it and offers ReportCardAsync. A report that fails is logged and never stops the import; a server without the endpoint gets no more reports (SRS-10). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
9 files changed
+376
-6
No files matched your search
@@ -26,6 +26,27 @@ public sealed class ServerImportSink(TitanoClient client, string stagingDirector
|
||||
/// <summary>Every file committed or refused so far, by card path; pass it to <see cref="LocalImport.RunAsync"/>.</summary>
|
||||
public ImportSinkReport Report { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Runs a confirmed plan into the server through this sink (<see cref="LocalImport.RunAsync"/>) and tells the server's
|
||||
/// other clients about it as <paramref name="card"/> (BRIEF §14.3 <c>cardEvent</c>): started, its progress, and
|
||||
/// finished, also when it stopped early.
|
||||
/// </summary>
|
||||
public async Task<ImportOutcome> RunAsync(ImportPlan plan, string card, bool verify, IProgress<ImportProgress>? progress, CancellationToken cancellationToken)
|
||||
{
|
||||
var events = new CardImportReporter(client, plan.Id, card, plan.FileCount);
|
||||
events.Started();
|
||||
ImportOutcome? outcome = null;
|
||||
try
|
||||
{
|
||||
outcome = await LocalImport.RunAsync(plan, this, verify, events.Forward(progress), cancellationToken, Report).ConfigureAwait(false);
|
||||
return outcome;
|
||||
}
|
||||
finally
|
||||
{
|
||||
await events.FinishedAsync(outcome).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<SinkCommit>> CommitAsync(PlannedSession session, IReadOnlyList<StagedFile> files, CancellationToken cancellationToken)
|
||||
{
|
||||
var results = new List<SinkCommit>(files.Count);
|
||||
@@ -81,3 +102,123 @@ public sealed class ServerImportSink(TitanoClient client, string stagingDirector
|
||||
private static bool IsRefusal(TitanoApiException ex) =>
|
||||
!ex.IsTransient && ex.Status is not (HttpStatusCode.Unauthorized or HttpStatusCode.Forbidden) && (int)ex.Status is >= 400 and < 500;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The reports of one card import into the server (BRIEF §14.3 <c>cardEvent</c>, §14.4), sent one at a time and in order:
|
||||
/// progress at most once a second, and a progress report still waiting is replaced by the next report, so a slow or
|
||||
/// unreachable server never piles them up. A report that fails is logged and the import goes on; a server without the
|
||||
/// endpoint (404) gets no more reports.
|
||||
/// </summary>
|
||||
/// <param name="total">Photos of the plan, skipped ones included (like <see cref="ImportProgress.ItemsTotal"/>).</param>
|
||||
public sealed class CardImportReporter(TitanoClient client, Guid importId, string card, int total, TimeProvider? time = null)
|
||||
{
|
||||
private static readonly TimeSpan ProgressInterval = TimeSpan.FromSeconds(1);
|
||||
private static readonly TimeSpan SendTimeout = TimeSpan.FromSeconds(10);
|
||||
|
||||
private readonly TimeProvider _time = time ?? TimeProvider.System;
|
||||
private readonly Lock _gate = new();
|
||||
private readonly List<CardImportReport> _queue = [];
|
||||
private Task _sending = Task.CompletedTask;
|
||||
private bool _draining;
|
||||
private bool _finished;
|
||||
private bool _unsupported;
|
||||
private long? _lastProgress;
|
||||
private ImportProgress? _latest;
|
||||
|
||||
public void Started() => Post(CardImportStates.Started, 0, 0);
|
||||
|
||||
/// <summary>Progress that reaches <paramref name="inner"/> (the screen's own) and these reports.</summary>
|
||||
public IProgress<ImportProgress> Forward(IProgress<ImportProgress>? inner) => new Forwarding(this, inner);
|
||||
|
||||
public void Report(ImportProgress progress)
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_latest = progress;
|
||||
var now = _time.GetTimestamp();
|
||||
if (_lastProgress is { } last && _time.GetElapsedTime(last, now) < ProgressInterval) return;
|
||||
_lastProgress = now;
|
||||
}
|
||||
Post(CardImportStates.Progress, progress.ItemsDone, progress.Failed);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reports the end: every photo handled when the import gave an <paramref name="outcome"/>, else as far as its progress
|
||||
/// got. Completes when the reports still queued are sent (each gives up after 10 s).
|
||||
/// </summary>
|
||||
public Task FinishedAsync(ImportOutcome? outcome)
|
||||
{
|
||||
int done, failed;
|
||||
lock (_gate)
|
||||
{
|
||||
done = outcome is null ? _latest?.ItemsDone ?? 0 : total;
|
||||
failed = outcome?.Failures.Count ?? _latest?.Failed ?? 0;
|
||||
}
|
||||
Post(CardImportStates.Finished, done, failed);
|
||||
lock (_gate) return _sending;
|
||||
}
|
||||
|
||||
private void Post(string state, int done, int failed)
|
||||
{
|
||||
var report = new CardImportReport(importId, card, state, Math.Clamp(done, 0, total), total, Math.Clamp(failed, 0, total));
|
||||
lock (_gate)
|
||||
{
|
||||
if (_finished || _unsupported) return;
|
||||
_finished = state == CardImportStates.Finished;
|
||||
if (_queue.Count > 0 && _queue[^1].State == CardImportStates.Progress) _queue[^1] = report;
|
||||
else _queue.Add(report);
|
||||
if (_draining) return;
|
||||
_draining = true;
|
||||
_sending = Task.Run(DrainAsync);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DrainAsync()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
CardImportReport report;
|
||||
lock (_gate)
|
||||
{
|
||||
if (_queue.Count == 0 || _unsupported)
|
||||
{
|
||||
_queue.Clear();
|
||||
_draining = false;
|
||||
return;
|
||||
}
|
||||
report = _queue[0];
|
||||
_queue.RemoveAt(0);
|
||||
}
|
||||
await SendAsync(report).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SendAsync(CardImportReport report)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var timeout = new CancellationTokenSource(SendTimeout);
|
||||
await client.ReportCardAsync(report, timeout.Token).ConfigureAwait(false);
|
||||
}
|
||||
catch (TitanoApiException ex) when (ex.Status is HttpStatusCode.NotFound or HttpStatusCode.MethodNotAllowed)
|
||||
{
|
||||
// A server from before card events.
|
||||
lock (_gate) _unsupported = true;
|
||||
Log.Info("import", $"The server takes no card reports ({ex.Error.Code})");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// The reports only inform the other clients: nothing about them may stop or fail the import.
|
||||
Log.Warning("import", $"The {report.State} report of the import from {card} did not reach the server", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class Forwarding(CardImportReporter reporter, IProgress<ImportProgress>? inner) : IProgress<ImportProgress>
|
||||
{
|
||||
public void Report(ImportProgress value)
|
||||
{
|
||||
inner?.Report(value);
|
||||
reporter.Report(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1165,7 +1165,7 @@ public sealed class ImportViewModel : SectionViewModel
|
||||
if (choices.Destination == ImportDestinationKind.Folder)
|
||||
outcome = await Task.Run(() => LocalImport.RunAsync(plan, new FolderImportSink(choices.FolderPath!), choices.VerifyCopies, progress, cts.Token), cts.Token);
|
||||
else if (Services.Server.Flow is { Address: { } address, Token: { } token })
|
||||
outcome = await RunServerAsync(plan, choices, address, token, progress, cts.Token);
|
||||
outcome = await RunServerAsync(plan, choices, source.Title, address, token, progress, cts.Token);
|
||||
else
|
||||
{
|
||||
Fail(Loc.L("Import.Blocked.NoServer"));
|
||||
@@ -1185,15 +1185,15 @@ public sealed class ImportViewModel : SectionViewModel
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<ImportOutcome> RunServerAsync(ImportPlan plan, ImportChoices choices, Uri address, string token, IProgress<ImportProgress> progress,
|
||||
CancellationToken ct)
|
||||
private async Task<ImportOutcome> RunServerAsync(ImportPlan plan, ImportChoices choices, string card, Uri address, string token,
|
||||
IProgress<ImportProgress> progress, CancellationToken ct)
|
||||
{
|
||||
var staging = Path.Combine(ServerStagingRoot, plan.Id.ToString("N"));
|
||||
using var client = new TitanoClient(address, new CertificatePinning(Services.Server.Fingerprints), $"Titano ({Environment.MachineName})") { Token = token };
|
||||
try
|
||||
{
|
||||
var sink = new ServerImportSink(client, staging);
|
||||
return await Task.Run(() => LocalImport.RunAsync(plan, sink, choices.VerifyCopies, progress, ct, sink.Report), ct);
|
||||
return await Task.Run(() => sink.RunAsync(plan, card, choices.VerifyCopies, progress, ct), ct);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@@ -173,6 +173,13 @@ public sealed class TitanoClient : IDisposable
|
||||
return await RetryAsync(() => SendAsync<CompleteUploadResponse>(HttpMethod.Post, $"api/v1/uploads/{uploadId}/complete", ProtocolJson.Default.CompleteUploadResponse, ct), ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reports a card import this client streams into the server (started, progress, finished); the server tells every
|
||||
/// connected client with a <see cref="CardEvent"/> (BRIEF §14.3).
|
||||
/// </summary>
|
||||
public Task ReportCardAsync(CardImportReport report, CancellationToken ct = default) =>
|
||||
SendAsync(HttpMethod.Post, "api/v1/uploads/cards", report, ProtocolJson.Default.CardImportReport, ct);
|
||||
|
||||
/// <summary>
|
||||
/// Render node: uploads the output of a claimed job in resumable chunks, then completes the job; the server verifies
|
||||
/// the SHA-256 in <paramref name="result"/> and files the render under the sequence's exports (BRIEF §14.7).
|
||||
|
||||
@@ -10,6 +10,7 @@ namespace Titano.Protocol;
|
||||
[JsonDerivedType(typeof(JobProgressEvent), "jobProgress")]
|
||||
[JsonDerivedType(typeof(JobStateEvent), "jobState")]
|
||||
[JsonDerivedType(typeof(SequenceChangedEvent), "sequenceChanged")]
|
||||
[JsonDerivedType(typeof(CardEvent), "cardEvent")]
|
||||
[JsonDerivedType(typeof(NodeStateEvent), "nodeState")]
|
||||
public abstract record ServerEvent;
|
||||
|
||||
@@ -19,4 +20,24 @@ public sealed record JobStateEvent(Guid JobId, string State, string? Error) : Se
|
||||
|
||||
public sealed record SequenceChangedEvent(Guid SequenceId, string Change, int FramesVersion, int SettingsVersion) : ServerEvent;
|
||||
|
||||
/// <summary>
|
||||
/// A card import that a connected client streams into the server (BRIEF §14.4), as that client reported it
|
||||
/// (<see cref="CardImportReport"/>): <see cref="State"/> is one of <see cref="CardImportStates"/>, <see cref="Done"/>
|
||||
/// counts the photos of its plan handled so far (failures included), <see cref="User"/> and <see cref="ClientName"/>
|
||||
/// say who imports it.
|
||||
/// </summary>
|
||||
public sealed record CardEvent(Guid ImportId, string Card, string State, int Done, int Total, int Failed, string User, string? ClientName) : ServerEvent;
|
||||
|
||||
/// <summary>States of a card import report and of its <see cref="CardEvent"/>.</summary>
|
||||
public static class CardImportStates
|
||||
{
|
||||
public const string Started = "started";
|
||||
public const string Progress = "progress";
|
||||
|
||||
/// <summary>The import ended: every photo handled, or fewer when it stopped early.</summary>
|
||||
public const string Finished = "finished";
|
||||
|
||||
public static bool IsKnown(string? state) => state is Started or Progress or Finished;
|
||||
}
|
||||
|
||||
public sealed record NodeStateEvent(Guid NodeId, string ClientName, string State) : ServerEvent;
|
||||
@@ -35,6 +35,7 @@ namespace Titano.Protocol;
|
||||
[JsonSerializable(typeof(ChunkReceived))]
|
||||
[JsonSerializable(typeof(CompleteUploadResponse))]
|
||||
[JsonSerializable(typeof(UploadState))]
|
||||
[JsonSerializable(typeof(CardImportReport))]
|
||||
[JsonSerializable(typeof(JobDto))]
|
||||
[JsonSerializable(typeof(Page<JobDto>))]
|
||||
[JsonSerializable(typeof(CreateJobRequest))]
|
||||
|
||||
@@ -17,6 +17,13 @@ public sealed record CompleteUploadResponse(Guid FrameId, Guid SequenceId, int I
|
||||
/// <summary><c>GET /api/v1/uploads/{id}</c>: resume state.</summary>
|
||||
public sealed record UploadState(Guid UploadId, string FileName, long Size, string Sha256, int ChunkSize, IReadOnlyList<int> Received, string State);
|
||||
|
||||
/// <summary>
|
||||
/// <c>POST /api/v1/uploads/cards</c>: a client importing a card into the server through these uploads reports its start,
|
||||
/// progress and end (BRIEF §14.4); the server tells every connected client with a <see cref="CardEvent"/> (BRIEF §14.3).
|
||||
/// <see cref="ImportId"/> is the client's import plan, <see cref="Card"/> the card's name as the client shows it.
|
||||
/// </summary>
|
||||
public sealed record CardImportReport(Guid ImportId, string Card, string State, int Done, int Total, int Failed);
|
||||
|
||||
/// <summary>
|
||||
/// <c>POST /api/v1/sequences/{id}/renders</c>: a client offers one of its renders of the sequence (BRIEF §14.5,
|
||||
/// renders are uploaded on request). The render keeps its id on the server, so both sides recognise it; when the
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Titano.Core.Model;
|
||||
using Titano.Protocol;
|
||||
using Titano.Server.Events;
|
||||
using Titano.Server.Security;
|
||||
using Titano.Server.Storage;
|
||||
|
||||
@@ -7,10 +8,13 @@ namespace Titano.Server.Api;
|
||||
|
||||
/// <summary>
|
||||
/// <c>/api/v1/uploads</c> (BRIEF §14.4). Chunk, state and cancel calls also serve render nodes uploading their output,
|
||||
/// which start and complete through the node endpoints.
|
||||
/// which start and complete through the node endpoints. <c>POST /uploads/cards</c> takes the reports of a card import a
|
||||
/// client streams through these uploads and tells every connected client (BRIEF §14.3 <c>cardEvent</c>).
|
||||
/// </summary>
|
||||
public static class UploadEndpoints
|
||||
{
|
||||
private const int MaxCardName = 200;
|
||||
|
||||
public static void Map(RouteGroupBuilder api)
|
||||
{
|
||||
var uploads = api.MapGroup("/uploads").Requires(Permissions.Upload | Permissions.RenderNode);
|
||||
@@ -23,6 +27,7 @@ public static class UploadEndpoints
|
||||
uploads.MapPost("/{id:guid}/complete", async (HttpContext context, Caller caller, Guid id, UploadService service) =>
|
||||
(await service.CompleteAsync(caller, id)).ToResult(context, r => Results.Json(r, ProtocolJson.Default.CompleteUploadResponse)))
|
||||
.Requires(Permissions.Upload);
|
||||
uploads.MapPost("/cards", ReportCard).Requires(Permissions.Upload);
|
||||
}
|
||||
|
||||
private static async Task<IResult> StartAsync(HttpContext context, Caller caller, StartUploadRequest request, UploadService service)
|
||||
@@ -32,6 +37,19 @@ public static class UploadEndpoints
|
||||
statusCode: r.AlreadyExists ? StatusCodes.Status200OK : StatusCodes.Status201Created));
|
||||
}
|
||||
|
||||
/// <summary>A card import's start, progress or end, published as a <see cref="CardEvent"/> naming who imports it; 204.</summary>
|
||||
private static IResult ReportCard(HttpContext context, Caller caller, CardImportReport report, EventHub events)
|
||||
{
|
||||
if (report.ImportId == Guid.Empty) return ApiErrors.BadRequest(context, "bad_request", "importId");
|
||||
var card = report.Card?.Trim();
|
||||
if (string.IsNullOrEmpty(card) || card.Length > MaxCardName || card.Any(char.IsControl)) return ApiErrors.BadRequest(context, "bad_request", "card");
|
||||
if (!CardImportStates.IsKnown(report.State)) return ApiErrors.BadRequest(context, "bad_request", "state");
|
||||
if (report.Total < 0 || report.Done < 0 || report.Done > report.Total || report.Failed < 0 || report.Failed > report.Total)
|
||||
return ApiErrors.BadRequest(context, "bad_request", "done");
|
||||
events.Publish(new CardEvent(report.ImportId, card, report.State, report.Done, report.Total, report.Failed, caller.User.Username, caller.ClientName));
|
||||
return Results.NoContent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Chunks are the only large request bodies; every other endpoint keeps Kestrel's small default limit. The service
|
||||
/// raises it to the chunk size of this upload (fixed when it started) before it reads the body.
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
@@ -113,7 +114,88 @@ public sealed class ServerImportSinkTests(TestContext context)
|
||||
Assert.True(ImportPlans.Totals(unlimited, verify: true).HasEnoughSpace);
|
||||
}
|
||||
|
||||
/// <summary>Start, chunk and complete of BRIEF §14.4, plus the title update; the hash is checked at completion.</summary>
|
||||
[Test]
|
||||
public async Task AnImportIntoTheServerIsReportedAsCardEventsFromStartToFinish()
|
||||
{
|
||||
var card = Card(context.CreateTempDirectory("card"), 8, Start);
|
||||
var staging = context.CreateTempDirectory("staging");
|
||||
var server = new FakeUploadServer(chunkSize: 4096);
|
||||
using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
|
||||
var plan = await new UnindexedDestination(ImportDestinationKind.Server, staging).PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
|
||||
var progress = new List<ImportProgress>();
|
||||
|
||||
var outcome = await new ServerImportSink(client, staging).RunAsync(plan, "EOS R5 · card A", true, new SyncProgress<ImportProgress>(progress.Add),
|
||||
context.Cancellation);
|
||||
|
||||
Assert.True(outcome.IsComplete);
|
||||
Assert.NotEmpty(progress);
|
||||
var reports = server.Cards.ToList();
|
||||
Assert.Equal(CardImportStates.Started, reports[0].State);
|
||||
Assert.Equal((CardImportStates.Finished, plan.FileCount, plan.FileCount, 0), (reports[^1].State, reports[^1].Done, reports[^1].Total, reports[^1].Failed));
|
||||
Assert.True(reports.All(r => r.ImportId == plan.Id && r.Card == "EOS R5 · card A"), "every report names the import");
|
||||
Assert.True(reports.Skip(1).SkipLast(1).All(r => r.State == CardImportStates.Progress), "progress in between");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AServerWithoutCardEventsStillTakesTheImport()
|
||||
{
|
||||
var card = Card(context.CreateTempDirectory("card"), 8, Start);
|
||||
var staging = context.CreateTempDirectory("staging");
|
||||
var server = new FakeUploadServer(chunkSize: 4096) { CardsUnsupported = true };
|
||||
using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
|
||||
var plan = await new UnindexedDestination(ImportDestinationKind.Server, staging).PlanAsync(card, new ImportChoices().ToOptions(), null, null, context.Cancellation);
|
||||
|
||||
var outcome = await new ServerImportSink(client, staging).RunAsync(plan, "EOS R5 · card A", true, null, context.Cancellation);
|
||||
|
||||
Assert.Equal(8, outcome.Copied);
|
||||
Assert.Equal(1, server.CardAttempts, "a server that answers 404 gets no further reports");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ReportsWaitingForASlowServerAreReplacedByNewerOnes()
|
||||
{
|
||||
var gate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var server = new FakeUploadServer(chunkSize: 4096) { CardGate = gate.Task };
|
||||
using var client = new TitanoClient(new Uri("https://nas.local:8443/"), new CertificatePinning([]), "tests", server) { Token = "t" };
|
||||
var time = new ManualTime();
|
||||
var reporter = new CardImportReporter(client, Guid.CreateVersion7(), "EOS R5 · card A", 100, time);
|
||||
|
||||
reporter.Started();
|
||||
await server.CardArrived.Task.WaitAsync(TimeSpan.FromSeconds(10));
|
||||
reporter.Report(Progress(10));
|
||||
reporter.Report(Progress(20));
|
||||
time.Advance(TimeSpan.FromSeconds(1));
|
||||
reporter.Report(Progress(30));
|
||||
var finished = reporter.FinishedAsync(null);
|
||||
gate.SetResult();
|
||||
await finished.WaitAsync(TimeSpan.FromSeconds(10));
|
||||
|
||||
// The start was on its way; progress 10 waited and was replaced by 30 (20 came within the second), then by the end.
|
||||
Assert.SequenceEqual([(CardImportStates.Started, 0), (CardImportStates.Finished, 30)], server.Cards.Select(r => (r.State, r.Done)));
|
||||
reporter.Report(Progress(40));
|
||||
Assert.Count(2, server.Cards);
|
||||
}
|
||||
|
||||
private static ImportProgress Progress(int done) => new(done, 100, done * 10L, 1000, done, 0, 0, null);
|
||||
|
||||
/// <summary>Reports synchronously (Progress<T> posts and may arrive late).</summary>
|
||||
private sealed class SyncProgress<T>(Action<T> report) : IProgress<T>
|
||||
{
|
||||
public void Report(T value) => report(value);
|
||||
}
|
||||
|
||||
private sealed class ManualTime : TimeProvider
|
||||
{
|
||||
private long _ticks;
|
||||
|
||||
public override long TimestampFrequency => TimeSpan.TicksPerSecond;
|
||||
|
||||
public override long GetTimestamp() => Interlocked.Read(ref _ticks);
|
||||
|
||||
public void Advance(TimeSpan by) => Interlocked.Add(ref _ticks, by.Ticks);
|
||||
}
|
||||
|
||||
/// <summary>Start, chunk and complete of BRIEF §14.4, plus the title update and card reports; the hash is checked at completion.</summary>
|
||||
private sealed class FakeUploadServer(int chunkSize) : HttpMessageHandler
|
||||
{
|
||||
private readonly Dictionary<Guid, Upload> _open = [];
|
||||
@@ -130,9 +212,34 @@ public sealed class ServerImportSinkTests(TestContext context)
|
||||
/// <summary>Answer 401 to new uploads once this many were committed (the sign-in expired).</summary>
|
||||
public int? SignedOutAfter { get; init; }
|
||||
|
||||
/// <summary>Card reports taken, in order (they arrive on another thread than the uploads).</summary>
|
||||
public ConcurrentQueue<CardImportReport> Cards { get; } = new();
|
||||
|
||||
public int CardAttempts => _cardAttempts;
|
||||
|
||||
/// <summary>A server from before card events: 404.</summary>
|
||||
public bool CardsUnsupported { get; init; }
|
||||
|
||||
/// <summary>Card reports wait for this (a slow server); <see cref="CardArrived"/> says when the first one is waiting.</summary>
|
||||
public Task? CardGate { get; init; }
|
||||
|
||||
public TaskCompletionSource CardArrived { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
private int _cardAttempts;
|
||||
|
||||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
var path = request.RequestUri!.AbsolutePath.Trim('/').Split('/');
|
||||
if (request.Method == HttpMethod.Post && path is ["api", "v1", "uploads", "cards"])
|
||||
{
|
||||
Interlocked.Increment(ref _cardAttempts);
|
||||
if (CardsUnsupported) return new HttpResponseMessage(HttpStatusCode.NotFound);
|
||||
var report = (await request.Content!.ReadFromJsonAsync(ProtocolJson.Default.CardImportReport, cancellationToken))!;
|
||||
CardArrived.TrySetResult();
|
||||
if (CardGate is { } gate) await gate.WaitAsync(cancellationToken);
|
||||
Cards.Enqueue(report);
|
||||
return new HttpResponseMessage(HttpStatusCode.NoContent);
|
||||
}
|
||||
if (request.Method == HttpMethod.Post && path is ["api", "v1", "uploads"])
|
||||
{
|
||||
var start = (await request.Content!.ReadFromJsonAsync(ProtocolJson.Default.StartUploadRequest, cancellationToken))!;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Net.WebSockets;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Titano.Core.Model;
|
||||
@@ -74,6 +75,73 @@ public sealed class EventTests(TestContext context)
|
||||
await plain.ExpectErrorAsync(HttpStatusCode.BadRequest, "websocket_required");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ACardImportReportedByOneClientReachesTheOthersAsCardEvents()
|
||||
{
|
||||
await using var server = await TestServer.StartAsync(context);
|
||||
var (uploaderHttp, _, uploaderToken) = await server.UserAsync("studio-pc", "uploader");
|
||||
var (_, _, editorToken) = await server.UserAsync("alberto", "editor");
|
||||
|
||||
using var listener = new TitanoClient(server.HttpBase, new CertificatePinning([]), "laptop") { Token = editorToken };
|
||||
using var listening = CancellationTokenSource.CreateLinkedTokenSource(context.Cancellation);
|
||||
listening.CancelAfter(TimeSpan.FromSeconds(20));
|
||||
var received = new List<CardEvent>();
|
||||
var connected = new TaskCompletionSource();
|
||||
var reader = Task.Run(async () =>
|
||||
{
|
||||
var events = listener.EventsAsync(listening.Token).GetAsyncEnumerator(listening.Token);
|
||||
var first = events.MoveNextAsync();
|
||||
connected.TrySetResult();
|
||||
try
|
||||
{
|
||||
if (!await first) return;
|
||||
do
|
||||
{
|
||||
if (events.Current is CardEvent card) received.Add(card);
|
||||
if (events.Current is CardEvent { State: CardImportStates.Finished }) return;
|
||||
}
|
||||
while (await events.MoveNextAsync());
|
||||
}
|
||||
finally
|
||||
{
|
||||
await events.DisposeAsync();
|
||||
}
|
||||
});
|
||||
await connected.Task;
|
||||
await WaitForSubscriberAsync(server);
|
||||
|
||||
// The importing client (an upload-only account is enough) reports through the protocol client.
|
||||
using var importer = new TitanoClient(server.HttpBase, new CertificatePinning([]), "Titano (STUDIO)") { Token = uploaderToken };
|
||||
var importId = Guid.CreateVersion7();
|
||||
await importer.ReportCardAsync(new CardImportReport(importId, "EOS R5 · card A", CardImportStates.Started, 0, 120, 0), context.Cancellation);
|
||||
await importer.ReportCardAsync(new CardImportReport(importId, "EOS R5 · card A", CardImportStates.Progress, 60, 120, 1), context.Cancellation);
|
||||
await importer.ReportCardAsync(new CardImportReport(importId, "EOS R5 · card A", CardImportStates.Finished, 120, 120, 2), context.Cancellation);
|
||||
await reader.WaitAsync(TimeSpan.FromSeconds(20));
|
||||
|
||||
Assert.SequenceEqual([CardImportStates.Started, CardImportStates.Progress, CardImportStates.Finished], received.Select(e => e.State));
|
||||
Assert.True(received.All(e => e.ImportId == importId && e.Card == "EOS R5 · card A" && e.Total == 120), "every event names the import");
|
||||
Assert.Equal((60, 1), (received[1].Done, received[1].Failed));
|
||||
// Who imports: the account and the client name it signed in with.
|
||||
Assert.Equal(("studio-pc", "tests"), (received[2].User, received[2].ClientName));
|
||||
|
||||
// Reports are checked like any request.
|
||||
foreach (var (report, field) in new[]
|
||||
{
|
||||
(new CardImportReport(Guid.Empty, "card", CardImportStates.Started, 0, 1, 0), "importId"),
|
||||
(new CardImportReport(importId, " ", CardImportStates.Started, 0, 1, 0), "card"),
|
||||
(new CardImportReport(importId, "card", "paused", 0, 1, 0), "state"),
|
||||
(new CardImportReport(importId, "card", CardImportStates.Progress, 2, 1, 0), "done"),
|
||||
})
|
||||
{
|
||||
using var refused = await uploaderHttp.PostAsJsonAsync("/api/v1/uploads/cards", report, ProtocolJson.Default.CardImportReport);
|
||||
Assert.Equal(field, (await refused.ExpectErrorAsync(HttpStatusCode.BadRequest, "bad_request")).Details);
|
||||
}
|
||||
var (viewer, _, _) = await server.UserAsync("guest", "viewer");
|
||||
using var forbidden = await viewer.PostAsJsonAsync("/api/v1/uploads/cards", new CardImportReport(importId, "card", CardImportStates.Started, 0, 1, 0),
|
||||
ProtocolJson.Default.CardImportReport);
|
||||
await forbidden.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
|
||||
}
|
||||
|
||||
private static async Task WaitForSubscriberAsync(TestServer server)
|
||||
{
|
||||
var hub = server.Services.GetRequiredService<Titano.Server.Events.EventHub>();
|
||||
|
||||
Reference in new issue
Block a user