Repository navigation
Expand file tree
/
Copy pathEarlyExternalEventTest.cs
More file actions
114 lines (103 loc) · 4.88 KB
/
Copy pathEarlyExternalEventTest.cs
File metadata and controls
114 lines (103 loc) · 4.88 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
using Microsoft.DurableTask;
using Microsoft.DurableTask.Client;
using Microsoft.DurableTask.Testing;
using NUnit.Framework;
using UnitTestEx.DurableFunction;
namespace UnitTestEx.Azure.Functions.Durable.Test;
/// <summary>
/// Verifies external events are buffered while an orchestration is suspended before its event wait.
/// </summary>
[TestFixture]
public sealed class EarlyExternalEventTest
{
/// <summary>Verifies an event raised during a preceding activity is delivered after the event wait is registered.</summary>
[Test]
public async Task EventRaisedBeforeWaitIsBufferedAndDeserialized()
{
DurableTaskTestHost? host = null;
var gateStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var releaseGate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
try
{
host = await DurableTaskTestHost.StartAsync(registry =>
{
registry.AddOrchestratorFunc<string, ApprovalDecision>(
"EarlyEventOrchestration",
async (context, input) =>
{
await context.CallActivityAsync<string>("EarlyEventGate", input);
return await context.WaitForExternalEvent<ApprovalDecision>("Approved");
});
registry.AddActivityFunc<string, string>(
"EarlyEventGate",
async (_, input) =>
{
gateStarted.TrySetResult();
await releaseGate.Task;
return input;
});
});
var instanceId = await host.Client.ScheduleNewOrchestrationInstanceAsync("EarlyEventOrchestration", "hold");
await gateStarted.Task.WaitAsync(TimeSpan.FromSeconds(5));
var decision = new ApprovalDecision(true);
await host.Client.RaiseEventAsync(instanceId, "Approved", decision);
releaseGate.TrySetResult();
var metadata = await host.Client.WaitForInstanceCompletionAsync(instanceId, getInputsAndOutputs: true)
.WaitAsync(TimeSpan.FromSeconds(5));
Assert.That(metadata!.RuntimeStatus, Is.EqualTo(OrchestrationRuntimeStatus.Completed));
Assert.That(metadata.ReadOutputAs<ApprovalDecision>(), Is.EqualTo(decision));
}
finally
{
releaseGate.TrySetResult();
if (host is not null)
await host.DisposeAsync();
}
}
/// <summary>Verifies multiple same-name events raised before either wait are delivered in arrival order.</summary>
[Test]
public async Task MultipleEventsRaisedBeforeWaitsAreBufferedInArrivalOrder()
{
DurableTaskTestHost? host = null;
var gateStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var releaseGate = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
try
{
host = await DurableTaskTestHost.StartAsync(registry =>
{
registry.AddOrchestratorFunc<string, string>(
"MultipleEarlyEventOrchestration",
async (context, input) =>
{
await context.CallActivityAsync<string>("MultipleEarlyEventGate", input);
string first = await context.WaitForExternalEvent<string>("Progress");
string second = await context.WaitForExternalEvent<string>("Progress");
return $"{first}|{second}";
});
registry.AddActivityFunc<string, string>(
"MultipleEarlyEventGate",
async (_, input) =>
{
gateStarted.TrySetResult();
await releaseGate.Task;
return input;
});
});
var instanceId = await host.Client.ScheduleNewOrchestrationInstanceAsync("MultipleEarlyEventOrchestration", "hold");
await gateStarted.Task.WaitAsync(TimeSpan.FromSeconds(5));
await host.Client.RaiseEventAsync(instanceId, "Progress", "first");
await host.Client.RaiseEventAsync(instanceId, "Progress", "second");
releaseGate.TrySetResult();
var metadata = await host.Client.WaitForInstanceCompletionAsync(instanceId, getInputsAndOutputs: true)
.WaitAsync(TimeSpan.FromSeconds(5));
Assert.That(metadata!.RuntimeStatus, Is.EqualTo(OrchestrationRuntimeStatus.Completed));
Assert.That(metadata.ReadOutputAs<string>(), Is.EqualTo("first|second"));
}
finally
{
releaseGate.TrySetResult();
if (host is not null)
await host.DisposeAsync();
}
}
}