-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFlowsWithResultTests.cs
More file actions
193 lines (156 loc) · 6.5 KB
/
Copy pathFlowsWithResultTests.cs
File metadata and controls
193 lines (156 loc) · 6.5 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
using Cleipnir.ResilientFunctions;
using Cleipnir.ResilientFunctions.Domain;
using Cleipnir.ResilientFunctions.Helpers;
using Cleipnir.ResilientFunctions.Storage;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
namespace Cleipnir.Flows.Tests.Flows;
[TestClass]
public class FlowsWithResultTests
{
[TestMethod]
public async Task SimpleFlowCompletesSuccessfully()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<SimpleFuncFlow>();
var flowStore = new InMemoryFunctionStore();
var flowsContainer = new FlowsContainer(
flowStore,
serviceCollection.BuildServiceProvider()
);
var flows = new SimpleFuncFlows(flowsContainer);
var result = await flows.Run("someInstanceId", "someParameter");
result.ShouldBe(1);
SimpleFuncFlow.InstanceId.ShouldBe("someInstanceId");
SimpleFuncFlow.ExecutedWithParameter.ShouldBe("someParameter");
var controlPanel = await flows.ControlPanel(instanceId: "someInstanceId");
controlPanel.ShouldNotBeNull();
controlPanel.Status.ShouldBe(Status.Succeeded);
controlPanel.Result.ShouldBe(1);
}
private class SimpleFuncFlows : Flows<SimpleFuncFlow, string, int>
{
public SimpleFuncFlows(FlowsContainer flowsContainer)
: base(nameof(SimpleFuncFlow), flowsContainer, options: null) { }
}
public class SimpleFuncFlow : Flow<string, int>
{
public static string? ExecutedWithParameter { get; set; }
public static string? InstanceId { get; set; }
public override async Task<int> Run(string param)
{
await Task.Delay(1);
ExecutedWithParameter = param;
InstanceId = Workflow.FlowId.Instance.ToString();
return 1;
}
}
[TestMethod]
public async Task CompletionOfScheduledFlowCanBeAwaited()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<SimpledDelayedFlow>();
var flowStore = new InMemoryFunctionStore();
var flowsContainer = new FlowsContainer(
flowStore,
serviceCollection.BuildServiceProvider(),
new Settings(watchdogCheckFrequency: TimeSpan.FromMilliseconds(100))
);
var flows = new SimpledDelayedFlows(flowsContainer);
var scheduled = await flows.Schedule("someInstanceId", "someParameter");
// Flow uses non-suspending delay, so it completes directly
var result = await scheduled.Completion(timeout: TimeSpan.FromSeconds(5));
result.ShouldBe(1);
}
private class SimpledDelayedFlows : Flows<SimpledDelayedFlow, string, int>
{
public SimpledDelayedFlows(FlowsContainer flowsContainer)
: base(nameof(SimpledDelayedFlow), flowsContainer, options: null) { }
}
public class SimpledDelayedFlow : Flow<string, int>
{
public override async Task<int> Run(string param)
{
// Use non-suspending delay to avoid watchdog dependency
await Delay(TimeSpan.FromMilliseconds(100), suspend: false);
return 1;
}
}
[TestMethod]
public async Task EventDrivenFlowCompletesSuccessfully()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<MessageDrivenFuncFlow>();
var flowStore = new InMemoryFunctionStore();
var flowsContainer = new FlowsContainer(
flowStore,
serviceCollection.BuildServiceProvider(),
new Settings(watchdogCheckFrequency: TimeSpan.FromMilliseconds(100))
);
var flows = new MessageDrivenFuncFlows(flowsContainer);
await flows.Schedule("someInstanceId", "someParameter");
var controlPanel = await flows.ControlPanel(instanceId: "someInstanceId");
controlPanel.ShouldNotBeNull();
await controlPanel.BusyWaitUntil(c => c.Status == Status.Suspended);
var messageWriter = flows.MessageWriter("someInstanceId");
await messageWriter.AppendMessage(new IntWrapper(2));
await controlPanel.WaitForCompletion(allowPostponeAndSuspended: true);
await controlPanel.Refresh();
controlPanel.ShouldNotBeNull();
controlPanel.Status.ShouldBe(Status.Succeeded);
controlPanel.Result.ShouldBe(2);
}
private class MessageDrivenFuncFlows : Flows<MessageDrivenFuncFlow, string, int>
{
public MessageDrivenFuncFlows(FlowsContainer flowsContainer)
: base(nameof(MessageDrivenFuncFlow), flowsContainer, options: null) { }
}
public class MessageDrivenFuncFlow : Flow<string, int>
{
public override async Task<int> Run(string param)
{
var next = await Message<IntWrapper>();
return next.Value;
}
}
public record IntWrapper(int Value);
[TestMethod]
public async Task FailingFlowCompletesWithError()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<FailingFuncFlow>();
var flowStore = new InMemoryFunctionStore();
var flowsContainer = new FlowsContainer(
flowStore,
serviceCollection.BuildServiceProvider()
);
var flows = new FailingFuncFlows(flowsContainer);
FailingFuncFlow.ShouldThrow = true;
await Should.ThrowAsync<FatalWorkflowException<ArgumentException>>(() =>
flows.Run("someInstanceId", "someParameter")
);
var controlPanel = await flows.ControlPanel(instanceId: "someInstanceId");
controlPanel.ShouldNotBeNull();
controlPanel.Status.ShouldBe(Status.Failed);
FailingFuncFlow.ShouldThrow = false;
await controlPanel.ScheduleRestart().Completion();
await controlPanel.Refresh();
controlPanel.Status.ShouldBe(Status.Succeeded);
controlPanel.Result.ShouldBe("someParameter");
}
private class FailingFuncFlows : Flows<FailingFuncFlow, string, string>
{
public FailingFuncFlows(FlowsContainer flowsContainer)
: base(nameof(FailingFuncFlow), flowsContainer, options: null) { }
}
public class FailingFuncFlow : Flow<string, string>
{
public static bool ShouldThrow = true;
public override Task<string> Run(string param)
{
if (ShouldThrow)
return Task.FromException<string>(new ArgumentException(param));
return param.ToTask();
}
}
}