fix(events): card reports cannot crowd the event queues or hold an import

The server publishes one account's card progress at most once a second
(sooner reports are taken and dropped) and at most 120 card events of an
account a minute (429 rate_limited beyond), so an upload-only account
can no longer push job and sequence events out of every client's queue.

On the client, the end of a card import waits at most two seconds for
its reports to reach the server, then cancels whatever is still on its
way, so a cancelled import or one that lost its server ends at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 10:56:18 +00:00
1 parent 568ba4f21f
commit 1de6717dda
5 files changed
+146 -12

No files matched your search

@@ -142,6 +142,52 @@ public sealed class EventTests(TestContext context)
await forbidden.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
}
[Test]
public async Task OneAccountsCardReportsCannotCrowdTheEventQueues()
{
var clock = new ManualTimeProvider();
await using var server = await TestServer.StartAsync(context, services: s => s.AddSingleton<TimeProvider>(clock));
var (uploader, _, _) = await server.UserAsync("studio-pc", "uploader");
var (other, _, _) = await server.UserAsync("roof-pc", "uploader");
using var subscription = server.Services.GetRequiredService<Titano.Server.Events.EventHub>().Subscribe();
var importId = Guid.CreateVersion7();
// Progress goes out at most once a second; the reports in between are taken and dropped.
for (var done = 1; done <= 5; done++) await ReportCardAsync(uploader, new CardImportReport(importId, "card", CardImportStates.Progress, done, 100, 0));
clock.Advance(TimeSpan.FromSeconds(1));
await ReportCardAsync(uploader, new CardImportReport(importId, "card", CardImportStates.Progress, 6, 100, 0));
Assert.SequenceEqual([1, 6], CardEvents(subscription).Select(e => e.Done));
// Starts and ends always go out, up to 120 events of one account a minute.
for (var i = 0; i < 118; i++) await ReportCardAsync(uploader, new CardImportReport(Guid.CreateVersion7(), "card", CardImportStates.Started, 0, 100, 0));
using (var refused = await uploader.PostAsJsonAsync("/api/v1/uploads/cards",
new CardImportReport(importId, "card", CardImportStates.Finished, 100, 100, 0), ProtocolJson.Default.CardImportReport))
Assert.Equal("59", (await refused.ExpectErrorAsync(HttpStatusCode.TooManyRequests, "rate_limited")).Details);
await ReportCardAsync(other, new CardImportReport(Guid.CreateVersion7(), "card", CardImportStates.Started, 0, 100, 0));
Assert.Count(119, CardEvents(subscription));
clock.Advance(TimeSpan.FromMinutes(1));
await ReportCardAsync(uploader, new CardImportReport(importId, "card", CardImportStates.Finished, 100, 100, 0));
Assert.SequenceEqual([CardImportStates.Finished], CardEvents(subscription).Select(e => e.State));
}
private static async Task ReportCardAsync(HttpClient client, CardImportReport report)
{
using var response = await client.PostAsJsonAsync("/api/v1/uploads/cards", report, ProtocolJson.Default.CardImportReport);
await response.ExpectAsync(HttpStatusCode.NoContent);
}
/// <summary>The card events published since the last call.</summary>
private static List<CardEvent> CardEvents(Titano.Server.Events.EventHub.Subscription subscription)
{
var events = new List<CardEvent>();
while (subscription.Reader.TryRead(out var e))
{
if (e is CardEvent card) events.Add(card);
}
return events;
}
private static async Task WaitForSubscriberAsync(TestServer server)
{
var hub = server.Services.GetRequiredService<Titano.Server.Events.EventHub>();