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() {