-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathDispatcherMiddlewareTests.cs
More file actions
542 lines (458 loc) · 24.3 KB
/
Copy pathDispatcherMiddlewareTests.cs
File metadata and controls
542 lines (458 loc) · 24.3 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------
#if !NET48 // for some reasons these tests are not discoverable on 1ES, leading to the test getting aborted. TODO: Needs investigation
#nullable enable
namespace DurableTask.Core.Tests
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using DurableTask.Core.Command;
using DurableTask.Core.History;
using DurableTask.Core.Tracing;
using DurableTask.Emulator;
using DurableTask.Test.Orchestrations;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DiagnosticsActivityStatusCode = System.Diagnostics.ActivityStatusCode;
using TraceActivityStatusCode = DurableTask.Core.Tracing.ActivityStatusCode;
[TestClass]
public class DispatcherMiddlewareTests
{
TaskHubWorker worker = null!;
TaskHubClient client = null!;
[TestInitialize]
public void InitializeTests()
{
// configure logging so traces are emitted during tests.
// This facilitates debugging when tests fail.
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole().SetMinimumLevel(LogLevel.Trace);
});
var service = new LocalOrchestrationService();
this.worker = new TaskHubWorker(service, loggerFactory);
// We use `GetAwaiter().GetResult()` because otherwise this method will fail with:
// "X has wrong signature. The method must be non-static, public, does not return a value and should not take any parameter."
this.worker
.AddTaskOrchestrations(
typeof(SimplestGreetingsOrchestration),
typeof(ParentWorkflow),
typeof(ChildWorkflow),
typeof(ActivityStatusResetOrchestration))
.AddTaskActivities(
typeof(SimplestGetUserTask),
typeof(SimplestSendGreetingTask),
typeof(ActivityStatusResetActivity))
.StartAsync().GetAwaiter().GetResult();
this.client = new TaskHubClient(service);
}
[TestCleanup]
public void CleanupTests()
{
// We use `GetAwaiter().GetResult()` because otherwise this method will fail with:
// "X has wrong signature. The method must be non-static, public, does not return a value and should not take any parameter."
this.worker!.StopAsync(true).GetAwaiter().GetResult();
}
[TestMethod]
public async Task DispatchMiddlewareContextBuiltInProperties()
{
TaskOrchestration? orchestration = null;
OrchestrationRuntimeState? state = null;
OrchestrationInstance? instance1 = null;
OrchestrationExecutionContext? executionContext1 = null;
TaskActivity? activity = null;
TaskScheduledEvent? taskScheduledEvent = null;
OrchestrationInstance? instance2 = null;
OrchestrationExecutionContext? executionContext2 = null;
this.worker.AddOrchestrationDispatcherMiddleware((context, next) =>
{
orchestration = context.GetProperty<TaskOrchestration>();
state = context.GetProperty<OrchestrationRuntimeState>();
instance1 = context.GetProperty<OrchestrationInstance>();
executionContext1 = context.GetProperty<OrchestrationExecutionContext>();
return next();
});
this.worker.AddActivityDispatcherMiddleware((context, next) =>
{
activity = context.GetProperty<TaskActivity>();
taskScheduledEvent = context.GetProperty<TaskScheduledEvent>();
instance2 = context.GetProperty<OrchestrationInstance>();
executionContext2 = context.GetProperty<OrchestrationExecutionContext>();
return next();
});
OrchestrationInstance instance = await this.client.CreateOrchestrationInstanceAsync(typeof(SimplestGreetingsOrchestration), null);
TimeSpan timeout = TimeSpan.FromSeconds(Debugger.IsAttached ? 1000 : 10);
await this.client.WaitForOrchestrationAsync(instance, timeout);
Assert.IsNotNull(orchestration);
Assert.IsNotNull(state);
Assert.IsNotNull(instance1);
Assert.IsNotNull(executionContext1);
Assert.IsNotNull(activity);
Assert.IsNotNull(taskScheduledEvent);
Assert.IsNotNull(instance2);
Assert.IsNotNull(executionContext2);
Assert.AreNotSame(instance1, instance2);
Assert.AreEqual(instance1!.InstanceId, instance2!.InstanceId);
}
[TestMethod]
public async Task OrchestrationDispatcherMiddlewareContextFlow()
{
StringBuilder? output = null;
for (var i = 0; i < 10; i++)
{
string value = i.ToString();
this.worker.AddOrchestrationDispatcherMiddleware(async (context, next) =>
{
output = context.GetProperty<StringBuilder>("output");
if (output == null)
{
output = new StringBuilder();
context.SetProperty("output", output);
}
// This is an async method and the output is out of the scope, output is used in closure
Debug.Assert(output != null);
output!.Append(value);
await next();
output.Append(value);
});
}
OrchestrationInstance instance = await this.client.CreateOrchestrationInstanceAsync(typeof(SimplestGreetingsOrchestration), null);
TimeSpan timeout = TimeSpan.FromSeconds(Debugger.IsAttached ? 1000 : 10);
await this.client.WaitForOrchestrationAsync(instance, timeout);
// Each reply gets a new context, so the output should stay the same regardless of how
// many replays an orchestration goes through.
Assert.IsNotNull(output);
Assert.AreEqual("01234567899876543210", output?.ToString());
}
[TestMethod]
public async Task ActivityDispatcherMiddlewareContextFlow()
{
StringBuilder? output = null;
for (var i = 0; i < 10; i++)
{
string value = i.ToString();
this.worker.AddActivityDispatcherMiddleware(async (context, next) =>
{
output = context.GetProperty<StringBuilder>("output");
if (output == null)
{
output = new StringBuilder();
context.SetProperty("output", output);
}
// This is an async method and the output is out of the scope, output is used in closure
Debug.Assert(output != null);
output!.Append(value);
await next();
output.Append(value);
});
}
OrchestrationInstance instance = await this.client.CreateOrchestrationInstanceAsync(typeof(SimplestGreetingsOrchestration), null);
TimeSpan timeout = TimeSpan.FromSeconds(Debugger.IsAttached ? 1000 : 10);
await this.client.WaitForOrchestrationAsync(instance, timeout);
// Each activity gets a new context, so the output should stay the same regardless of how
// many activities an orchestration schedules (as long as there is at least one).
Assert.IsNotNull(output);
Assert.AreEqual("01234567899876543210", output?.ToString());
}
[TestMethod]
public async Task EnsureOrchestrationDispatcherMiddlewareHasAccessToRuntimeState()
{
OrchestrationExecutionContext? executionContext = null;
for (var i = 0; i < 10; i++)
{
string value = i.ToString();
this.worker.AddOrchestrationDispatcherMiddleware(async (context, next) =>
{
executionContext = context.GetProperty<OrchestrationExecutionContext>();
await next();
});
}
OrchestrationInstance instance = await this.client.CreateOrchestrationInstanceAsync(
NameVersionHelper.GetDefaultName(typeof(SimplestGreetingsOrchestration)),
NameVersionHelper.GetDefaultVersion(typeof(SimplestGreetingsOrchestration)),
"testInstanceId",
null,
new Dictionary<string, string>
{
{ "Test", "Value" }
});
TimeSpan timeout = TimeSpan.FromSeconds(Debugger.IsAttached ? 1000 : 10);
await this.client.WaitForOrchestrationAsync(instance, timeout);
// Each activity gets a new context, so the output should stay the same regardless of how
// many activities an orchestration schedules (as long as there is at least one).
Assert.IsNotNull(executionContext);
Assert.AreEqual("Value", executionContext?.OrchestrationTags?["Test"]);
}
/// <summary>
/// Test to ensure <see cref="OrchestrationExecutionContext"/> supports DataContract serialization.
/// </summary>
[TestMethod]
public void EnsureOrchestrationExecutionContextSupportsDataContractSerialization()
{
OrchestrationExecutionContext orchestrationExecutionContext = new OrchestrationExecutionContext
{
OrchestrationTags = new Dictionary<string, string> { { "Key", "Value" } }
};
DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(OrchestrationExecutionContext));
StringBuilder stringBuilder = new StringBuilder();
XmlWriter xmlWriter = XmlWriter.Create(stringBuilder);
dataContractSerializer.WriteObject(xmlWriter, orchestrationExecutionContext);
xmlWriter.Close();
string writtenString = stringBuilder.ToString();
Assert.AreEqual(
"<?xml version=\"1.0\" encoding=\"utf-16\"?><OrchestrationExecutionContext xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/DurableTask.Core\"><OrchestrationTags xmlns:d2p1=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\"><d2p1:KeyValueOfstringstring><d2p1:Key>Key</d2p1:Key><d2p1:Value>Value</d2p1:Value></d2p1:KeyValueOfstringstring></OrchestrationTags></OrchestrationExecutionContext>",
writtenString);
StringReader stringReader = new StringReader(writtenString);
XmlReader xmlReader = XmlReader.Create(stringReader);
OrchestrationExecutionContext deserializedContext = (OrchestrationExecutionContext)dataContractSerializer.ReadObject(xmlReader)!;
Assert.AreEqual("Value", deserializedContext.OrchestrationTags["Key"]);
}
[TestMethod]
public async Task EnsureSubOrchestrationDispatcherMiddlewareHasAccessToRuntimeState()
{
ConcurrentBag<OrchestrationExecutionContext> capturedContexts = new ConcurrentBag<OrchestrationExecutionContext>();
for (var i = 0; i < 10; i++)
{
string value = i.ToString();
this.worker.AddOrchestrationDispatcherMiddleware(async (context, next) =>
{
capturedContexts.Add(context.GetProperty<OrchestrationExecutionContext>());
await next();
});
}
OrchestrationInstance instance = await this.client.CreateOrchestrationInstanceAsync(
NameVersionHelper.GetDefaultName(typeof(ParentWorkflow)),
NameVersionHelper.GetDefaultVersion(typeof(ParentWorkflow)),
"testInstanceId",
false,
new Dictionary<string, string>
{
{ "Test", "Value" }
});
TimeSpan timeout = TimeSpan.FromSeconds(Debugger.IsAttached ? 1000 : 10);
await this.client.WaitForOrchestrationAsync(instance, timeout);
foreach (OrchestrationExecutionContext context in capturedContexts)
{
Assert.IsNotNull(context);
Assert.AreEqual("Value", context?.OrchestrationTags?["Test"]);
}
}
[TestMethod]
public async Task EnsureActivityDispatcherMiddlewareHasAccessToRuntimeState()
{
OrchestrationExecutionContext? executionContext = null;
for (var i = 0; i < 10; i++)
{
string value = i.ToString();
this.worker.AddActivityDispatcherMiddleware(async (context, next) =>
{
executionContext = context.GetProperty<OrchestrationExecutionContext>();
await next();
});
}
OrchestrationInstance instance = await this.client.CreateOrchestrationInstanceAsync(
NameVersionHelper.GetDefaultName(typeof(SimplestGreetingsOrchestration)),
NameVersionHelper.GetDefaultVersion(typeof(SimplestGreetingsOrchestration)),
"testInstanceId",
null,
new Dictionary<string, string>
{
{ "Test", "Value" }
});
TimeSpan timeout = TimeSpan.FromSeconds(Debugger.IsAttached ? 1000 : 10);
await this.client.WaitForOrchestrationAsync(instance, timeout);
// Each activity gets a new context, so the output should stay the same regardless of how
// many activities an orchestration schedules (as long as there is at least one).
Assert.IsNotNull(executionContext);
Assert.AreEqual("Value", executionContext?.OrchestrationTags?["Test"]);
}
[DataTestMethod]
[DataRow(OrchestrationStatus.Completed)]
[DataRow(OrchestrationStatus.Failed)]
[DataRow(OrchestrationStatus.Terminated)]
public async Task MockOrchestrationCompletion(OrchestrationStatus forcedStatus)
{
TaskOrchestration? orchestration = null;
this.worker.AddOrchestrationDispatcherMiddleware((context, next) =>
{
// Expecting a null here since "FakeName"/"FakeVersion" doesn't exist
orchestration = context.GetProperty<TaskOrchestration>();
context.SetProperty(new OrchestratorExecutionResult
{
CustomStatus = "custom",
Actions = new[]
{
new OrchestrationCompleteOrchestratorAction
{
OrchestrationStatus = forcedStatus,
},
},
});
// don't call next() - we're short-circuiting the actual orchestration executor logic
return Task.FromResult(0);
});
// We can safely use non-existing orchestrator names because the orchestration executor gets short-circuited.
// This allows middleware the flexibility to invent their own dynamic orchestration logic.
// A practical application of this is out-of-process orchestrator execution.
OrchestrationInstance instance = await this.client.CreateOrchestrationInstanceAsync(
name: "FakeName",
version: "FakeVersion",
input: null);
TimeSpan timeout = TimeSpan.FromSeconds(Debugger.IsAttached ? 1000 : 5);
OrchestrationState state = await this.client.WaitForOrchestrationAsync(instance, timeout);
Assert.AreEqual(forcedStatus, state.OrchestrationStatus);
Assert.AreEqual("custom", state.Status);
Assert.IsNull(orchestration, "Expected a null orchestration object in the middleware");
}
[TestMethod]
public async Task MockActivityOrchestration()
{
// Orchestrator middleware mocks an orchestration that calls one activity
// and returns the activity result as its own result
this.worker.AddOrchestrationDispatcherMiddleware((context, next) =>
{
OrchestrationRuntimeState state = context.GetProperty<OrchestrationRuntimeState>();
if (state.NewEvents.OfType<ExecutionStartedEvent>().Any())
{
// Manually schedule an activity execution
context.SetProperty(new OrchestratorExecutionResult
{
Actions = new[]
{
new ScheduleTaskOrchestratorAction
{
Name = "FakeActivity",
Version = "FakeActivityVersion",
Input = "SomeInput",
}
}
});
}
else
{
// If we get here, it's because the activity completed
TaskCompletedEvent taskCompletedEvent = state.NewEvents.OfType<TaskCompletedEvent>().Single();
// We know the activity completed at this point
context.SetProperty(new OrchestratorExecutionResult
{
Actions = new[]
{
new OrchestrationCompleteOrchestratorAction
{
OrchestrationStatus = OrchestrationStatus.Completed,
Result = taskCompletedEvent?.Result,
},
},
});
}
// don't call next() - we're short-circuiting the actual orchestration executor logic
return Task.FromResult(0);
});
// Activity middleware returns a result immediately
this.worker.AddActivityDispatcherMiddleware((context, next) =>
{
TaskScheduledEvent taskScheduledEvent = context.GetProperty<TaskScheduledEvent>();
// Use the activity parameters as the activity output
string output = $"{taskScheduledEvent.Name},{taskScheduledEvent.Version},{taskScheduledEvent.Input}";
context.SetProperty(new ActivityExecutionResult
{
ResponseEvent = new TaskCompletedEvent(-1, taskScheduledEvent.EventId, output),
});
// don't call next() - we're short-circuiting the actual activity executor logic
return Task.FromResult(0);
});
OrchestrationInstance instance = await this.client.CreateOrchestrationInstanceAsync(
name: "FakeName",
version: "FakeVersion",
input: null);
TimeSpan timeout = TimeSpan.FromSeconds(Debugger.IsAttached ? 1000 : 5);
OrchestrationState state = await this.client.WaitForOrchestrationAsync(instance, timeout);
Assert.AreEqual(OrchestrationStatus.Completed, state.OrchestrationStatus);
Assert.AreEqual("FakeActivity,FakeActivityVersion,SomeInput", state.Output);
}
[TestMethod]
public async Task ActivityAndOrchestrationSpansResetStatuses()
{
using var activityCollector = new DurableActivityCollector();
OrchestrationInstance instance = await this.client.CreateOrchestrationInstanceAsync(
typeof(ActivityStatusResetOrchestration),
"payload");
TimeSpan timeout = TimeSpan.FromSeconds(Debugger.IsAttached ? 1000 : 10);
OrchestrationState finalState = await this.client.WaitForOrchestrationAsync(instance, timeout);
Assert.AreEqual(OrchestrationStatus.Completed, finalState.OrchestrationStatus);
string orchestrationName = NameVersionHelper.GetDefaultName(typeof(ActivityStatusResetOrchestration));
Activity? orchestrationActivity = activityCollector.Find(TraceActivityConstants.Orchestration, orchestrationName, ActivityKind.Server);
Assert.IsNotNull(orchestrationActivity, "Expected orchestration server trace to be captured.");
Assert.AreEqual(DiagnosticsActivityStatusCode.Ok, orchestrationActivity!.Status);
string activityName = NameVersionHelper.GetDefaultName(typeof(ActivityStatusResetActivity));
Activity? activitySpan = activityCollector.Find(TraceActivityConstants.Activity, activityName, ActivityKind.Server);
Assert.IsNotNull(activitySpan, "Expected activity server trace to be captured.");
Assert.AreEqual(DiagnosticsActivityStatusCode.Ok, activitySpan!.Status);
}
private sealed class DurableActivityCollector : IDisposable
{
private readonly ActivityListener listener;
private readonly ConcurrentBag<Activity> activities = new ConcurrentBag<Activity>();
public DurableActivityCollector()
{
this.listener = new ActivityListener
{
ShouldListenTo = source => source.Name == "DurableTask.Core",
Sample = (ref ActivityCreationOptions<ActivityContext> _) => ActivitySamplingResult.AllData,
SampleUsingParentId = (ref ActivityCreationOptions<string> _) => ActivitySamplingResult.AllData,
ActivityStopped = activity => this.activities.Add(activity),
};
ActivitySource.AddActivityListener(this.listener);
}
public Activity? Find(string taskType, string taskName, ActivityKind kind)
{
return this.activities
.Where(activity => activity.Kind == kind)
.Where(activity => string.Equals(Convert.ToString(activity.GetTagItem(Schema.Task.Type)), taskType, StringComparison.Ordinal))
.Where(activity => string.Equals(Convert.ToString(activity.GetTagItem(Schema.Task.Name)), taskName, StringComparison.Ordinal))
.LastOrDefault();
}
public void Dispose()
{
this.listener.Dispose();
}
}
private sealed class ActivityStatusResetOrchestration : TaskOrchestration<string, string>
{
public override async Task<string> RunTask(OrchestrationContext context, string input)
{
DistributedTraceActivity.Current?.SetStatus(TraceActivityStatusCode.Error, "orchestration instrumentation error");
string? activityOutput = await context.ScheduleTask<string>(typeof(ActivityStatusResetActivity), input ?? "ok");
return activityOutput ?? string.Empty;
}
}
private sealed class ActivityStatusResetActivity : TaskActivity<string, string>
{
protected override string Execute(TaskContext context, string input)
{
Activity.Current?.SetStatus(TraceActivityStatusCode.Error, "activity instrumentation error");
return input ?? "ok";
}
}
}
}
#endif