Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(EventBus): middleware changed to first in first out #412

Merged
merged 2 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace Microsoft.Extensions.DependencyInjection;

public static class ServiceCollectionExtensions
Expand All @@ -28,6 +29,8 @@ public static IServiceCollection AddEventBus(

services.AddSingleton<EventBusProvider>();

services.TryAddEnumerable(new ServiceDescriptor(typeof(IMiddleware<>), typeof(TransactionMiddleware<>), ServiceLifetime.Transient));

var builder = new EventBusBuilder(services);
eventBusBuilder?.Invoke(builder);

Expand All @@ -42,7 +45,6 @@ public static IServiceCollection AddEventBus(
services.TryAddSingleton<IExceptionStrategyProvider, DefaultExceptionStrategyProvider>();
services.TryAdd(typeof(IExecutionStrategy), typeof(ExecutionStrategy), ServiceLifetime.Singleton);
services.TryAddScoped<IInitializeServiceProvider, InitializeServiceProvider>();
services.AddTransient(typeof(IMiddleware<>), typeof(TransactionMiddleware<>));
services.AddScoped(typeof(IEventBus), typeof(EventBus));
MasaApp.TrySetServiceCollection(services);
return services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ public void TestUseMiddleware2()
Assert.AreEqual(2, services.Count);
}

[TestMethod]
public async Task TestMiddlewareByOrderOfExecutionAsync()
{
var services = new ServiceCollection();
services.AddEventBus(new[] { typeof(CustomMiddleware<>).Assembly }, eventBusBuilder
=> eventBusBuilder.UseMiddleware(new[] { typeof(CustomMiddleware<>), typeof(Custom2Middleware<>) }));
var serviceProvider = services.BuildServiceProvider();

var middlewares = serviceProvider.GetService<IEnumerable<IMiddleware<MiddlewareEvent>>>();
Assert.IsNotNull(middlewares);
Assert.AreEqual(3, middlewares.Count());

var eventBus = serviceProvider.GetRequiredService<IEventBus>();
var @event = new MiddlewareEvent();
await eventBus.PublishAsync(@event);

Assert.AreEqual(3, @event.Results.Count);
Assert.AreEqual(nameof(CustomMiddleware<MiddlewareEvent>), @event.Results[0]);
Assert.AreEqual(nameof(Custom2Middleware<MiddlewareEvent>), @event.Results[1]);
Assert.AreEqual(nameof(EventHandlers.MiddlewareEventHandler), @event.Results[2]);
}

public class RequestMiddleware<TEvent> : Middleware<TEvent> where TEvent : IEvent
{
private readonly ILogger<RequestMiddleware<TEvent>>? _logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Dispatcher.Events.Tests.EventHandlers;

public class MiddlewareEventHandler
{
[EventHandler]
public void Handle(MiddlewareEvent @event)
{
@event.Results.Add(nameof(MiddlewareEventHandler));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Dispatcher.Events.Tests.Events;

/// <summary>
/// Events for testing middleware execution order
/// </summary>
public record MiddlewareEvent : Event
{
/// <summary>
/// Class name used to store execution middleware and Handler
/// </summary>
public List<string> Results { get; set; } = new();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Dispatcher.Events.Tests.Middleware;

public class Custom2Middleware<TEvent> : Middleware<TEvent> where TEvent : IEvent
{
public override Task HandleAsync(TEvent @event, EventHandlerDelegate next)
{
if (@event is MiddlewareEvent middlewareEvent)
{
middlewareEvent.Results.Add(nameof(Custom2Middleware<TEvent>));
}
return next();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Dispatcher.Events.Tests.Middleware;

public class CustomMiddleware<TEvent> : Middleware<TEvent> where TEvent : IEvent
{
public override Task HandleAsync(TEvent @event, EventHandlerDelegate next)
{
if (@event is MiddlewareEvent middlewareEvent)
{
middlewareEvent.Results.Add(nameof(CustomMiddleware<TEvent>));
}
return next();
}
}