-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPublishTests.cs
More file actions
207 lines (168 loc) · 5.85 KB
/
Copy pathPublishTests.cs
File metadata and controls
207 lines (168 loc) · 5.85 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using FluentAssertions;
using Mediator.Tests.TestData;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Mediator.Tests;
/// <summary>
/// Tests for IMediator.Publish functionality.
/// </summary>
public class PublishTests : IDisposable
{
private readonly ServiceProvider _serviceProvider;
private readonly IMediator _mediator;
public PublishTests()
{
var services = new ServiceCollection();
services.AddMediator(config =>
{
config.AddAssembly(typeof(PublishTests).Assembly);
});
_serviceProvider = services.BuildServiceProvider();
_mediator = _serviceProvider.GetRequiredService<IMediator>();
// Reset static state
UserCreatedHandler1.Reset();
UserCreatedHandler2.Reset();
OrderPlacedHandler.Reset();
CreateOrderEventHandler.Reset();
}
public void Dispose()
{
_serviceProvider.Dispose();
}
[Fact]
public async Task Publish_NotifiesAllHandlers()
{
// Arrange
var notification = new UserCreatedNotification(1, "John");
UserCreatedHandler1.Reset();
UserCreatedHandler2.Reset();
// Act
await _mediator.Publish(notification);
// Assert
UserCreatedHandler1.HandleCount.Should().Be(1);
UserCreatedHandler2.HandleCount.Should().Be(1);
}
[Fact]
public async Task Publish_HandlersReceiveCorrectNotification()
{
// Arrange
var notification = new UserCreatedNotification(42, "Alice");
UserCreatedHandler1.Reset();
UserCreatedHandler2.Reset();
// Act
await _mediator.Publish(notification);
// Assert
UserCreatedHandler1.ReceivedNotifications.Should().ContainSingle()
.Which.Should().BeEquivalentTo(notification);
UserCreatedHandler2.ReceivedNotifications.Should().ContainSingle()
.Which.Should().BeEquivalentTo(notification);
}
[Fact]
public async Task Publish_WithSingleHandler_WorksCorrectly()
{
// Arrange
var notification = new OrderPlacedNotification(123);
OrderPlacedHandler.Reset();
// Act
await _mediator.Publish(notification);
// Assert
OrderPlacedHandler.HandleCount.Should().Be(1);
}
[Fact]
public async Task Publish_WithNullNotification_ThrowsArgumentNullException()
{
// Act
Func<Task> act = async () => await _mediator.Publish<UserCreatedNotification>(null!);
// Assert
await act.Should().ThrowAsync<ArgumentNullException>();
}
[Fact]
public async Task Publish_MultipleNotifications_AllHandled()
{
// Arrange
UserCreatedHandler1.Reset();
UserCreatedHandler2.Reset();
// Act
for (int i = 0; i < 5; i++)
{
await _mediator.Publish(new UserCreatedNotification(i, $"User{i}"));
}
// Assert
UserCreatedHandler1.HandleCount.Should().Be(5);
UserCreatedHandler2.HandleCount.Should().Be(5);
UserCreatedHandler1.ReceivedNotifications.Should().HaveCount(5);
}
[Fact]
public async Task Publish_ObjectOverload_WorksCorrectly()
{
// Arrange
object notification = new OrderPlacedNotification(999);
OrderPlacedHandler.Reset();
// Act
await _mediator.Publish(notification);
// Assert
OrderPlacedHandler.HandleCount.Should().Be(1);
}
[Fact]
public async Task Publish_ConcurrentNotifications_AllHandled()
{
// Arrange
UserCreatedHandler1.Reset();
UserCreatedHandler2.Reset();
var notifications = Enumerable.Range(1, 50)
.Select(i => new UserCreatedNotification(i, $"User{i}"))
.ToList();
// Act
var tasks = notifications.Select(n => _mediator.Publish(n));
await Task.WhenAll(tasks);
// Assert
UserCreatedHandler1.HandleCount.Should().Be(50);
UserCreatedHandler2.HandleCount.Should().Be(50);
}
[Fact]
public async Task Publish_WithCancellationToken_PassesToHandlers()
{
// Arrange
var notification = new UserCreatedNotification(1, "Test");
using var cts = new CancellationTokenSource();
UserCreatedHandler1.Reset();
// Act
await _mediator.Publish(notification, cts.Token);
// Assert
UserCreatedHandler1.HandleCount.Should().Be(1);
}
[Fact]
public async Task Publish_WithNoHandlers_CompletesSuccessfully()
{
// Arrange - OrphanNotification has no registered handlers
var notification = new OrphanNotification("Test");
// Act
Func<Task> act = async () => await _mediator.Publish(notification);
// Assert - Should complete without throwing
await act.Should().NotThrowAsync();
}
[Fact]
public async Task Publish_WithInheritance_InvokesDerivedTypeHandler()
{
// Arrange - CreateOrderEvent inherits from DomainEventBase
DomainEventBase domainEvent = new CreateOrderEvent(123, "John Doe");
CreateOrderEventHandler.Reset();
// Act - Publish using base class reference
await _mediator.Publish(domainEvent);
// Assert - Handler for CreateOrderEvent should be invoked, not DomainEventBase
CreateOrderEventHandler.HandleCount.Should().Be(1);
CreateOrderEventHandler.ReceivedEvents.Should().ContainSingle()
.Which.Should().BeEquivalentTo(new CreateOrderEvent(123, "John Doe"));
}
[Fact]
public async Task Publish_WithInheritance_ObjectOverload_InvokesDerivedTypeHandler()
{
// Arrange
object domainEvent = new CreateOrderEvent(456, "Jane Doe");
CreateOrderEventHandler.Reset();
// Act
await _mediator.Publish(domainEvent);
// Assert
CreateOrderEventHandler.HandleCount.Should().Be(1);
}
}