Skip to content

Commit 6b3e989

Browse files
authored
refactor(EventBus): middleware changed to first in first out (#412)
* refactor(EventBus): middleware changed to first in first out * test: Complementary unit tests
1 parent 651b5cd commit 6b3e989

File tree

6 files changed

+85
-1
lines changed

6 files changed

+85
-1
lines changed

src/Contrib/Dispatcher/Masa.Contrib.Dispatcher.Events/Extensions/ServiceCollectionExtensions.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

44
// ReSharper disable once CheckNamespace
5+
56
namespace Microsoft.Extensions.DependencyInjection;
67

78
public static class ServiceCollectionExtensions
@@ -28,6 +29,8 @@ public static IServiceCollection AddEventBus(
2829

2930
services.AddSingleton<EventBusProvider>();
3031

32+
services.TryAddEnumerable(new ServiceDescriptor(typeof(IMiddleware<>), typeof(TransactionMiddleware<>), ServiceLifetime.Transient));
33+
3134
var builder = new EventBusBuilder(services);
3235
eventBusBuilder?.Invoke(builder);
3336

@@ -42,7 +45,6 @@ public static IServiceCollection AddEventBus(
4245
services.TryAddSingleton<IExceptionStrategyProvider, DefaultExceptionStrategyProvider>();
4346
services.TryAdd(typeof(IExecutionStrategy), typeof(ExecutionStrategy), ServiceLifetime.Singleton);
4447
services.TryAddScoped<IInitializeServiceProvider, InitializeServiceProvider>();
45-
services.AddTransient(typeof(IMiddleware<>), typeof(TransactionMiddleware<>));
4648
services.AddScoped(typeof(IEventBus), typeof(EventBus));
4749
MasaApp.TrySetServiceCollection(services);
4850
return services;

src/Contrib/Dispatcher/Tests/Masa.Contrib.Dispatcher.Events.Tests/EventBusBuilderTest.cs

+22
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ public void TestUseMiddleware2()
2626
Assert.AreEqual(2, services.Count);
2727
}
2828

29+
[TestMethod]
30+
public async Task TestMiddlewareByOrderOfExecutionAsync()
31+
{
32+
var services = new ServiceCollection();
33+
services.AddEventBus(new[] { typeof(CustomMiddleware<>).Assembly }, eventBusBuilder
34+
=> eventBusBuilder.UseMiddleware(new[] { typeof(CustomMiddleware<>), typeof(Custom2Middleware<>) }));
35+
var serviceProvider = services.BuildServiceProvider();
36+
37+
var middlewares = serviceProvider.GetService<IEnumerable<IMiddleware<MiddlewareEvent>>>();
38+
Assert.IsNotNull(middlewares);
39+
Assert.AreEqual(3, middlewares.Count());
40+
41+
var eventBus = serviceProvider.GetRequiredService<IEventBus>();
42+
var @event = new MiddlewareEvent();
43+
await eventBus.PublishAsync(@event);
44+
45+
Assert.AreEqual(3, @event.Results.Count);
46+
Assert.AreEqual(nameof(CustomMiddleware<MiddlewareEvent>), @event.Results[0]);
47+
Assert.AreEqual(nameof(Custom2Middleware<MiddlewareEvent>), @event.Results[1]);
48+
Assert.AreEqual(nameof(EventHandlers.MiddlewareEventHandler), @event.Results[2]);
49+
}
50+
2951
public class RequestMiddleware<TEvent> : Middleware<TEvent> where TEvent : IEvent
3052
{
3153
private readonly ILogger<RequestMiddleware<TEvent>>? _logger;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.Dispatcher.Events.Tests.EventHandlers;
5+
6+
public class MiddlewareEventHandler
7+
{
8+
[EventHandler]
9+
public void Handle(MiddlewareEvent @event)
10+
{
11+
@event.Results.Add(nameof(MiddlewareEventHandler));
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.Dispatcher.Events.Tests.Events;
5+
6+
/// <summary>
7+
/// Events for testing middleware execution order
8+
/// </summary>
9+
public record MiddlewareEvent : Event
10+
{
11+
/// <summary>
12+
/// Class name used to store execution middleware and Handler
13+
/// </summary>
14+
public List<string> Results { get; set; } = new();
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.Dispatcher.Events.Tests.Middleware;
5+
6+
public class Custom2Middleware<TEvent> : Middleware<TEvent> where TEvent : IEvent
7+
{
8+
public override Task HandleAsync(TEvent @event, EventHandlerDelegate next)
9+
{
10+
if (@event is MiddlewareEvent middlewareEvent)
11+
{
12+
middlewareEvent.Results.Add(nameof(Custom2Middleware<TEvent>));
13+
}
14+
return next();
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.Dispatcher.Events.Tests.Middleware;
5+
6+
public class CustomMiddleware<TEvent> : Middleware<TEvent> where TEvent : IEvent
7+
{
8+
public override Task HandleAsync(TEvent @event, EventHandlerDelegate next)
9+
{
10+
if (@event is MiddlewareEvent middlewareEvent)
11+
{
12+
middlewareEvent.Results.Add(nameof(CustomMiddleware<TEvent>));
13+
}
14+
return next();
15+
}
16+
}

0 commit comments

Comments
 (0)