-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathOptionsTests.cs
More file actions
107 lines (88 loc) · 3.65 KB
/
Copy pathOptionsTests.cs
File metadata and controls
107 lines (88 loc) · 3.65 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
using Cleipnir.Flows.AspNet;
using Cleipnir.ResilientFunctions.Domain;
using Cleipnir.ResilientFunctions.Domain.Exceptions;
using Cleipnir.ResilientFunctions.Storage;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
namespace Cleipnir.Flows.Tests.Flows;
[TestClass]
public class OptionsTests
{
[TestMethod]
public async Task SimpleFlowCompletesSuccessfully()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddFlows(c => c
.UseInMemoryStore()
.WithOptions(new Settings(
messagesDefaultMaxWaitForCompletion: TimeSpan.FromDays(1),
watchdogCheckFrequency: TimeSpan.FromMilliseconds(100)
))
.RegisterFlow<OptionsTestWithDefaultProvidedOptionsFlow>()
);
serviceCollection.AddScoped<OptionsTestWithOverriddenOptionsFlow>();
serviceCollection.AddTransient(sp => new Flows<OptionsTestWithOverriddenOptionsFlow>(
nameof(OptionsTestWithOverriddenOptionsFlow),
sp.GetRequiredService<FlowsContainer>(),
options: new FlowOptions(messagesDefaultMaxWaitForCompletion: TimeSpan.Zero)
));
var sp = serviceCollection.BuildServiceProvider();
var flowsWithOverridenOptions = sp.GetRequiredService<Flows<OptionsTestWithOverriddenOptionsFlow>>();
await Should.ThrowAsync<InvocationSuspendedException>(
() => flowsWithOverridenOptions.Run("Id")
);
var flowsWithDefaultProvidedOptions = sp.GetRequiredService<Flows<OptionsTestWithDefaultProvidedOptionsFlow>>();
await flowsWithDefaultProvidedOptions.Schedule("Id");
await Task.Delay(100);
var controlPanel = await flowsWithDefaultProvidedOptions.ControlPanel("Id");
controlPanel.ShouldNotBeNull();
// Flow may be Executing (waiting for message) or Suspended depending on timing
controlPanel.Status.ShouldBeOneOf(Status.Executing, Status.Suspended);
await controlPanel.Messages.Append(new StringWrapper("Hello"));
await controlPanel.WaitForCompletion(allowPostponeAndSuspended: true);
}
[TestMethod]
public async Task FlowNameCanBeSpecifiedFromTheOutside()
{
var serviceCollection = new ServiceCollection();
var store = new InMemoryFunctionStore();
var storedType = await store.TypeStore.InsertOrGetStoredType("SomeOtherFlowName");
serviceCollection.AddFlows(c => c
.UseInMemoryStore(store)
.WithOptions(new Settings(
messagesDefaultMaxWaitForCompletion: TimeSpan.FromDays(1),
watchdogCheckFrequency: TimeSpan.FromMilliseconds(100)
))
);
serviceCollection.AddScoped<SimpleFlow>();
serviceCollection.AddTransient(sp => new Flows<SimpleFlow>(
flowName: "SomeOtherFlowName",
sp.GetRequiredService<FlowsContainer>()
));
var sp = serviceCollection.BuildServiceProvider();
var flows = sp.GetRequiredService<Flows<SimpleFlow>>();
await flows.Run("Id");
var sf = await store.GetFunction(StoredId.Create(storedType, "Id"));
sf.ShouldNotBeNull();
sf.Status.ShouldBe(Status.Succeeded);
}
}
public class OptionsTestWithOverriddenOptionsFlow : Flow
{
public override async Task Run()
{
await Message<StringWrapper>();
}
}
public class OptionsTestWithDefaultProvidedOptionsFlow : Flow
{
public override async Task Run()
{
await Message<StringWrapper>();
}
}
public record StringWrapper(string Value);
public class SimpleFlow : Flow
{
public override Task Run() => Task.CompletedTask;
}