Repository navigation
Expand file tree
/
Copy pathApprovalHostEventTest.cs
More file actions
91 lines (80 loc) · 4.34 KB
/
Copy pathApprovalHostEventTest.cs
File metadata and controls
91 lines (80 loc) · 4.34 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
using Microsoft.DurableTask;
using Microsoft.DurableTask.Client;
using Microsoft.DurableTask.Testing;
using NUnit.Framework;
using UnitTestEx;
using UnitTestEx.Azure.Functions;
using UnitTestEx.Azure.Functions.Durable.TestHost;
using UnitTestEx.DurableFunction;
namespace UnitTestEx.Azure.Functions.Durable.Test;
/// <summary>
/// Verifies external-event completion of the approval orchestration in the in-memory Durable Task host.
/// </summary>
[TestFixture]
public sealed class ApprovalHostEventTest
{
/// <summary>Verifies an external event completes the orchestration without waiting for its 24-hour timer.</summary>
[Test]
public async Task ApprovalEventCompletesBeforeProductionLengthTimeout()
{
using var functions = FunctionTester.Create<WorkflowActivities>();
DurableTaskTestHost? host = null;
try
{
host = await functions.StartDurableTaskTestHostAsync(registry =>
registry.AddOrchestratorFuncWithTimerDiagnostics<ApprovalRequest, ApprovalDecision>(
nameof(ApprovalOrchestration.AwaitApproval),
(context, _) => ApprovalOrchestration.AwaitApproval(context)));
var instanceId = await host.Client.ScheduleNewOrchestrationInstanceAsync(
nameof(ApprovalOrchestration.AwaitApproval),
new ApprovalRequest(TimeSpan.FromHours(24)));
await host.Client.WaitForInstanceStartAsync(instanceId).WaitAsync(TimeSpan.FromSeconds(5));
await host.Client.RaiseEventAsync(instanceId, "Approved", new ApprovalDecision(true));
var metadata = await host.Client.WaitForInstanceCompletionAsync(instanceId, getInputsAndOutputs: true)
.WaitAsync(TimeSpan.FromSeconds(5));
Assert.That(metadata, Is.Not.Null);
Assert.That(metadata!.RuntimeStatus, Is.EqualTo(OrchestrationRuntimeStatus.Completed));
Assert.That(metadata.ReadOutputAs<ApprovalDecision>(), Is.EqualTo(new ApprovalDecision(true)));
Assert.That(functions.SharedState.GetLoggerMessages().Any(message =>
message?.Contains("uses the host's real wall clock", StringComparison.Ordinal) == true), Is.True);
await host.Client.RaiseEventAsync(instanceId, "Approved", new ApprovalDecision(false));
var afterLateEvent = await host.Client.GetInstanceAsync(instanceId, getInputsAndOutputs: true)
.WaitAsync(TimeSpan.FromSeconds(5));
Assert.That(afterLateEvent!.RuntimeStatus, Is.EqualTo(OrchestrationRuntimeStatus.Completed));
Assert.That(afterLateEvent.ReadOutputAs<ApprovalDecision>(), Is.EqualTo(new ApprovalDecision(true)));
}
finally
{
if (host is not null)
await host.DisposeAsync();
}
}
/// <summary>Verifies the real approval orchestration returns a timeout decision for an explicit short test input.</summary>
[Test]
public async Task ExplicitShortTestTimeoutCompletesWithDeniedDecision()
{
using var functions = FunctionTester.Create<WorkflowActivities>();
DurableTaskTestHost? host = null;
try
{
host = await functions.StartDurableTaskTestHostAsync(registry =>
registry.AddOrchestratorFuncWithTimerDiagnostics<ApprovalRequest, ApprovalDecision>(
nameof(ApprovalOrchestration.AwaitApproval),
(context, _) => ApprovalOrchestration.AwaitApproval(context)));
var instanceId = await host.Client.ScheduleNewOrchestrationInstanceAsync(
nameof(ApprovalOrchestration.AwaitApproval),
new ApprovalRequest(TimeSpan.FromMilliseconds(100)));
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(new ApprovalDecision(false)));
Assert.That(functions.SharedState.GetLoggerMessages().Any(message =>
message?.Contains("uses the host's real wall clock", StringComparison.Ordinal) == true), Is.True);
}
finally
{
if (host is not null)
await host.DisposeAsync();
}
}
}