-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMessagesTests.cs
More file actions
370 lines (306 loc) · 15.4 KB
/
MessagesTests.cs
File metadata and controls
370 lines (306 loc) · 15.4 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Cleipnir.ResilientFunctions.CoreRuntime.Invocation;
using Cleipnir.ResilientFunctions.CoreRuntime.Serialization;
using Cleipnir.ResilientFunctions.Domain;
using Cleipnir.ResilientFunctions.Domain.Exceptions;
using Cleipnir.ResilientFunctions.Messaging;
using Cleipnir.ResilientFunctions.Storage;
using Cleipnir.ResilientFunctions.Tests.Utils;
using Shouldly;
namespace Cleipnir.ResilientFunctions.Tests.Messaging.TestTemplates;
public abstract class MessagesTests
{
private static Settings CreateSettings(
Action<FrameworkException> unhandledExceptionHandler,
TimeSpan? messagesPullFrequency = null) =>
new(
unhandledExceptionHandler,
watchdogCheckFrequency: TimeSpan.FromMilliseconds(100),
messagesPullFrequency: messagesPullFrequency ?? TimeSpan.FromMilliseconds(100)
);
public abstract Task MessagesSunshineScenario();
protected async Task MessagesSunshineScenario(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var functionsRegistry = new FunctionsRegistry(
functionStore,
CreateSettings(unhandledExceptionCatcher.Catch)
);
var rFunc = functionsRegistry.RegisterFunc(
nameof(MessagesSunshineScenario),
inner: async Task<string> (string _, Workflow workflow)
=> await workflow.Message<string>()
);
var scheduled = await rFunc.Schedule("instanceId", "");
var messageWriter = rFunc.MessageWriters.For("instanceId".ToFlowInstance());
await messageWriter.AppendMessage("hello world");
var result = await scheduled.Completion(timeout: TimeSpan.FromSeconds(5));
result.ShouldBe("hello world");
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task QueueClientReturnsNullAfterTimeout();
protected async Task QueueClientReturnsNullAfterTimeout(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var functionsRegistry = new FunctionsRegistry(
functionStore,
CreateSettings(unhandledExceptionCatcher.Catch)
);
var rFunc = functionsRegistry.RegisterFunc(
nameof(QueueClientReturnsNullAfterTimeout),
inner: async Task<string?> (string _, Workflow workflow)
=> await workflow.Message<string>(TimeSpan.FromMilliseconds(100))
);
var scheduled = await rFunc.Schedule("instanceId", "");
var result = await scheduled.Completion(timeout: TimeSpan.FromSeconds(5));
result.ShouldBeNull();
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task MessagesFirstOfTypesReturnsNoneForFirstOfTypesOnTimeout();
protected async Task MessagesFirstOfTypesReturnsNoneForFirstOfTypesOnTimeout(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var functionsRegistry = new FunctionsRegistry(
functionStore,
CreateSettings(unhandledExceptionCatcher.Catch)
);
var rFunc = functionsRegistry.RegisterFunc(
nameof(MessagesFirstOfTypesReturnsNoneForFirstOfTypesOnTimeout),
inner: async Task<string> (string _, Workflow workflow) =>
{
var message = await workflow.Message<object>(
filter: m => m is string or int,
waitFor: TimeSpan.Zero
);
return message == null ? "NONE" : message.ToString()!;
}
);
var scheduled = await rFunc.Schedule("instanceId", "");
var result = await scheduled.Completion(timeout: TimeSpan.FromSeconds(5));
result.ShouldBe("NONE");
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task MessagesFirstOfTypesReturnsFirstForFirstOfTypesOnFirst();
protected async Task MessagesFirstOfTypesReturnsFirstForFirstOfTypesOnFirst(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var functionsRegistry = new FunctionsRegistry(
functionStore,
CreateSettings(unhandledExceptionCatcher.Catch)
);
var rFunc = functionsRegistry.RegisterFunc(
nameof(MessagesFirstOfTypesReturnsFirstForFirstOfTypesOnFirst),
inner: async Task<string> (string _, Workflow workflow) =>
{
var message = await workflow.Message<object>(filter: m => m is string or int);
return message.ToString()!;
}
);
var scheduled = await rFunc.Schedule("instanceId", "");
var messageWriter = rFunc.MessageWriters.For("instanceId".ToFlowInstance());
await messageWriter.AppendMessage("Hello");
var result = await scheduled.Completion(timeout: TimeSpan.FromSeconds(5));
result.ShouldBe("Hello");
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task MessagesFirstOfTypesReturnsSecondForFirstOfTypesOnSecond();
protected async Task MessagesFirstOfTypesReturnsSecondForFirstOfTypesOnSecond(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var functionsRegistry = new FunctionsRegistry(
functionStore,
CreateSettings(unhandledExceptionCatcher.Catch)
);
var rFunc = functionsRegistry.RegisterFunc(
nameof(MessagesFirstOfTypesReturnsSecondForFirstOfTypesOnSecond),
inner: async Task<string> (string _, Workflow workflow)
=> await workflow.Message<string>()
);
var scheduled = await rFunc.Schedule("instanceId", "");
var messageWriter = rFunc.MessageWriters.For("instanceId".ToFlowInstance());
await messageWriter.AppendMessage("1");
var result = await scheduled.Completion(timeout: TimeSpan.FromSeconds(5));
result.ShouldBe("1");
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task SecondEventWithExistingIdempotencyKeyIsIgnored();
protected async Task SecondEventWithExistingIdempotencyKeyIsIgnored(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var functionsRegistry = new FunctionsRegistry(
functionStore,
CreateSettings(unhandledExceptionCatcher.Catch)
);
var rFunc = functionsRegistry.RegisterFunc(
nameof(SecondEventWithExistingIdempotencyKeyIsIgnored),
inner: async Task<Tuple<string, string>> (string _, Workflow workflow) =>
{
var message1 = await workflow.Message<string>();
var message2 = await workflow.Message<string>();
return Tuple.Create(message1, message2);
}
);
var scheduled = await rFunc.Schedule("instanceId", "");
var messageWriter = rFunc.MessageWriters.For("instanceId".ToFlowInstance());
await messageWriter.AppendMessage("hello world", idempotencyKey: "1");
await messageWriter.AppendMessage("hello world", idempotencyKey: "1");
await messageWriter.AppendMessage("hello universe");
var result = await scheduled.Completion(timeout: TimeSpan.FromSeconds(5));
result.Item1.ShouldBe("hello world");
result.Item2.ShouldBe("hello universe");
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task QueueClientCanPullMultipleMessages();
protected async Task QueueClientCanPullMultipleMessages(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var functionsRegistry = new FunctionsRegistry(
functionStore,
CreateSettings(unhandledExceptionCatcher.Catch)
);
var rFunc = functionsRegistry.RegisterFunc(
nameof(QueueClientCanPullMultipleMessages),
inner: async Task<string> (string _, Workflow workflow) =>
{
var message1 = await workflow.Message<string>();
var message2 = await workflow.Message<string>();
return $"{message1},{message2}";
}
);
var scheduled = await rFunc.Schedule("instanceId", "");
var messageWriter = rFunc.MessageWriters.For("instanceId".ToFlowInstance());
await messageWriter.AppendMessage("hello world", "1");
await messageWriter.AppendMessage("hello world", "1");
await messageWriter.AppendMessage("hello universe");
var result = await scheduled.Completion(TimeSpan.FromSeconds(5));
result.ShouldBe("hello world,hello universe");
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task BatchedMessagesIsDeliveredToAwaitingFlows();
protected async Task BatchedMessagesIsDeliveredToAwaitingFlows(Task<IFunctionStore> functionStoreTask)
{
var flowType = TestFlowId.Create().Type;
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var registry = new FunctionsRegistry(functionStore, CreateSettings(unhandledExceptionCatcher.Catch));
var registration = registry.RegisterParamless(
flowType,
async Task (workflow) =>
{
await workflow.Message<string>();
}
);
await registration.Schedule("Instance#1");
await registration.Schedule("Instance#2");
await registration.SendMessages(
[
new BatchedMessage("Instance#1", "hallo world", IdempotencyKey: "1"),
new BatchedMessage("Instance#2", "hallo world", IdempotencyKey: "1")
]
);
var controlPanel1 = await registration.ControlPanel("Instance#1").ShouldNotBeNullAsync();
var controlPanel2 = await registration.ControlPanel("Instance#2").ShouldNotBeNullAsync();
await controlPanel1.WaitForCompletion(allowPostponeAndSuspended: true, maxWait: TimeSpan.FromSeconds(2));
await controlPanel2.WaitForCompletion(allowPostponeAndSuspended: true, maxWait: TimeSpan.FromSeconds(2));
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task MultipleMessagesCanBeAppendedOneAfterTheOther();
protected async Task MultipleMessagesCanBeAppendedOneAfterTheOther(Task<IFunctionStore> functionStoreTask)
{
var flowType = TestFlowId.Create().Type;
var functionStore = await functionStoreTask;
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var registry = new FunctionsRegistry(functionStore, CreateSettings(unhandledExceptionCatcher.Catch));
var messages = new List<string>();
var registration = registry.RegisterParamless(
flowType,
async Task (workflow) =>
{
while (true)
{
var message = await workflow.Message<object>();
if (message is string s)
await workflow.Effect.Capture(() => messages.Add(s));
else
return;
}
});
var instanceId = "Instance#1";
await registration.SendMessage(instanceId, "Hallo");
await registration.SendMessage(instanceId, "World");
await registration.SendMessage(instanceId, "And");
await registration.SendMessage(instanceId, "Universe");
await registration.SendMessage(instanceId, new object());
var cp = await registration.ControlPanel(instanceId).ShouldNotBeNullAsync();
await cp.WaitForCompletion(allowPostponeAndSuspended: true);
messages.Count.ShouldBe(4);
messages[0].ShouldBe("Hallo");
messages[1].ShouldBe("World");
messages[2].ShouldBe("And");
messages[3].ShouldBe("Universe");
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
private record Ping(int Number);
private record Pong(int Number);
public abstract Task PingPongMessagesCanBeExchangedMultipleTimes();
protected async Task PingPongMessagesCanBeExchangedMultipleTimes(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
functionStore = functionStore.WithPrefix("pingpong" + Guid.NewGuid().ToString("N"));
await functionStore.Initialize();
var unhandledExceptionCatcher = new UnhandledExceptionCatcher();
using var registry = new FunctionsRegistry(functionStore, CreateSettings(unhandledExceptionCatcher.Catch, messagesPullFrequency: TimeSpan.FromMilliseconds(10)));
ParamlessRegistration pongRegistration = null!;
ParamlessRegistration pingRegistration = null!;
pingRegistration = registry.RegisterParamless(
"PingFlow",
async Task (workflow) =>
{
for (var i = 0; i < 10; i++)
{
await pongRegistration.SendMessage("Pong", new Ping(i), idempotencyKey: $"Pong{i}");
await workflow.Message<Pong>(filter: pong => pong.Number == i);
}
});
pongRegistration = registry.RegisterParamless(
"PongFlow",
async Task (workflow) =>
{
for (var i = 0; i < 10; i++)
{
await workflow.Message<Ping>(filter: ping => ping.Number == i);
await pingRegistration.SendMessage("Ping", new Pong(i), idempotencyKey: $"Ping{i}");
}
});
await pongRegistration.Schedule("Pong");
await pingRegistration.Schedule("Ping");
var pongCp = await pongRegistration.ControlPanel("Pong").ShouldNotBeNullAsync();
var pingCp = await pingRegistration.ControlPanel("Ping").ShouldNotBeNullAsync();
await pongCp.WaitForCompletion(allowPostponeAndSuspended: true);
await pingCp.WaitForCompletion(allowPostponeAndSuspended: true);
unhandledExceptionCatcher.ShouldNotHaveExceptions();
}
public abstract Task SenderIsPersistedAndCanBeFetched();
protected async Task SenderIsPersistedAndCanBeFetched(Task<IFunctionStore> functionStoreTask)
{
var functionStore = await functionStoreTask;
var storedType = new StoredType(1);
var storedId = StoredId.Create(storedType, "instanceId");
var serializer = DefaultSerializer.Instance;
var messageStore = functionStore.MessageStore;
var messageWriter = new MessageWriter(storedId, messageStore, serializer);
await messageWriter.AppendMessage("hello world", idempotencyKey: "key1", sender: "TestSender");
var messages = await messageStore.GetMessages(storedId);
messages.Count.ShouldBe(1);
messages[0].Sender.ShouldBe("TestSender");
}
}