|
| 1 | +using System.Net; |
| 2 | +using Microsoft.AspNetCore.Mvc.Testing; |
| 3 | + |
| 4 | +namespace PythonScriptRunner.Worker.Tests; |
| 5 | + |
| 6 | +/// <summary>Spec 009: the standalone WorkerHost (D-107).</summary> |
| 7 | +public sealed class WorkerHostTests(PostgresFixture postgres, BrokerFixture broker, PythonFixture python) |
| 8 | +{ |
| 9 | + private static CancellationToken Ct => TestContext.Current.CancellationToken; |
| 10 | + |
| 11 | + [Fact] |
| 12 | + public async Task WorkerHost_MigratesAnswersHealthAndRunsQueuedSteps() |
| 13 | + { |
| 14 | + var database = await postgres.CreateDatabaseAsync(); |
| 15 | + var amqp = await broker.CreateVirtualHostAsync(); |
| 16 | + var workRoot = Path.Combine(Path.GetTempPath(), "psr-tests", "work", Guid.NewGuid().ToString("N")); |
| 17 | + await using var factory = new WebApplicationFactory<Program>().WithWebHostBuilder(web => web |
| 18 | + .UseSetting("ConnectionStrings:psr", database) |
| 19 | + .UseSetting("ConnectionStrings:rabbitmq", amqp) |
| 20 | + .UseSetting("PSR_AUTO_MIGRATE", "true") |
| 21 | + .UseSetting("PythonScriptRunner:WorkRoot", workRoot) |
| 22 | + .UseSetting("PythonScriptRunner:UvPath", python.UvPath) |
| 23 | + .UseSetting("PythonScriptRunner:EnvironmentCacheRoot", python.EnvironmentCacheRoot) |
| 24 | + .UseSetting("PythonScriptRunner:PsrunnerRequirement", python.WheelPath) |
| 25 | + .UseSetting("PythonScriptRunner:Worker:MaxConcurrency", "2") |
| 26 | + .UseSetting("Serilog:MinimumLevel:Default", "Warning")); |
| 27 | + using var client = factory.CreateClient(); |
| 28 | + |
| 29 | + (await client.GetAsync("/health/live", Ct)).StatusCode.ShouldBe(HttpStatusCode.OK); |
| 30 | + await Eventually.WaitAsync(async () => (await client.GetStringAsync("/health/ready", Ct)) == "Healthy", TimeSpan.FromSeconds(30)); |
| 31 | + |
| 32 | + var admin = factory.Services.GetRequiredService<ICatalogAdmin>(); |
| 33 | + var draft = await admin.CreateStageAsync(new NewCatalogItem("eco", "eco"), PsrEnvironment.Author, Ct); |
| 34 | + var saved = await admin.SaveStageDraftAsync("eco", draft.Version, draft.Data with |
| 35 | + { |
| 36 | + Script = Scripts.Echo, |
| 37 | + Inputs = [Scripts.Text("texto")], |
| 38 | + Outputs = [Scripts.Text("texto")], |
| 39 | + }, draft.RowVersion, PsrEnvironment.Author, Ct); |
| 40 | + await admin.PublishStageAsync("eco", saved.Version, saved.RowVersion, PsrEnvironment.Author, Ct); |
| 41 | + var handle = await factory.Services.GetRequiredService<IRunQueue>().EnqueueStageAsync("eco", new { texto = "host" }, cancellationToken: Ct); |
| 42 | + |
| 43 | + var runs = factory.Services.GetRequiredService<IRunManager>(); |
| 44 | + RunSnapshot? run = null; |
| 45 | + await Eventually.WaitAsync(async () => (run = await runs.GetAsync(handle.RunId, Ct))?.Status == RunStatus.Succeeded, TimeSpan.FromSeconds(60), |
| 46 | + () => $"run is {run?.Status}"); |
| 47 | + run!.Steps.Single().Attempts.Single().WorkerId.ShouldNotBeNullOrEmpty(); |
| 48 | + } |
| 49 | +} |
0 commit comments