Repository navigation
Expand file tree
/
Copy pathApprovalVirtualTimeTest.cs
More file actions
262 lines (208 loc) · 13.5 KB
/
Copy pathApprovalVirtualTimeTest.cs
File metadata and controls
262 lines (208 loc) · 13.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using NUnit.Framework;
using UnitTestEx.DurableFunction;
using UnitTestEx.Azure.Functions.Durable;
namespace UnitTestEx.Azure.Functions.Durable.Test;
/// <summary>
/// Verifies virtual-time timer, event, and cancellation behavior for the approval orchestration.
/// </summary>
[TestFixture]
public sealed class ApprovalVirtualTimeTest
{
/// <summary>Verifies unsupported orchestration operations fail explicitly.</summary>
[Test]
public void OperationsOutsideTimerEventScopeFailExplicitly()
{
using var context = new VirtualTimeOrchestrationContext(nameof(ApprovalOrchestration.AwaitApproval), new ApprovalRequest(TimeSpan.FromHours(24)));
Assert.Throws<NotSupportedException>(() => { _ = context.CallActivityAsync<string>("activity"); });
Assert.Throws<NotSupportedException>(() => { _ = context.CallSubOrchestratorAsync<string>("child"); });
Assert.Throws<NotSupportedException>(() => context.SetCustomStatus("unsupported"));
Assert.Throws<NotSupportedException>(() => context.NewGuid());
}
/// <summary>Verifies that a pre-canceled timer remains canceled after advancing virtual time.</summary>
[Test]
public async Task PreCanceledTimerStaysCanceledAfterClockAdvance()
{
using var context = new VirtualTimeOrchestrationContext(nameof(ApprovalOrchestration.AwaitApproval), new ApprovalRequest(TimeSpan.FromHours(24)));
using var cancellation = new CancellationTokenSource();
cancellation.Cancel();
Task timer = context.CreateTimer(context.CurrentUtcDateTime.AddHours(24), cancellation.Token);
Assert.ThrowsAsync<TaskCanceledException>(async () => await timer);
context.AdvanceBy(TimeSpan.FromHours(24));
Assert.That(context.Timers.Single().Outcome, Is.EqualTo(VirtualTimeOrchestrationContext.TimerOutcome.Canceled));
}
/// <summary>Verifies that a pre-canceled event wait rejects a later event.</summary>
[Test]
public async Task PreCanceledEventWaitRejectsLaterEvent()
{
using var context = new VirtualTimeOrchestrationContext(nameof(ApprovalOrchestration.AwaitApproval), new ApprovalRequest(TimeSpan.FromHours(24)));
using var cancellation = new CancellationTokenSource();
cancellation.Cancel();
Task<ApprovalDecision> wait = context.WaitForExternalEvent<ApprovalDecision>("Approved", cancellation.Token);
Assert.ThrowsAsync<TaskCanceledException>(async () => await wait);
Assert.That(context.EventWaits.Single().IsCanceled, Is.True);
Assert.Throws<InvalidOperationException>(() => context.RaiseEvent("Approved", new ApprovalDecision(true)));
}
/// <summary>Verifies a same-named event completes the most recently registered wait and another wait can be canceled.</summary>
[Test]
public async Task SameNamedEventCompletesLatestWaitAndOtherWaitCanBeCanceled()
{
using var context = new VirtualTimeOrchestrationContext(nameof(ApprovalOrchestration.AwaitApproval), new ApprovalRequest(TimeSpan.FromHours(24)));
using var firstCancellation = new CancellationTokenSource();
using var secondCancellation = new CancellationTokenSource();
Task<ApprovalDecision> firstWait = context.WaitForExternalEvent<ApprovalDecision>("Approved", firstCancellation.Token);
Task<ApprovalDecision> secondWait = context.WaitForExternalEvent<ApprovalDecision>("Approved", secondCancellation.Token);
context.RaiseEvent("Approved", new ApprovalDecision(true));
Assert.That((await secondWait.WaitAsync(TimeSpan.FromSeconds(5))).Approved, Is.True);
Assert.That(firstWait.IsCompleted, Is.False);
firstCancellation.Cancel();
Assert.ThrowsAsync<TaskCanceledException>(async () => await firstWait);
Assert.That(context.EventWaits.Select(wait => wait.IsCanceled), Is.EqualTo(new[] { true, false }));
}
/// <summary>Verifies canceling a pending timer prevents a later clock advance from firing it.</summary>
[Test]
public async Task PendingTimerCancellationStaysCanceledAfterClockAdvance()
{
using var context = new VirtualTimeOrchestrationContext(nameof(ApprovalOrchestration.AwaitApproval), new ApprovalRequest(TimeSpan.FromHours(24)));
using var cancellation = new CancellationTokenSource();
Task timer = context.CreateTimer(context.CurrentUtcDateTime.AddHours(24), cancellation.Token);
cancellation.Cancel();
Assert.ThrowsAsync<TaskCanceledException>(async () => await timer);
context.AdvanceBy(TimeSpan.FromHours(24));
Assert.That(context.Timers.Single().Outcome, Is.EqualTo(VirtualTimeOrchestrationContext.TimerOutcome.Canceled));
}
/// <summary>Verifies canceling a pending event wait prevents a later event from completing it.</summary>
[Test]
public async Task PendingEventCancellationRejectsLaterEvent()
{
using var context = new VirtualTimeOrchestrationContext(nameof(ApprovalOrchestration.AwaitApproval), new ApprovalRequest(TimeSpan.FromHours(24)));
using var cancellation = new CancellationTokenSource();
Task<ApprovalDecision> wait = context.WaitForExternalEvent<ApprovalDecision>("Approved", cancellation.Token);
cancellation.Cancel();
Assert.ThrowsAsync<TaskCanceledException>(async () => await wait);
Assert.That(context.EventWaits.Single().IsCanceled, Is.True);
Assert.Throws<InvalidOperationException>(() => context.RaiseEvent("Approved", new ApprovalDecision(true)));
}
/// <summary>Verifies a timer at or before the current virtual time completes immediately.</summary>
[Test]
public void DueTimerCompletesImmediately()
{
using var context = new VirtualTimeOrchestrationContext(nameof(ApprovalOrchestration.AwaitApproval), new ApprovalRequest(TimeSpan.FromHours(24)));
Task timer = context.CreateTimer(context.CurrentUtcDateTime.AddTicks(-1), CancellationToken.None);
Assert.That(timer.IsCompletedSuccessfully, Is.True);
Assert.That(context.Timers.Single().Outcome, Is.EqualTo(VirtualTimeOrchestrationContext.TimerOutcome.Fired));
}
/// <summary>Verifies negative clock advances are rejected without changing virtual time.</summary>
[Test]
public void NegativeClockAdvanceIsRejectedWithoutChangingTime()
{
using var context = new VirtualTimeOrchestrationContext(nameof(ApprovalOrchestration.AwaitApproval), new ApprovalRequest(TimeSpan.FromHours(24)));
DateTime before = context.CurrentUtcDateTime;
Assert.Throws<ArgumentOutOfRangeException>(() => context.AdvanceBy(TimeSpan.FromTicks(-1)));
Assert.That(context.CurrentUtcDateTime, Is.EqualTo(before));
}
/// <summary>Verifies timer deadlines must use UTC and are not registered when invalid.</summary>
[Test]
public void NonUtcTimerDeadlineIsRejectedBeforeRegistration()
{
using var context = new VirtualTimeOrchestrationContext(nameof(ApprovalOrchestration.AwaitApproval), new ApprovalRequest(TimeSpan.FromHours(24)));
DateTime deadline = DateTime.SpecifyKind(context.CurrentUtcDateTime.AddHours(1), DateTimeKind.Unspecified);
Assert.Throws<ArgumentException>(() => context.CreateTimer(deadline, CancellationToken.None));
Assert.That(context.Timers, Is.Empty);
}
/// <summary>Verifies timer deadlines determine which timers fire as the virtual clock advances.</summary>
[Test]
public void TimersFireByDeadlineIndependentOfCreationOrder()
{
using var context = new VirtualTimeOrchestrationContext(nameof(ApprovalOrchestration.AwaitApproval), new ApprovalRequest(TimeSpan.FromHours(24)));
Task later = context.CreateTimer(context.CurrentUtcDateTime.AddHours(2), CancellationToken.None);
Task earlier = context.CreateTimer(context.CurrentUtcDateTime.AddHours(1), CancellationToken.None);
context.AdvanceBy(TimeSpan.FromHours(1));
Assert.That(earlier.IsCompletedSuccessfully, Is.True);
Assert.That(later.IsCompleted, Is.False);
context.AdvanceBy(TimeSpan.FromHours(1));
Assert.That(later.IsCompletedSuccessfully, Is.True);
}
/// <summary>Verifies every timer due during one clock advance completes, including equal deadlines.</summary>
[Test]
public void OneClockAdvanceCompletesAllDueTimers()
{
using var context = new VirtualTimeOrchestrationContext(nameof(ApprovalOrchestration.AwaitApproval), new ApprovalRequest(TimeSpan.FromHours(24)));
Task[] timers =
[
context.CreateTimer(context.CurrentUtcDateTime.AddHours(1), CancellationToken.None),
context.CreateTimer(context.CurrentUtcDateTime.AddHours(2), CancellationToken.None),
context.CreateTimer(context.CurrentUtcDateTime.AddHours(2), CancellationToken.None),
context.CreateTimer(context.CurrentUtcDateTime.AddHours(4), CancellationToken.None),
];
context.AdvanceBy(TimeSpan.FromHours(2));
Assert.That(timers.Take(3).All(timer => timer.IsCompletedSuccessfully), Is.True);
Assert.That(timers[3].IsCompleted, Is.False);
Assert.That(context.Timers.Select(timer => timer.Outcome), Is.EqualTo(new[]
{
VirtualTimeOrchestrationContext.TimerOutcome.Fired,
VirtualTimeOrchestrationContext.TimerOutcome.Fired,
VirtualTimeOrchestrationContext.TimerOutcome.Fired,
VirtualTimeOrchestrationContext.TimerOutcome.Pending,
}));
context.AdvanceBy(TimeSpan.FromHours(2));
Assert.That(timers.All(timer => timer.IsCompletedSuccessfully), Is.True);
Assert.That(context.Timers.All(timer => timer.Outcome == VirtualTimeOrchestrationContext.TimerOutcome.Fired), Is.True);
}
/// <summary>Verifies an event delivered after the clock fires the due timer cannot change the timeout result.</summary>
[Test]
public async Task EventAfterAdvanceToDeadlineCannotChangeTimeoutResult()
{
using var context = new VirtualTimeOrchestrationContext(nameof(ApprovalOrchestration.AwaitApproval), new ApprovalRequest(TimeSpan.FromHours(24)));
Task<ApprovalDecision> execution = ApprovalOrchestration.AwaitApproval(context);
context.AdvanceBy(TimeSpan.FromHours(24));
ApprovalDecision result = await execution.WaitAsync(TimeSpan.FromSeconds(5));
Assert.That(result.Approved, Is.False);
Assert.That(context.Timers.Single().Outcome, Is.EqualTo(VirtualTimeOrchestrationContext.TimerOutcome.Fired));
Assert.That(context.EventWaits.Single().IsCanceled, Is.True);
Assert.Throws<InvalidOperationException>(() => context.RaiseEvent("Approved", new ApprovalDecision(true)));
}
/// <summary>Verifies that an event just before the deadline cancels the timer.</summary>
[Test]
public async Task EventOneTickBeforeTimeoutCancelsTimer()
{
using var context = new VirtualTimeOrchestrationContext(nameof(ApprovalOrchestration.AwaitApproval), new ApprovalRequest(TimeSpan.FromHours(24)));
Task<ApprovalDecision> execution = ApprovalOrchestration.AwaitApproval(context);
context.AdvanceBy(TimeSpan.FromHours(24) - TimeSpan.FromTicks(1));
Assert.That(context.CurrentUtcDateTime, Is.EqualTo(new DateTime(2026, 1, 1, 23, 59, 59, DateTimeKind.Utc).AddTicks(9_999_999)));
Assert.That(execution.IsCompleted, Is.False);
context.RaiseEvent("Approved", new ApprovalDecision(true));
ApprovalDecision result = await execution.WaitAsync(TimeSpan.FromSeconds(5));
context.AdvanceBy(TimeSpan.FromTicks(1));
Assert.That(result.Approved, Is.True);
Assert.That(context.Timers.Single().Outcome, Is.EqualTo(VirtualTimeOrchestrationContext.TimerOutcome.Canceled));
}
/// <summary>Verifies that an event completes the orchestration before the configured timeout.</summary>
[Test]
public async Task EventBeforeProductionTimeoutCompletesAndCancelsTimer()
{
using var context = new VirtualTimeOrchestrationContext(nameof(ApprovalOrchestration.AwaitApproval), new ApprovalRequest(TimeSpan.FromHours(24)));
Task<ApprovalDecision> execution = ApprovalOrchestration.AwaitApproval(context);
Assert.That(execution.IsCompleted, Is.False);
Assert.That(context.Timers, Has.Count.EqualTo(1));
Assert.That(context.EventWaits, Has.Count.EqualTo(1));
context.AdvanceBy(TimeSpan.FromHours(12));
Assert.That(execution.IsCompleted, Is.False);
context.RaiseEvent("Approved", new ApprovalDecision(true));
ApprovalDecision result = await execution.WaitAsync(TimeSpan.FromSeconds(5));
Assert.That(result.Approved, Is.True);
Assert.That(context.Timers.Single().Outcome, Is.EqualTo(VirtualTimeOrchestrationContext.TimerOutcome.Canceled));
Assert.That(context.EventWaits.Single().IsCanceled, Is.False);
}
/// <summary>Verifies that a production-length timeout completes by advancing virtual time.</summary>
[Test]
public async Task ProductionTimeoutCanAdvanceWithoutWaitingAndCancelsEventWait()
{
using var context = new VirtualTimeOrchestrationContext(nameof(ApprovalOrchestration.AwaitApproval), new ApprovalRequest(TimeSpan.FromHours(24)));
Task<ApprovalDecision> execution = ApprovalOrchestration.AwaitApproval(context);
context.AdvanceBy(TimeSpan.FromHours(24));
ApprovalDecision result = await execution.WaitAsync(TimeSpan.FromSeconds(5));
Assert.That(result.Approved, Is.False);
Assert.That(context.Timers.Single().Outcome, Is.EqualTo(VirtualTimeOrchestrationContext.TimerOutcome.Fired));
Assert.That(context.EventWaits.Single().IsCanceled, Is.True);
}
}