|
| 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 | +} |
0 commit comments