Files
Chronos/tests/Chronos.Server.Tests/Jobs/JobApiTests.cs
T
Alby96andClaude Opus 5.5 92dba8665a
CI / Build and test (pull_request) Successful in 5m35s
CI / Windows tests and installer (pull_request) Skipped
CI / YAML check (pull_request) Successful in 4s
CI / PostgreSQL 16 integration (pull_request) Successful in 4m13s
CI / PostgreSQL 17 integration (pull_request) Successful in 4m8s
CI / Docker image (pull_request) Successful in 1m29s
test(server): scale the job waits by the CI time factor
RenderJobsThatCannotBeReadNeverStopTheEngine timed out on the shared runner
(PostgreSQL 17) while the upload's background jobs ran first: the 15 s wait was
the one job wait not scaled by CHRONOS_TEST_TIME_FACTOR.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 00:41:45 +01:00

140 lines
7.5 KiB
C#

using System.Net;
using System.Net.Http.Json;
using Microsoft.Extensions.DependencyInjection;
using Chronos.Core.Model;
using Chronos.Data.Jobs;
using Chronos.Protocol;
using Chronos.Testing;
namespace Chronos.Server.Tests.Jobs;
[Category("server")]
public sealed class JobApiTests(TestContext context)
{
[Test]
public async Task JobsMoveThroughPauseResumeAndDone()
{
var handler = new BlockingJobHandler(JobKind.DeepAnalyse);
await using var server = await TestServer.StartAsync(context, services: s => s.AddSingleton<IJobHandler>(handler));
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var sequence = await CreateSequenceAsync(editor);
var job = await CreateAsync(editor, "deep-analyse", sequence);
Assert.Equal(("queued", "user", "Harbour sunset"), (job.State, job.Priority, job.TargetLabel));
await handler.Started.Task.WaitAsync(TimeSpan.FromSeconds(10));
await WaitForAsync(editor, job.Id, "running");
Assert.Equal("paused", (await ActAsync(editor, job.Id, "pause", HttpStatusCode.OK)).State);
await WaitForAsync(editor, job.Id, "paused");
await ActAsync(editor, job.Id, "pause", HttpStatusCode.Conflict);
await ActAsync(editor, job.Id, "retry", HttpStatusCode.Conflict);
await ActAsync(editor, job.Id, "resume", HttpStatusCode.OK);
await WaitForAsync(editor, job.Id, "running");
handler.Release();
var done = await WaitForAsync(editor, job.Id, "done");
Assert.Equal((4L, 4L), (done.ProgressDone, done.ProgressTotal));
await ActAsync(editor, job.Id, "cancel", HttpStatusCode.Conflict);
using var list = await editor.GetAsync("/api/v1/jobs?state=done&kind=deep-analyse");
Assert.Equal(job.Id, (await list.ReadAsync(ProtocolJson.Default.PageJobDto)).Items.Single().Id);
}
[Test]
public async Task FailedJobsRetryAndRunningJobsCancel()
{
var handler = new BlockingJobHandler(JobKind.Proxies) { FailWith = "decoder exploded" };
await using var server = await TestServer.StartAsync(context, services: s => s.AddSingleton<IJobHandler>(handler));
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var sequence = await CreateSequenceAsync(editor);
var job = await CreateAsync(editor, "proxies", sequence);
var failed = await WaitForAsync(editor, job.Id, "failed");
Assert.Equal("decoder exploded", failed.Error);
Assert.Contains("decoder exploded", failed.LogTail);
await ActAsync(editor, job.Id, "retry", HttpStatusCode.OK);
await WaitForAsync(editor, job.Id, "running");
Assert.Equal("cancelled", (await ActAsync(editor, job.Id, "cancel", HttpStatusCode.OK)).State);
await Task.Delay(200);
Assert.Equal("cancelled", (await GetAsync(editor, job.Id)).State);
}
[Test]
public async Task WaitingJobsChangePriorityAndRightsAreChecked()
{
var handler = new BlockingJobHandler(JobKind.Thumbs);
await using var server = await TestServer.StartAsync(context, o => o.Parallelism = 1, s => s.AddSingleton<IJobHandler>(handler));
var (editor, _, _) = await server.UserAsync("alberto", "editor");
var (viewer, _, _) = await server.UserAsync("marco", "viewer");
var sequence = await CreateSequenceAsync(editor);
var first = await CreateAsync(editor, "thumbs", sequence);
await WaitForAsync(editor, first.Id, "running");
var second = await CreateAsync(editor, "thumbs", sequence);
using (var lower = await editor.PostAsJsonAsync($"/api/v1/jobs/{second.Id}/priority", new JobPriorityRequest("background"), ProtocolJson.Default.JobPriorityRequest))
Assert.Equal("background", (await lower.ReadAsync(ProtocolJson.Default.JobDto)).Priority);
using (var bad = await editor.PostAsJsonAsync($"/api/v1/jobs/{second.Id}/priority", new JobPriorityRequest("urgent"), ProtocolJson.Default.JobPriorityRequest))
await bad.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_priority");
using (var foreign = await viewer.PostAsync($"/api/v1/jobs/{second.Id}/cancel", null)) await foreign.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
using (var create = await viewer.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("thumbs", [sequence], null), ProtocolJson.Default.CreateJobRequest))
await create.ExpectErrorAsync(HttpStatusCode.Forbidden, "forbidden");
using (var unknown = await editor.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("teleport", [sequence], null), ProtocolJson.Default.CreateJobRequest))
await unknown.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_job_kind");
using (var unavailable = await editor.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("deep-analyse", [sequence], null), ProtocolJson.Default.CreateJobRequest))
await unavailable.ExpectErrorAsync(HttpStatusCode.Conflict, "job_unavailable");
using (var render = await editor.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest("render", [sequence], "{}"), ProtocolJson.Default.CreateJobRequest))
await render.ExpectErrorAsync(HttpStatusCode.BadRequest, "invalid_parameters");
var admin = await server.AdminAsync();
using (var removed = await admin.PostAsync($"/api/v1/jobs/{second.Id}/cancel", null)) await removed.ExpectAsync(HttpStatusCode.OK);
handler.Release();
await WaitForAsync(editor, first.Id, "done");
}
public static async Task<Guid> CreateSequenceAsync(HttpClient client, string title = "Harbour sunset")
{
using var created = await client.PostAsJsonAsync("/api/v1/sequences", new CreateSequenceRequest(title, null), ProtocolJson.Default.CreateSequenceRequest);
await created.ExpectAsync(HttpStatusCode.Created);
return (await created.ReadAsync(ProtocolJson.Default.SequenceDto)).Id;
}
public static async Task<JobDto> CreateAsync(HttpClient client, string kind, Guid target, string? parameters = null)
{
using var response = await client.PostAsJsonAsync("/api/v1/jobs", new CreateJobRequest(kind, [target], parameters), ProtocolJson.Default.CreateJobRequest);
await response.ExpectAsync(HttpStatusCode.Accepted);
return await response.ReadAsync(ProtocolJson.Default.JobDto);
}
public static async Task<JobDto> GetAsync(HttpClient client, Guid id)
{
using var response = await client.GetAsync($"/api/v1/jobs/{id}");
return await response.ReadAsync(ProtocolJson.Default.JobDto);
}
public static async Task<JobDto> WaitForAsync(HttpClient client, Guid id, string state)
{
var deadline = DateTime.UtcNow.AddSeconds(TestTiming.Budget(15));
while (true)
{
var job = await GetAsync(client, id);
if (job.State == state) return job;
if (DateTime.UtcNow > deadline) Assert.Fail($"Job {id} is {job.State}, expected {state}");
await Task.Delay(50);
}
}
private static async Task<JobDto> ActAsync(HttpClient client, Guid id, string action, HttpStatusCode expected)
{
using var response = await client.PostAsync($"/api/v1/jobs/{id}/{action}", null);
if (expected != HttpStatusCode.OK)
{
await response.ExpectErrorAsync(expected, "invalid_state");
return await GetAsync(client, id);
}
await response.ExpectAsync(expected);
return await response.ReadAsync(ProtocolJson.Default.JobDto);
}
}