-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathCompositionHandlerTests.cs
More file actions
127 lines (105 loc) · 4.62 KB
/
CompositionHandlerTests.cs
File metadata and controls
127 lines (105 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using Eventuous.Subscriptions;
using Eventuous.Subscriptions.Context;
using Eventuous.Subscriptions.Filters;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging.Abstractions;
using Shouldly;
#pragma warning disable CS9113 // Parameter is unread.
namespace Eventuous.Tests.Subscriptions;
public class CompositionHandlerTests {
TestServer _server = null!;
IHost _host = null!;
[Before(Test)]
public async Task Setup() {
_host = new HostBuilder()
.ConfigureWebHost(webHostBuilder => webHostBuilder
.UseTestServer()
.UseStartup<Startup>()
)
.Build();
await _host.StartAsync();
_server = _host.GetTestServer();
}
[After(Test)]
public async Task Teardown() {
_server.Dispose();
await _host.StopAsync();
_host.Dispose();
}
[Test]
public async Task ShouldResolveCompositionHandlerWithFactory() {
// This test validates that AddCompositionEventHandler correctly registers
// handlers when using a factory function
var handler = _server.Services.GetRequiredKeyedService<TestHandler>("sub-with-factory");
await Assert.That(handler).IsNotNull();
await Assert.That(handler.Dependency.Value).IsEqualTo("test-value");
}
[Test]
public async Task ShouldHandleEventWithCompositionHandler() {
var logger = _server.Services.GetRequiredService<TestHandlerLogger>();
var subs = _server.Services.GetServices<TestSub>().ToArray();
var sub = subs.FirstOrDefault(x => x.SubscriptionId == "sub-with-factory");
sub.ShouldNotBeNull();
var ctx = new MessageConsumeContext(
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
0,
0,
0,
0,
DateTime.UtcNow,
new TestEvent(),
new(),
sub.SubscriptionId,
default
) { LogContext = new(sub.SubscriptionId, NullLoggerFactory.Instance) };
await sub.Pipe.Send(ctx);
var handled = logger.Records.Where(x => x.Context.SubscriptionId == sub.SubscriptionId).ToArray();
await Assert.That(handled.Length).IsEqualTo(1);
var handledMessage = handled[0];
await Assert.That(handledMessage.HandlerType).IsEqualTo(typeof(CompositionWrapper));
await Assert.That(handledMessage.Context.MessageId).IsEqualTo(ctx.MessageId);
}
class Startup {
public static void ConfigureServices(IServiceCollection services) {
services.AddSingleton(new TestHandlerLogger());
services.AddSingleton<TestDependency>();
// Test the AddCompositionEventHandler with a factory function
services.AddSubscription<TestSub, TestOptions>(
"sub-with-factory",
builder => builder.AddCompositionEventHandler<TestHandler, CompositionWrapper>(
sp => new(sp.GetRequiredService<TestDependency>(), sp.GetRequiredService<TestHandlerLogger>()),
(handler, sp) => new(handler, sp.GetRequiredService<TestHandlerLogger>())
)
);
}
public void Configure(IApplicationBuilder app) { }
}
record TestOptions : SubscriptionOptions;
class TestSub(TestOptions options, ConsumePipe consumePipe)
: EventSubscription<TestOptions>(options, consumePipe, NullLoggerFactory.Instance, null) {
protected override ValueTask Subscribe(CancellationToken cancellationToken) => default;
protected override ValueTask Unsubscribe(CancellationToken cancellationToken) => default;
}
public class TestDependency {
public string Value => "test-value";
}
class TestHandler(TestDependency dependency, TestHandlerLogger logger) : BaseEventHandler {
public TestDependency Dependency { get; } = dependency;
public override ValueTask<EventHandlingStatus> HandleEvent(IMessageConsumeContext ctx)
=> logger.EventReceived(GetType(), ctx);
}
class CompositionWrapper(IEventHandler _, TestHandlerLogger logger) : BaseEventHandler {
public override ValueTask<EventHandlingStatus> HandleEvent(IMessageConsumeContext ctx) {
// Wrap the inner handler call - this simulates what PollyEventHandler does
return logger.EventReceived(GetType(), ctx);
}
}
record TestEvent;
}