diff --git a/src/Mocha/src/Mocha/Middlewares/DispatchContext.cs b/src/Mocha/src/Mocha/Middlewares/DispatchContext.cs index 53e8719201d..d57c2bf0352 100644 --- a/src/Mocha/src/Mocha/Middlewares/DispatchContext.cs +++ b/src/Mocha/src/Mocha/Middlewares/DispatchContext.cs @@ -226,6 +226,7 @@ public void Initialize( Services = services; Endpoint = endpoint; Transport = endpoint.Transport; + Runtime = runtime; Host = runtime.Host; CancellationToken = cancellationToken; MessageType = messageType; diff --git a/src/Mocha/test/Mocha.Tests/Middlewares/MiddlewareTests.cs b/src/Mocha/test/Mocha.Tests/Middlewares/MiddlewareTests.cs index 4b5c98b3dad..adb02615c77 100644 --- a/src/Mocha/test/Mocha.Tests/Middlewares/MiddlewareTests.cs +++ b/src/Mocha/test/Mocha.Tests/Middlewares/MiddlewareTests.cs @@ -1,3 +1,4 @@ +using System.Collections.Concurrent; using Microsoft.Extensions.DependencyInjection; using Mocha.Transport.InMemory; @@ -329,6 +330,65 @@ public async Task Pipeline_Should_ProcessAllRequestsWhenConcurrent_When_Concurre Assert.All(responses, r => Assert.Equal("Shipped", r.Status)); } + [Theory] + [InlineData(MessageKind.Publish, 1)] + [InlineData(MessageKind.Send, 1)] + [InlineData(MessageKind.Request, 2)] + public async Task Initialize_Should_AssignRuntime_When_MessageIsPublishedSentOrRequested( + string kind, + int expectedDispatchCount) + { + // arrange + // the middleware runs for every outgoing message, so a request also captures its reply + var capturedRuntimes = new ConcurrentQueue(); + var services = new ServiceCollection(); + services.AddSingleton(new MessageRecorder()); + var builder = services.AddMessageBus(); + builder.AddRequestHandler(); + builder.ConfigureMessageBus(b => b.UseDispatch( + new DispatchMiddlewareConfiguration( + (_, next) => context => + { + capturedRuntimes.Enqueue(context.Runtime); + return next(context); + }, + "CaptureRuntime"))); + builder.AddInMemory(t => + { + t.DeclareQueue("events"); + t.DispatchEndpoint("events").ToQueue("events").Publish(); + t.DeclareQueue("commands"); + t.DispatchEndpoint("commands").ToQueue("commands").Send(); + }); + + await using var provider = services.BuildServiceProvider(); + var runtime = (MessagingRuntime)provider.GetRequiredService(); + await runtime.StartAsync(CancellationToken.None); + + using var scope = provider.CreateScope(); + var bus = scope.ServiceProvider.GetRequiredService(); + + // act + switch (kind) + { + case MessageKind.Publish: + await bus.PublishAsync(new OrderCreated { OrderId = "runtime" }, CancellationToken.None); + break; + + case MessageKind.Send: + await bus.SendAsync(new ProcessPayment { Amount = 42.00m }, CancellationToken.None); + break; + + case MessageKind.Request: + await bus.RequestAsync(new GetOrderStatus { OrderId = "runtime" }, CancellationToken.None); + break; + } + + // assert + Assert.Equal(expectedDispatchCount, capturedRuntimes.Count); + Assert.All(capturedRuntimes, captured => Assert.Same(runtime, captured)); + } + private static readonly TimeSpan s_timeout = TimeSpan.FromSeconds(10); private static MessagingRuntime CreateRuntime(Action configure)