-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathEventBusUnitTest.cs
More file actions
174 lines (140 loc) · 5.92 KB
/
EventBusUnitTest.cs
File metadata and controls
174 lines (140 loc) · 5.92 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
using CodeWF.EventBus.Tests.Commands;
using CodeWF.EventBus.Tests.Handlers;
using CodeWF.EventBus.Tests.Queries;
using CommandAndQueryModel.Commands;
using CommandAndQueryModel.Queries;
using CommandAndQueryModel.Services;
namespace CodeWF.EventBus.Tests
{
public class EventBusUnitTest
{
private readonly IEventBus _eventBus;
private readonly CommandAndQueryHandler _handler;
public EventBusUnitTest()
{
ProductService.Reset();
StaticHandler.TestCount = 0;
CommandAndQueryHandler.ResetTestState();
_eventBus = new EventBus();
_handler = new CommandAndQueryHandler(_eventBus, ProductService.Default);
}
[Fact]
public async Task Subscribe_WithoutSubscribe_ShouldQueryNothing()
{
var productsQuery = new ProductsQuery
{
Name = "Xiao"
};
await _eventBus.PublishAsync(productsQuery);
Assert.Null(productsQuery.Result);
}
[Fact]
public async Task Subscribe_WithSubscribe_ShouldQuerySuccess()
{
var productsQuery = new ProductsQuery
{
Name = "Xiao"
};
_eventBus.Subscribe(_handler);
await _eventBus.PublishAsync(productsQuery);
Assert.NotNull(productsQuery.Result);
Assert.Empty(productsQuery.Result);
await _eventBus.PublishAsync(new CreateProductCommand() { Name = "XiaoMi", Price = 8999 });
await _eventBus.PublishAsync(productsQuery);
Assert.NotNull(productsQuery.Result);
Assert.True(productsQuery.Result.Count > 0);
}
[Fact]
public async Task Should_SubscribeStaticHandle_Success()
{
var query = new TestQuery();
Assert.Equal(0, query.Result);
_eventBus.Subscribe<TestAddCommand>(CommandAndQueryHandler.ReceiveAddCommand);
_eventBus.Subscribe<TestQuery>(CommandAndQueryHandler.ReceiveStaticQuery);
await _eventBus.PublishAsync(new TestAddCommand());
await _eventBus.PublishAsync(query);
Assert.True(query.Result == 1);
_eventBus.Unsubscribe<TestAddCommand>(CommandAndQueryHandler.ReceiveAddCommand);
await _eventBus.PublishAsync(new TestAddCommand());
await _eventBus.PublishAsync(query);
Assert.True(query.Result == 1);
_eventBus.Subscribe<TestAddCommand>(CommandAndQueryHandler.ReceiveAddCommand);
await _eventBus.PublishAsync(new TestAddCommand());
await _eventBus.PublishAsync(query);
Assert.True(query.Result == 2);
}
[Fact]
public async Task Should_NotDuplicateSameStaticSubscription()
{
var query = new TestQuery();
_eventBus.Subscribe<TestAddCommand>(CommandAndQueryHandler.ReceiveAddCommand);
_eventBus.Subscribe<TestAddCommand>(CommandAndQueryHandler.ReceiveAddCommand);
_eventBus.Subscribe<TestQuery>(CommandAndQueryHandler.ReceiveStaticQuery);
await _eventBus.PublishAsync(new TestAddCommand());
var addCount = await _eventBus.QueryAsync(query);
Assert.Equal(1, addCount);
}
[Fact]
public async Task Should_AutoSubscribeStaticHandle_Success()
{
var query = new TestQuery();
Assert.Equal(0, query.Result);
_eventBus.Subscribe<StaticHandler>();
await _eventBus.PublishAsync(new TestAddCommand());
await _eventBus.PublishAsync(query);
Assert.True(query.Result == 1);
_eventBus.Unsubscribe<StaticHandler>();
await _eventBus.PublishAsync(new TestAddCommand());
await _eventBus.PublishAsync(query);
Assert.True(query.Result == 1);
_eventBus.Subscribe<StaticHandler>();
await _eventBus.PublishAsync(new TestAddCommand());
await _eventBus.PublishAsync(query);
Assert.True(query.Result == 2);
}
[Fact]
public async Task Should_UnsubscribeStaticHandle_Success()
{
var query = new TestQuery();
var addCount = 0;
Assert.Equal(0, query.Result);
_eventBus.Subscribe<StaticHandler>();
_eventBus.Subscribe<StaticHandler2>();
await _eventBus.PublishAsync(new TestAddCommand());
addCount = await _eventBus.QueryAsync(query);
Assert.True(addCount == 2);
_eventBus.Unsubscribe<StaticHandler>();
await _eventBus.PublishAsync(new TestAddCommand());
addCount = await _eventBus.QueryAsync(query);
Assert.True(query.Result == 3);
_eventBus.Unsubscribe<StaticHandler2>();
await _eventBus.PublishAsync(new TestAddCommand());
addCount = await _eventBus.QueryAsync(query);
Assert.True(addCount == 3);
_eventBus.Subscribe<StaticHandler2>();
await _eventBus.PublishAsync(new TestAddCommand());
addCount = await _eventBus.QueryAsync(query);
Assert.True(addCount == 4);
}
[Fact]
public async Task Publish_WithAutoHandlersWithoutServiceResolver_ShouldThrowHelpfulException()
{
var eventBus = new EventBus();
eventBus.Subscribe(new[] { typeof(AutoResolverHandler).Assembly });
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() =>
eventBus.PublishAsync(new AutoResolverCommand()));
Assert.Contains("RegisterServiceHandlerAction", exception.Message);
}
[Event]
private sealed class AutoResolverHandler
{
[EventHandler]
private void Handle(AutoResolverCommand command)
{
}
}
private sealed class AutoResolverCommand : Command
{
}
}
}