Skip to content

Commit b5f43ca

Browse files
net0wellclaude
andcommitted
test: fila e worker com PostgreSQL 16 e RabbitMQ 3.13 via Testcontainers (specs 005, 006)
- RabbitMQ.Tests: AC-1 a AC-12 da spec 005, eventos de execução síncrona (D-105) e ponte por instância (D-110). - Worker.Tests: AC-1 a AC-17 da spec 006, reprocesso assíncrono (spec 003 AC-10), editor de agendamentos (D-101) e aviso de entrada obrigatória nova (D-77); worker morto simulado em processo (D-109). - Fixture html-para-email (CS-2, mapeamento §13.2); o teste roda só no Linux (D-106). - ExecuteStep sem runId/stepRunId/attempt vai para a dead-letter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 804edd3 commit b5f43ca

21 files changed

Lines changed: 2038 additions & 1 deletion

File tree

‎src/PythonScriptRunner.Worker/StepWorker.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ internal async Task<DeliveryOutcome> HandleAsync(PsrDelivery delivery, Cancellat
8484
}
8585

8686
command = JsonSerializer.Deserialize<ExecuteStepCommand>(delivery.Body.Span, PsrMessageJson.Options) ?? throw new JsonException("The body is null.");
87+
if (command.RunId.Value == Guid.Empty || command.StepRunId.Value == Guid.Empty || command.Attempt < 1)
88+
{
89+
throw new JsonException("runId, stepRunId and attempt (>= 1) are required.");
90+
}
8791
}
8892
catch (JsonException ex)
8993
{

‎tests/PythonScriptRunner.EntityFrameworkCore.Tests/SchemaTests.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.EntityFrameworkCore;
12
using Microsoft.Extensions.Diagnostics.HealthChecks;
23
using Microsoft.Extensions.Hosting;
34
using Npgsql;
@@ -46,7 +47,8 @@ public async Task Spec004_AC01_MigrateCreatesSchemaPartitionsAndIsRepeatable()
4647

4748
await ExecuteAsync(scripted.ConnectionString, script);
4849
(await scripted.ScalarAsync<string>(Signature)).ShouldBe(signature);
49-
(await scripted.ScalarAsync<long>("SELECT count(*) FROM psr.__ef_migrations_history")).ShouldBe(1);
50+
await using var db = await migrated.DbAsync();
51+
(await scripted.ScalarAsync<long>("SELECT count(*) FROM psr.__ef_migrations_history")).ShouldBe(db.Database.GetMigrations().Count());
5052
}
5153

