From 40cdb19d5ef71828ca67ce7900140834b5cf10ff Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 3 Oct 2026 09:43:33 +0000 Subject: [PATCH] test(jobs): selective claims list queued kinds in claim order and claim by id once Runs on SQLite and, with TITANO_TEST_PG, on PostgreSQL. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- .../Repositories/JobRepositoryTests.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/Titano.Data.Tests/Repositories/JobRepositoryTests.cs b/tests/Titano.Data.Tests/Repositories/JobRepositoryTests.cs index cacf4de9..1ead5d8f 100644 --- a/tests/Titano.Data.Tests/Repositories/JobRepositoryTests.cs +++ b/tests/Titano.Data.Tests/Repositories/JobRepositoryTests.cs @@ -59,6 +59,28 @@ public abstract class JobRepositoryTests(TestContext context) : RepositoryTest(c Assert.SequenceEqual([oldUser.Id, normal.Id, oldBackground.Id], order); } + [Test] + public async Task SelectiveClaimersListQueuedKindsInClaimOrderAndClaimById() + { + var db = await OpenTitanoAsync(multiConnection: true); + var oldBackground = TestData.Job(JobKind.Render, JobPriority.Background, TestData.Now.AddMinutes(-10)); + var newUser = TestData.Job(JobKind.Thumbs, JobPriority.User, TestData.Now); + var oldUser = TestData.Job(JobKind.Render, JobPriority.User, TestData.Now.AddMinutes(-1)); + var otherKind = TestData.Job(JobKind.Scan, JobPriority.User, TestData.Now.AddMinutes(-20)); + foreach (var job in new[] { oldBackground, newUser, oldUser, otherKind }) await db.Jobs.EnqueueAsync(job, Ct); + + var queued = await db.Jobs.ListQueuedAsync([JobKind.Render, JobKind.Thumbs], 10, Ct); + Assert.SequenceEqual([oldUser.Id, newUser.Id, oldBackground.Id], queued.Select(j => j.Id)); + Assert.Count(1, await db.Jobs.ListQueuedAsync([JobKind.Render, JobKind.Thumbs], 1, Ct)); + Assert.Empty(await db.Jobs.ListQueuedAsync([], 10, Ct)); + + // Workers racing for the same listed job: exactly one gets it, and it leaves the listing. + var claims = await Task.WhenAll(Enumerable.Range(0, 6).Select(w => Task.Run(() => db.Jobs.TryClaimAsync(oldUser.Id, "worker-" + w, Ct)))); + var claimed = Assert.NotNull(claims.SingleOrDefault(c => c is not null)); + Assert.Equal((JobState.Running, 1), (claimed.State, claimed.Attempts)); + Assert.SequenceEqual([newUser.Id, oldBackground.Id], (await db.Jobs.ListQueuedAsync([JobKind.Render, JobKind.Thumbs], 10, Ct)).Select(j => j.Id)); + } + [Test] public async Task ConcurrentClaimersNeverShareAJob() {