-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathParamlessFlowsTests.cs
More file actions
179 lines (145 loc) · 5.8 KB
/
Copy pathParamlessFlowsTests.cs
File metadata and controls
179 lines (145 loc) · 5.8 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
using Cleipnir.ResilientFunctions;
using Cleipnir.ResilientFunctions.Domain;
using Cleipnir.ResilientFunctions.Messaging;
using Cleipnir.ResilientFunctions.Storage;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
namespace Cleipnir.Flows.Tests.Flows;
[TestClass]
public class ParamlessFlowsTests
{
[TestMethod]
public async Task SimpleFlowCompletesSuccessfully()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<SimpleParamlessFlow>();
var flowStore = new InMemoryFunctionStore();
var flowsContainer = new FlowsContainer(
flowStore,
serviceCollection.BuildServiceProvider()
);
var flows = new SimpleParamlessFlows(flowsContainer);
await flows.Run("someInstanceId");
SimpleParamlessFlow.InstanceId.ShouldBe("someInstanceId");
var controlPanel = await flows.ControlPanel(instanceId: "someInstanceId");
controlPanel.ShouldNotBeNull();
controlPanel.Status.ShouldBe(Status.Succeeded);
}
private class SimpleParamlessFlows : Flows<SimpleParamlessFlow>
{
public SimpleParamlessFlows(FlowsContainer flowsContainer)
: base(nameof(SimpleParamlessFlow), flowsContainer, options: null) { }
}
public class SimpleParamlessFlow : Flow
{
public static string? InstanceId { get; set; }
public override async Task Run()
{
await Task.Delay(1);
InstanceId = Workflow.FlowId.Instance.ToString();
}
}
[TestMethod]
public async Task EventDrivenFlowCompletesSuccessfully()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<EventDrivenParamlessFlow>();
var flowStore = new InMemoryFunctionStore();
var flowsContainer = new FlowsContainer(
flowStore,
serviceCollection.BuildServiceProvider(),
new Settings(watchdogCheckFrequency: TimeSpan.FromMilliseconds(100))
);
var flows = new EventDrivenParamlessFlows(flowsContainer);
await flows.Schedule("someInstanceId");
var controlPanel = await flows.ControlPanel(instanceId: "someInstanceId");
controlPanel.ShouldNotBeNull();
await controlPanel.BusyWaitUntil(c => c.Status == Status.Suspended);
var eventSourceWriter = flows.MessageWriter("someInstanceId");
await eventSourceWriter.AppendMessage(new StringMessage("hello"));
await controlPanel.WaitForCompletion(allowPostponeAndSuspended: true);
await controlPanel.Refresh();
controlPanel.ShouldNotBeNull();
controlPanel.Status.ShouldBe(Status.Succeeded);
}
private class EventDrivenParamlessFlows : Flows<EventDrivenParamlessFlow>
{
public EventDrivenParamlessFlows(FlowsContainer flowsContainer)
: base(nameof(EventDrivenParamlessFlow), flowsContainer, options: null) { }
}
public class EventDrivenParamlessFlow : Flow
{
public override async Task Run()
{
await Message<StringMessage>();
}
}
public record StringMessage(string Value);
[TestMethod]
public async Task FailingFlowCompletesWithError()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<FailingParamlessFlow>();
var flowStore = new InMemoryFunctionStore();
var flowsContainer = new FlowsContainer(
flowStore,
serviceCollection.BuildServiceProvider()
);
var flows = new FailingParamlessFlows(flowsContainer);
FailingParamlessFlow.ShouldThrow = true;
await Should.ThrowAsync<FatalWorkflowException<TimeoutException>>(() =>
flows.Run("someInstanceId")
);
var controlPanel = await flows.ControlPanel(instanceId: "someInstanceId");
controlPanel.ShouldNotBeNull();
controlPanel.Status.ShouldBe(Status.Failed);
FailingParamlessFlow.ShouldThrow = false;
await controlPanel.ScheduleRestart().Completion();
await controlPanel.Refresh();
controlPanel.Status.ShouldBe(Status.Succeeded);
}
private class FailingParamlessFlows : Flows<FailingParamlessFlow>
{
public FailingParamlessFlows(FlowsContainer flowsContainer)
: base(nameof(FailingParamlessFlow), flowsContainer, options: null) { }
}
public class FailingParamlessFlow : Flow
{
public static bool ShouldThrow = true;
public override Task Run()
{
return ShouldThrow
? Task.FromException<TimeoutException>(new TimeoutException())
: Task.CompletedTask;
}
}
[TestMethod]
public async Task FlowCanBeCreatedWithInitialState()
{
var flowsContainer = FlowsContainer.Create();
var flow = new InitialStateFlow();
var flows = flowsContainer.RegisterAnonymousFlow(
flowFactory: () => flow
);
await flows.Run(
"SomeInstanceId",
new InitialState(
[new MessageAndIdempotencyKey(new StringMessage("InitialMessageValue"))],
[new InitialEffect(0.ToEffectId(), "InitialEffectValue")]
)
);
flow.InitialEffectValue.ShouldBe("InitialEffectValue");
flow.InitialMessageValue.ShouldBe("InitialMessageValue");
}
private class InitialStateFlow : Flow
{
public string? InitialEffectValue { get; set; }
public string? InitialMessageValue { get; set; }
public override async Task Run()
{
InitialEffectValue = await Capture(() => "should not be called");
var msg = await Message<StringMessage>();
InitialMessageValue = msg.Value;
}
}
}