Repository navigation
Expand file tree
/
Copy pathBatchOrchestrationTest.cs
More file actions
311 lines (262 loc) · 16 KB
/
Copy pathBatchOrchestrationTest.cs
File metadata and controls
311 lines (262 loc) · 16 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using Microsoft.DurableTask;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using NUnit.Framework;
using UnitTestEx.DurableFunction;
namespace UnitTestEx.Azure.Functions.Durable.Test;
/// <summary>
/// Verifies batch orchestration behavior with controllable activity and child calls.
/// </summary>
[TestFixture]
public sealed class BatchOrchestrationTest
{
/// <summary>Verifies that results retain input order when activities complete out of order.</summary>
[Test]
public async Task ProcessBatchPreservesInputOrderWhenActivitiesCompleteOutOfOrder()
{
var context = new ControllableOrchestrationContext(nameof(BatchOrchestration.ProcessBatch), new BatchRequest(" batch-7 ", [new WorkItem("A", 10), new WorkItem("B", 20), new WorkItem("C", 30)]));
Task<BatchResult> execution = BatchOrchestration.ProcessBatch(context);
var normalize = await context.WaitForActivityCallAsync<string>(nameof(ActivityTransforms.NormalizeReference));
normalize.Complete("BATCH-7");
var processCalls = await context.WaitForActivityCallsAsync<ItemResult>(nameof(WorkflowActivities.ProcessItem), 3);
Assert.That(processCalls.Select(call => ((WorkItem)call.Input!).Id), Is.EqualTo(new[] { "A", "B", "C" }));
foreach (var call in processCalls)
{
var retryPolicy = call.Options?.Retry?.Policy
?? throw new AssertionException("Each item activity call should carry its retry policy.");
Assert.That(retryPolicy.MaxNumberOfAttempts, Is.EqualTo(3));
Assert.That(retryPolicy.FirstRetryInterval, Is.EqualTo(TimeSpan.FromSeconds(1)));
}
processCalls[2].Complete(new ItemResult("C", 30));
processCalls[1].Complete(new ItemResult("B", 20));
Assert.That(execution.IsCompleted, Is.False);
processCalls[0].Complete(new ItemResult("A", 10));
var total = await context.WaitForActivityCallAsync<decimal>(nameof(ActivityTransforms.TotalItems));
var orderedItems = (ItemResult[])total.Input!;
Assert.That(orderedItems.Select(item => item.Id), Is.EqualTo(new[] { "A", "B", "C" }));
total.Complete(60);
var finalize = await context.WaitForSubOrchestrationCallAsync<BatchResult>(nameof(BatchOrchestration.FinalizeBatch));
var finalizationInput = (BatchResult)finalize.Input!;
Assert.That(finalizationInput.Reference, Is.EqualTo("BATCH-7"));
Assert.That(finalizationInput.Total, Is.EqualTo(60));
Assert.That(finalizationInput.Items.Select(item => item.Id), Is.EqualTo(new[] { "A", "B", "C" }));
Assert.That(finalize.Options, Is.TypeOf<SubOrchestrationOptions>());
Assert.That(((SubOrchestrationOptions)finalize.Options!).InstanceId, Is.EqualTo("00000000-0000-0000-0000-000000000001"));
finalize.Complete(finalizationInput);
BatchResult result = await execution.WaitAsync(TimeSpan.FromSeconds(5));
Assert.That(result, Is.EqualTo(finalizationInput));
}
/// <summary>Verifies that the orchestration handles SDK failure details from an activity call.</summary>
[Test]
public async Task RecoverWorkflowHandlesDurableFailureDetailsFromActivityCall()
{
var context = new ControllableOrchestrationContext(nameof(FailureOrchestration.RecoverWorkflow), "batch-9");
Task<string> execution = FailureOrchestration.RecoverWorkflow(context);
var reject = await context.WaitForActivityCallAsync<string>(nameof(ActivityTransforms.RejectWorkflow));
reject.Fail(new WorkflowRejectedException("Workflow batch-9 was declined."));
Assert.That(reject.FailureDetails, Is.Not.Null);
Assert.That(reject.FailureDetails!.ErrorType, Is.EqualTo(typeof(WorkflowRejectedException).FullName));
Assert.That(reject.FailureDetails.ErrorMessage, Is.EqualTo("Workflow batch-9 was declined."));
Assert.That(reject.FailureDetails.IsCausedBy<WorkflowRejectedException>(), Is.True);
Assert.That(await execution.WaitAsync(TimeSpan.FromSeconds(5)), Is.EqualTo("Declined"));
Assert.That(context.CustomStatus, Is.EqualTo("Declined"));
}
}
/// <summary>
/// Captures orchestration calls and lets a test complete them in a controlled order.
/// </summary>
/// <param name="name">The orchestration name.</param>
/// <param name="input">The orchestration input.</param>
internal sealed class ControllableOrchestrationContext(string name, object input) : TaskOrchestrationContext
{
private readonly object _gate = new();
private readonly List<RecordedCall> _calls = [];
private TaskCompletionSource _callsChanged = NewSignal();
private int _nextTaskId;
/// <summary>Gets the orchestration name.</summary>
public override TaskName Name => name;
/// <summary>Gets the orchestration instance identifier.</summary>
public override string InstanceId => "batch-test";
/// <summary>Gets the parent orchestration, which is always null in this context.</summary>
public override ParentOrchestrationInstance? Parent => null;
/// <summary>Gets the fixed orchestration time used by this test context.</summary>
public override DateTime CurrentUtcDateTime => new(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc);
/// <summary>Gets whether the orchestration is being replayed; this context never replays.</summary>
public override bool IsReplaying => false;
/// <summary>Gets the logger factory used by the orchestration context.</summary>
protected override ILoggerFactory LoggerFactory => NullLoggerFactory.Instance;
/// <summary>Gets the most recently assigned custom status.</summary>
public object? CustomStatus { get; private set; }
/// <summary>Gets the orchestration input.</summary>
/// <typeparam name="T">The expected input type.</typeparam>
/// <returns>The orchestration input.</returns>
public override T GetInput<T>()
=> input is T typed
? typed
: throw new InvalidCastException($"Orchestration input is not {typeof(T).FullName}.");
/// <summary>Records an activity call and returns a task completed by the test.</summary>
/// <typeparam name="TResult">The activity result type.</typeparam>
/// <param name="name">The activity name.</param>
/// <param name="input">The activity input.</param>
/// <param name="options">The activity options.</param>
/// <returns>The task completed by the test.</returns>
public override Task<TResult> CallActivityAsync<TResult>(TaskName name, object? input = null, TaskOptions? options = null)
{
var call = new ActivityCall<TResult>(name, input, options, Interlocked.Increment(ref _nextTaskId));
Record(call);
return call.Task;
}
/// <summary>Records a sub-orchestration call and returns a task completed by the test.</summary>
/// <typeparam name="TResult">The sub-orchestration result type.</typeparam>
/// <param name="orchestratorName">The sub-orchestration name.</param>
/// <param name="input">The sub-orchestration input.</param>
/// <param name="options">The sub-orchestration options.</param>
/// <returns>The task completed by the test.</returns>
public override Task<TResult> CallSubOrchestratorAsync<TResult>(TaskName orchestratorName, object? input = null, TaskOptions? options = null)
{
var call = new SubOrchestrationCall<TResult>(orchestratorName, input, options);
Record(call);
return call.Task;
}
/// <summary>Waits for the first recorded activity call with the specified name.</summary>
/// <typeparam name="TResult">The activity result type.</typeparam>
/// <param name="name">The activity name.</param>
/// <returns>The recorded activity call.</returns>
public async Task<ActivityCall<TResult>> WaitForActivityCallAsync<TResult>(string name)
=> (await WaitForCallsAsync<ActivityCall<TResult>>(name, 1)).Single();
/// <summary>Waits until the requested number of activity calls has been recorded.</summary>
/// <typeparam name="TResult">The activity result type.</typeparam>
/// <param name="name">The activity name.</param>
/// <param name="count">The minimum number of matching calls.</param>
/// <returns>The matching activity calls.</returns>
public Task<IReadOnlyList<ActivityCall<TResult>>> WaitForActivityCallsAsync<TResult>(string name, int count)
=> WaitForCallsAsync<ActivityCall<TResult>>(name, count);
/// <summary>Waits for the first recorded sub-orchestration call with the specified name.</summary>
/// <typeparam name="TResult">The sub-orchestration result type.</typeparam>
/// <param name="name">The sub-orchestration name.</param>
/// <returns>The recorded sub-orchestration call.</returns>
public async Task<SubOrchestrationCall<TResult>> WaitForSubOrchestrationCallAsync<TResult>(string name)
=> (await WaitForCallsAsync<SubOrchestrationCall<TResult>>(name, 1)).Single();
/// <summary>Waits until the requested number of matching calls has been recorded.</summary>
/// <typeparam name="TCall">The recorded call type.</typeparam>
/// <param name="name">The call name.</param>
/// <param name="count">The minimum number of matching calls.</param>
/// <returns>The matching calls.</returns>
private async Task<IReadOnlyList<TCall>> WaitForCallsAsync<TCall>(string name, int count)
where TCall : RecordedCall
{
while (true)
{
Task wait;
lock (_gate)
{
var matching = _calls.OfType<TCall>()
.Where(call => string.Equals(call.Name.ToString(), name, StringComparison.Ordinal))
.ToArray();
if (matching.Length >= count)
return matching;
wait = _callsChanged.Task;
}
await wait.WaitAsync(TimeSpan.FromSeconds(5));
}
}
/// <summary>Adds a call to the recorded call list and signals waiting tests.</summary>
/// <param name="call">The call to record.</param>
private void Record(RecordedCall call)
{
TaskCompletionSource changed;
lock (_gate)
{
_calls.Add(call);
changed = _callsChanged;
_callsChanged = NewSignal();
}
changed.TrySetResult();
}
/// <summary>Throws because timers are outside this batch-call test context.</summary>
/// <param name="fireAt">The timer deadline.</param>
/// <param name="cancellationToken">The token used to cancel the timer.</param>
/// <returns>This method does not return.</returns>
public override Task CreateTimer(DateTime fireAt, CancellationToken cancellationToken)
=> throw new NotSupportedException("Timers are outside this batch-call test context.");
/// <summary>Throws because external events are outside this batch-call test context.</summary>
/// <typeparam name="T">The event payload type.</typeparam>
/// <param name="eventName">The event name.</param>
/// <param name="cancellationToken">The token used to cancel the wait.</param>
/// <returns>This method does not return.</returns>
public override Task<T> WaitForExternalEvent<T>(string eventName, CancellationToken cancellationToken = default)
=> throw new NotSupportedException("External events are outside this batch-call test context.");
/// <summary>Throws because event sending is outside this batch-call test context.</summary>
/// <param name="instanceId">The target instance identifier.</param>
/// <param name="eventName">The event name.</param>
/// <param name="payload">The event payload.</param>
public override void SendEvent(string instanceId, string eventName, object payload)
=> throw new NotSupportedException("External events are outside this batch-call test context.");
/// <summary>Stores the orchestration's custom status.</summary>
/// <param name="customStatus">The custom status value.</param>
public override void SetCustomStatus(object? customStatus) => CustomStatus = customStatus;
/// <summary>Throws because continuing as new is outside this batch-call test context.</summary>
/// <param name="newInput">The new orchestration input.</param>
/// <param name="preserveUnprocessedEvents">Whether to preserve unprocessed events.</param>
public override void ContinueAsNew(object? newInput = null, bool preserveUnprocessedEvents = true)
=> throw new NotSupportedException("ContinueAsNew is outside this batch-call test context.");
/// <summary>Returns the fixed GUID used by this test context.</summary>
/// <returns>The fixed test-context GUID.</returns>
public override Guid NewGuid() => Guid.Parse("00000000-0000-0000-0000-000000000001");
/// <summary>Creates a task completion source used to signal recorded-call changes.</summary>
/// <returns>A task completion source that runs continuations asynchronously.</returns>
private static TaskCompletionSource NewSignal()
=> new(TaskCreationOptions.RunContinuationsAsynchronously);
/// <summary>Stores the name and input for a recorded orchestration call.</summary>
/// <param name="name">The call name.</param>
/// <param name="input">The call input.</param>
internal abstract class RecordedCall(TaskName name, object? input)
{
/// <summary>Gets the call name.</summary>
public TaskName Name { get; } = name;
/// <summary>Gets the call input.</summary>
public object? Input { get; } = input;
}
/// <summary>Represents a recorded activity call that a test can complete or fail.</summary>
/// <param name="name">The activity name.</param>
/// <param name="input">The activity input.</param>
/// <param name="options">The activity options.</param>
/// <param name="taskId">The activity task identifier.</param>
internal sealed class ActivityCall<TResult>(TaskName name, object? input, TaskOptions? options, int taskId)
: RecordedCall(name, input)
{
private readonly TaskCompletionSource<TResult> _completion = new(TaskCreationOptions.RunContinuationsAsynchronously);
/// <summary>Gets the activity options.</summary>
public TaskOptions? Options { get; } = options;
/// <summary>Gets the task completed by the test.</summary>
public Task<TResult> Task => _completion.Task;
/// <summary>Gets the failure details supplied when the activity call fails.</summary>
public TaskFailureDetails? FailureDetails { get; private set; }
/// <summary>Completes the activity call successfully.</summary>
/// <param name="result">The activity result.</param>
public void Complete(TResult result) => _completion.TrySetResult(result);
/// <summary>Fails the activity call with the specified exception.</summary>
/// <param name="exception">The activity exception.</param>
public void Fail(Exception exception)
{
FailureDetails = TaskFailureDetails.FromException(exception);
_completion.TrySetException(new TaskFailedException(Name.ToString(), taskId, FailureDetails));
}
}
/// <summary>Represents a recorded sub-orchestration call that a test can complete.</summary>
/// <param name="name">The sub-orchestration name.</param>
/// <param name="input">The sub-orchestration input.</param>
/// <param name="options">The sub-orchestration options.</param>
internal sealed class SubOrchestrationCall<TResult>(TaskName name, object? input, TaskOptions? options)
: RecordedCall(name, input)
{
private readonly TaskCompletionSource<TResult> _completion = new(TaskCreationOptions.RunContinuationsAsynchronously);
/// <summary>Gets the sub-orchestration options.</summary>
public TaskOptions? Options { get; } = options;
/// <summary>Gets the task completed by the test.</summary>
public Task<TResult> Task => _completion.Task;
/// <summary>Completes the sub-orchestration call successfully.</summary>
/// <param name="result">The sub-orchestration result.</param>
public void Complete(TResult result) => _completion.TrySetResult(result);
}
}