5254
[Fact]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Text;
2+
using RabbitMQ.Client;
3+
4+
namespace PythonScriptRunner.Testing;
5+
6+
/// <summary>Talks to the broker directly, like a system that is not .NET (or a misbehaving one) would.</summary>
7+
public sealed class RawBroker : IAsyncDisposable
8+
{
9+
private readonly IConnection _connection;
10+
private readonly IChannel _channel;
11+
12+
private RawBroker(IConnection connection, IChannel channel)
13+
{
14+
_connection = connection;
15+
_channel = channel;
16+
}
17+
18+
public static async Task<RawBroker> ConnectAsync(string uri)
19+
{
20+
var connection = await new ConnectionFactory { Uri = new Uri(uri) }.CreateConnectionAsync(TestContext.Current.CancellationToken);
21+
var channel = await connection.CreateChannelAsync(
22+
new CreateChannelOptions(publisherConfirmationsEnabled: true, publisherConfirmationTrackingEnabled: true), TestContext.Current.CancellationToken);
23+
return new RawBroker(connection, channel);
24+
}
25+
26+
public async Task PublishAsync(string exchange, string routingKey, string body, string? type, string? messageId = null)
27+
{
28+
var properties = new BasicProperties
29+
{
30+
MessageId = messageId ?? Guid.NewGuid().ToString(),
31+
Type = type,
32+
ContentType = "application/json",
33+
DeliveryMode = DeliveryModes.Persistent,
34+
};
35+
await _channel.BasicPublishAsync(exchange, routingKey, mandatory: true, properties, Encoding.UTF8.GetBytes(body), TestContext.Current.CancellationToken);
36+
}
37+
38+
/// <summary>Reads one message of a queue (without acknowledging others).</summary>
39+
public async Task<string?> GetAsync(string queue)
40+
{
41+
var result = await _channel.BasicGetAsync(queue, autoAck: true, TestContext.Current.CancellationToken);
42+
return result is null ? null : Encoding.UTF8.GetString(result.Body.Span);
43+
}
44+
45+
public async ValueTask DisposeAsync()
46+
{
47+
await _channel.DisposeAsync();
48+
await _connection.DisposeAsync();
49+
}
50+
}
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
using System.Collections.Concurrent;
2+
using System.Text.Json.Nodes;
3+
4+
namespace PythonScriptRunner.RabbitMQ.Tests;
5+
6+
/// <summary>Spec 005: the public StartPipelineRun command and run event subscriptions (D-72, D-35, D-110).</summary>
7+
public sealed class PublicContractTests(PostgresFixture postgres, BrokerFixture broker, PythonFixture python)
8+
{
9+
private static CancellationToken Ct => TestContext.Current.CancellationToken;
10+
11+
[Fact]
12+
public async Task Spec005_AC05_StartPipelineRun_CreatesAQueueCommandRunWithTheCorrelation()
13+
{
14+
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python);
15+
var host = await env.StartHostAsync();
16+
await PublishEchoPipelineAsync(host);
17+
18+
await SendAsync(env, new JsonObject
19+
{
20+
["pipelineKey"] = "eco-publica",
21+
["inputs"] = new JsonObject { ["texto"] = "de fora" },
22+
["correlationId"] = "erp-77881",
23+
["requestedBy"] = "erp",
24+
});
25+
26+
var queued = await host.Events.WaitForAsync(RunEventTypes.RunQueued, e => e.CorrelationId == "erp-77881");
27+
var run = await host.WaitForRunAsync(queued.RunId!.Value);
28+
run.Status.ShouldBe(RunStatus.Succeeded);
29+
run.Trigger.ShouldBe(RunTrigger.QueueCommand);
30+
run.TriggeredBy.ShouldBe("queue:erp");
31+
run.CorrelationId.ShouldBe("erp-77881");
32+
var succeeded = await host.Events.WaitForAsync(RunEventTypes.RunSucceeded, e => e.RunId == run.Id);
33+
succeeded.CorrelationId.ShouldBe("erp-77881");
34+
succeeded.Outputs!["texto"]!.GetValue<string>().ShouldBe("de fora");
35+
}
36+
37+
[Fact]
38+
public async Task Spec005_AC06_InvalidStartPipelineRun_IsRejectedWithReasonsAndCreatesNoRun()
39+
{
40+
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python);
41+
var host = await env.StartHostAsync();
42+
await PublishEchoPipelineAsync(host);
43+
await PsrEnvironment.PublishStageAsync(host, "le-arquivo", "from psrunner import stage\n\n\n@stage\ndef run(inputs, ctx):\n return {}\n",
44+
[Scripts.File("arquivo")], []);
45+
await PsrEnvironment.PublishPipelineAsync(host, "com-arquivo", new PipelineDraftData
46+
{
47+
Inputs = [Scripts.File("arquivo")],
48+
Steps = [new PipelineStepDefinition("le", "le-arquivo", 1)],
49+
});
50+
51+
await SendAsync(env, new JsonObject { ["pipelineKey"] = "nao-existe", ["correlationId"] = "c-1" });
52+
await SendAsync(env, new JsonObject { ["pipelineKey"] = "eco-publica", ["inputs"] = new JsonObject { ["texto"] = 42 }, ["correlationId"] = "c-2" });
53+
await SendAsync(env, new JsonObject { ["pipelineKey"] = "com-arquivo", ["correlationId"] = "c-3" });
54+
55+
var notFound = await host.Events.WaitForAsync(RunEventTypes.RunRejected, e => e.CorrelationId == "c-1");
56+
var invalid = await host.Events.WaitForAsync(RunEventTypes.RunRejected, e => e.CorrelationId == "c-2");
57+
var withFile = await host.Events.WaitForAsync(RunEventTypes.RunRejected, e => e.CorrelationId == "c-3");
58+
notFound.Error!.Code.ShouldBe("PIPELINE_NOT_FOUND");
59+
notFound.RunId.ShouldBeNull();
60+
invalid.Error!.Code.ShouldBe("INPUT_INVALID");
61+
invalid.Error.Reasons!.ShouldContain(r => r.StartsWith("texto", StringComparison.Ordinal));
62+
withFile.Error!.Code.ShouldBe(PsrErrorCodes.FileInputNotAllowed);
63+
withFile.Error.Reasons!.Single().ShouldContain("arquivo");
64+
(await env.ScalarAsync<long>("SELECT count(*) FROM psr.runs")).ShouldBe(0);
65+
}
66+
67+
[Fact]
68+
public async Task Spec005_AC07_SameIdempotencyKey_CreatesOneRun()
69+
{
70+
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python);
71+
var host = await env.StartHostAsync();
72+
await PublishEchoPipelineAsync(host);
73+
var command = new JsonObject
74+
{
75+
["pipelineKey"] = "eco-publica",
76+
["inputs"] = new JsonObject { ["texto"] = "uma vez" },
77+
["idempotencyKey"] = "pedido-123",
78+
["correlationId"] = "dup",
79+
};
80+
81+
await SendAsync(env, command);
82+
await SendAsync(env, command);
83+
84+
await Eventually.WaitAsync(() => Task.FromResult(host.Events.All.Count(e => e.Type == RunEventTypes.RunQueued && e.CorrelationId == "dup") == 2),
85+
TimeSpan.FromSeconds(30));
86+
host.Events.All.Where(e => e.Type == RunEventTypes.RunQueued && e.CorrelationId == "dup").Select(e => e.RunId).Distinct().Count().ShouldBe(1);
87+
(await env.ScalarAsync<long>("SELECT count(*) FROM psr.runs")).ShouldBe(1);
88+
}
89+
90+
[Fact]
91+
public async Task StartPipelineRun_RedeliveredWithoutIdempotencyKey_IsDeduplicatedByMessageId()
92+
{
93+
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python);
94+
var host = await env.StartHostAsync();
95+
await PublishEchoPipelineAsync(host);
96+
var command = new JsonObject { ["pipelineKey"] = "eco-publica", ["inputs"] = new JsonObject { ["texto"] = "x" }, ["correlationId"] = "msg" };
97+
var messageId = Guid.NewGuid().ToString();
98+
99+
await SendAsync(env, command, messageId);
100+
await SendAsync(env, command, messageId);
101+
102+
await Eventually.WaitAsync(() => Task.FromResult(host.Events.All.Count(e => e.Type == RunEventTypes.RunQueued && e.CorrelationId == "msg") == 2),
103+
TimeSpan.FromSeconds(30));
104+
(await env.ScalarAsync<long>("SELECT count(*) FROM psr.runs")).ShouldBe(1);
105+
}
106+
107+
[Fact]
108+
public async Task Spec005_AC08_Subscriber_ReceivesOnlyTheSuccessesOfItsPipelineWithOutputs()
109+
{
110+
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python);
111+
var host = await env.StartHostAsync(s =>
112+
{
113+
s.Services = services => services.AddSingleton<SubscriptionRecorder>();
114+
s.Builder = psr => psr.AddRunEventHandler<SubscriptionRecorder>(sub => sub.Subscription("calculo").On("run.succeeded.calculo-progressao"));
115+
});
116+
await env.ImportFixtureAsync(host, "calculo-progressao");
117+
await PublishEchoPipelineAsync(host);
118+
await PsrEnvironment.PublishStageAsync(host, "falha", Scripts.AlwaysFails, [], []);
119+
var inputs = new { a1 = 1, razao = 2, n = 4, tipo = "geometrica", coeficientes = new[] { 1, 0, 1 } };
120+
121+
var runs = new[]
122+
{
123+
await host.Queue.EnqueuePipelineAsync("calculo-progressao", inputs, cancellationToken: Ct),
124+
await host.Queue.EnqueuePipelineAsync("calculo-progressao", inputs, cancellationToken: Ct),
125+
await host.Queue.EnqueuePipelineAsync("eco-publica", new { texto = "outra" }, cancellationToken: Ct),
126+
await host.Queue.EnqueueStageAsync("falha", null, cancellationToken: Ct),
127+
};
128+
foreach (var run in runs)
129+
{
130+
await host.WaitForRunAsync(run.RunId, timeout: TimeSpan.FromMinutes(3));
131+
}
132+
133+
await Task.Delay(1500, Ct);
134+
var received = host.Services.GetRequiredService<SubscriptionRecorder>().Events;
135+
received.Select(e => e.RunId!.Value.Value).ShouldBe([runs[0].RunId.Value, runs[1].RunId.Value], ignoreOrder: true);
136+
received.ShouldAllBe(e => e.Type == RunEventTypes.RunSucceeded && e.PipelineKey == "calculo-progressao");
137+
received.ShouldAllBe(e => e.Outputs!["soma"]!.GetValue<double>() == 89 && !e.OutputsTruncated);
138+
}
139+
140+
[Fact]
141+
public async Task Spec005_AC11_EventHandler_ReceivesTheConfiguredPatterns()
142+
{
143+
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python);
144+
var host = await env.StartHostAsync(s =>
145+
{
146+
s.Services = services => services.AddSingleton<SubscriptionRecorder>();
147+
s.Builder = psr => psr.AddRunEventHandler<SubscriptionRecorder>(sub => sub.Subscription("padroes").On("run.*.eco-publica", "step.succeeded.#"));
148+
});
149+
await PublishEchoPipelineAsync(host);
150+
151+
var handle = await host.Queue.EnqueuePipelineAsync("eco-publica", new { texto = "padrões" }, cancellationToken: Ct);
152+
await host.WaitForRunAsync(handle.RunId);
153+
await Task.Delay(1500, Ct);
154+
155+
host.Services.GetRequiredService<SubscriptionRecorder>().Events.Select(e => e.Type).ShouldBe(
156+
[RunEventTypes.RunQueued, RunEventTypes.RunStarted, RunEventTypes.StepSucceeded, RunEventTypes.RunSucceeded], ignoreOrder: true);
157+
}
158+
159+
[Fact]
160+
public async Task PerInstanceSubscription_EveryInstanceReceivesEveryEvent()
161+
{
162+
await using var env = await PsrEnvironment.CreateAsync(postgres, broker, python);
163+
var first = await env.StartHostAsync(s => s.Worker = false);
164+
var second = await env.StartHostAsync();
165+
await PublishEchoPipelineAsync(first);
166+
167+
var handle = await first.Queue.EnqueuePipelineAsync("eco-publica", new { texto = "todos" }, cancellationToken: Ct);
168+
169+
await first.Events.WaitForAsync(RunEventTypes.RunSucceeded, e => e.RunId == handle.RunId);
170+
await second.Events.WaitForAsync(RunEventTypes.RunSucceeded, e => e.RunId == handle.RunId);
171+
}
172+
173+
private static async Task PublishEchoPipelineAsync(PsrHost host)
174+
{
175+
await PsrEnvironment.PublishStageAsync(host, "eco", Scripts.Echo, [Scripts.Text("texto")], [Scripts.Text("texto")]);
176+
await PsrEnvironment.PublishPipelineAsync(host, "eco-publica", Scripts.SingleStep("eco", 1, [Scripts.Text("texto")]));
177+
}
178+
179+
private static async Task SendAsync(PsrEnvironment env, JsonObject command, string? messageId = null)
180+
{
181+
await using var raw = await RawBroker.ConnectAsync(env.Broker);
182+
await raw.PublishAsync(PsrTopology.PublicExchange, PsrTopology.StartPipelineRoutingKey, command.ToJsonString(), PsrMessageTypes.StartPipelineRun,
183+
messageId);
184+
}
185+
186+
/// <summary>Events of a durable subscription.</summary>
187+
public sealed class SubscriptionRecorder : IRunEventHandler
188+
{
189+
private readonly ConcurrentQueue<RunEvent> _events = new();
190+
191+
public IReadOnlyList<RunEvent> Events => [.. _events];
192+
193+
public Task HandleAsync(RunEvent runEvent, CancellationToken cancellationToken)
194+
{
195+
_events.Enqueue(runEvent);
196+
return Task.CompletedTask;
197+
}
198+
}
199+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<!-- Queued runs are executed by a worker, so the transport is tested end to end with it. -->
5+
<ProjectReference Include="..\..\src\PythonScriptRunner.Worker\PythonScriptRunner.Worker.csproj" />
6+
</ItemGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="..\PythonScriptRunner.Core.Tests\Infrastructure\PythonFixture.cs" Link="Infrastructure\PythonFixture.cs" />
10+
<Compile Include="..\PythonScriptRunner.Core.Tests\Infrastructure\RunnerHost.cs" Link="Infrastructure\RunnerHost.cs" />
11+
<Compile Include="..\PythonScriptRunner.EntityFrameworkCore.Tests\Infrastructure\PostgresFixture.cs" Link="Infrastructure\PostgresFixture.cs" />
12+
<Compile Include="..\PythonScriptRunner.Worker.Tests\Infrastructure\*.cs" Link="Infrastructure\%(Filename)%(Extension)" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<Using Include="Microsoft.Extensions.DependencyInjection" />
17+
<Using Include="PythonScriptRunner.Core.Tests.Infrastructure" />
18+
<Using Include="PythonScriptRunner.EntityFrameworkCore.Tests.Infrastructure" />
19+
<Using Include="PythonScriptRunner.Testing" />
20+
<Using Include="Shouldly" />
21+
<Using Include="Xunit" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<PackageReference Include="Microsoft.Extensions.Configuration" />
26+
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" />
27+
<PackageReference Include="Microsoft.Extensions.Diagnostics.Testing" />
28+
<PackageReference Include="Microsoft.Extensions.Hosting" />
29+
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" />
30+
<PackageReference Include="Shouldly" />
31+
<PackageReference Include="Testcontainers.PostgreSql" />
32+
<PackageReference Include="Testcontainers.RabbitMq" />
33+
<PackageReference Include="xunit.v3" />
34+
</ItemGroup>
35+
36+
</Project>

0 commit comments

Comments
 (0)