-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathDurableControllerTests.cs
377 lines (315 loc) · 17.8 KB
/
DurableControllerTests.cs
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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
namespace Microsoft.Azure.Functions.PowerShellWorker.Test.Durable
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
using Microsoft.Azure.Functions.PowerShellWorker.DurableWorker;
using Microsoft.Azure.Functions.PowerShellWorker.Durable;
using Microsoft.Azure.Functions.PowerShellWorker.Utility;
using Newtonsoft.Json;
using Moq;
using Xunit;
using Grpc.Core;
public class DurableControllerTests
{
private readonly Mock<IPowerShellServices> _mockPowerShellServices = new Mock<IPowerShellServices>(MockBehavior.Strict);
private readonly Mock<IOrchestrationInvoker> _mockOrchestrationInvoker = new Mock<IOrchestrationInvoker>(MockBehavior.Strict);
private const string _contextParameterName = "ParameterName";
private static readonly OrchestrationContext _orchestrationContext = new OrchestrationContext { InstanceId = Guid.NewGuid().ToString() };
private static readonly OrchestrationBindingInfo _orchestrationBindingInfo = new OrchestrationBindingInfo(_contextParameterName, _orchestrationContext);
private static readonly ILogger _testLogger = new ConsoleLogger();
[Fact]
public void InitializeBindings_SetsDurableClient_ForDurableClientFunction()
{
var durableController = CreateDurableController(DurableFunctionType.None, "DurableClientBindingName");
var durableClient = new { FakeClientProperty = "FakeClientPropertyValue" };
var inputData = new[]
{
CreateParameterBinding("AnotherParameter", "IgnoredValue"),
CreateParameterBinding("DurableClientBindingName", durableClient),
CreateParameterBinding("YetAnotherParameter", "IgnoredValue")
};
_mockPowerShellServices.Setup(_ => _.SetDurableClient(It.IsAny<object>()));
_mockPowerShellServices.Setup(_ => _.HasExternalDurableSDK()).Returns(false);
_mockPowerShellServices.Setup(_ => _.EnableExternalDurableSDK());
_mockPowerShellServices.Setup(_ => _.isExternalDurableSdkLoaded()).Returns(false);
durableController.InitializeBindings(inputData, out bool hasExternalSDK);
_mockPowerShellServices.Verify(
_ => _.SetDurableClient(
It.Is<object>(c => (string)((Hashtable)c)["FakeClientProperty"] == durableClient.FakeClientProperty)),
Times.Once);
}
[Fact]
public void InitializeBindings_SetsOrchestrationContext_ForOrchestrationFunction()
{
var durableController = CreateDurableController(DurableFunctionType.OrchestrationFunction);
var inputData = new[]
{
CreateParameterBinding("ParameterName", _orchestrationContext)
};
_mockPowerShellServices.Setup(_ => _.SetOrchestrationContext(It.IsAny<ParameterBinding>(),
out It.Ref<IExternalOrchestrationInvoker>.IsAny))
.Returns(_orchestrationBindingInfo);
_mockOrchestrationInvoker.Setup(_ => _.SetExternalInvoker(It.IsAny<IExternalOrchestrationInvoker>()));
_mockPowerShellServices.Setup(_ => _.HasExternalDurableSDK()).Returns(false);
_mockPowerShellServices.Setup(_ => _.EnableExternalDurableSDK());
_mockPowerShellServices.Setup(_ => _.isExternalDurableSdkLoaded()).Returns(false);
durableController.InitializeBindings(inputData, out bool hasExternalSDK);
_mockPowerShellServices.Verify(
_ => _.SetOrchestrationContext(
It.Is<ParameterBinding>(c => c.Data.ToString().Contains(_orchestrationContext.InstanceId)),
out It.Ref<IExternalOrchestrationInvoker>.IsAny),
Times.Once);
}
[Fact]
public void InitializeBindings_Throws_OnOrchestrationFunctionWithoutContextParameter()
{
var durableController = CreateDurableController(DurableFunctionType.OrchestrationFunction);
var inputData = new ParameterBinding[0];
_mockPowerShellServices.Setup(_ => _.HasExternalDurableSDK()).Returns(false);
_mockPowerShellServices.Setup(_ => _.EnableExternalDurableSDK());
_mockPowerShellServices.Setup(_ => _.isExternalDurableSdkLoaded()).Returns(false);
Assert.ThrowsAny<ArgumentException>(() => durableController.InitializeBindings(inputData, out bool hasExternalSDK));
}
[Theory]
[InlineData(DurableFunctionType.None)]
[InlineData(DurableFunctionType.ActivityFunction)]
internal void InitializeBindings_DoesNothing_ForNonOrchestrationFunction(DurableFunctionType durableFunctionType)
{
var durableController = CreateDurableController(durableFunctionType);
var inputData = new[]
{
// Even if a parameter similar to orchestration context is passed:
CreateParameterBinding("ParameterName", _orchestrationContext)
};
_mockPowerShellServices.Setup(_ => _.HasExternalDurableSDK()).Returns(false);
_mockPowerShellServices.Setup(_ => _.EnableExternalDurableSDK());
_mockPowerShellServices.Setup(_ => _.isExternalDurableSdkLoaded()).Returns(false);
durableController.InitializeBindings(inputData, out bool hasExternalSDK);
}
[Theory]
[InlineData(DurableFunctionType.None)]
[InlineData(DurableFunctionType.ActivityFunction)]
[InlineData(DurableFunctionType.OrchestrationFunction)]
internal void AfterFunctionInvocation_ClearsOrchestrationContext(DurableFunctionType durableFunctionType)
{
var durableController = CreateDurableController(durableFunctionType);
_mockPowerShellServices.Setup(_ => _.ClearOrchestrationContext());
durableController.AfterFunctionInvocation();
_mockPowerShellServices.Verify(_ => _.ClearOrchestrationContext(), Times.Once);
}
[Fact]
public void TryGetInputBindingParameterValue_RetrievesOrchestrationContextParameter_ForOrchestrationFunction()
{
var durableController = CreateDurableController(DurableFunctionType.OrchestrationFunction);
var inputData = new[]
{
CreateParameterBinding(_contextParameterName, _orchestrationContext)
};
_mockPowerShellServices.Setup(_ => _.SetOrchestrationContext(
It.IsAny<ParameterBinding>(),
out It.Ref<IExternalOrchestrationInvoker>.IsAny))
.Returns(_orchestrationBindingInfo);
_mockPowerShellServices.Setup(_ => _.HasExternalDurableSDK()).Returns(false);
_mockPowerShellServices.Setup(_ => _.EnableExternalDurableSDK());
_mockPowerShellServices.Setup(_ => _.isExternalDurableSdkLoaded()).Returns(false);
_mockOrchestrationInvoker.Setup(_ => _.SetExternalInvoker(It.IsAny<IExternalOrchestrationInvoker>()));
durableController.InitializeBindings(inputData, out bool hasExternalSDK);
Assert.True(durableController.TryGetInputBindingParameterValue(_contextParameterName, out var value));
Assert.Equal(_orchestrationContext.InstanceId, ((OrchestrationContext)value).InstanceId);
}
[Theory]
[InlineData(DurableFunctionType.None)]
[InlineData(DurableFunctionType.ActivityFunction)]
internal void TryGetInputBindingParameterValue_RetrievesNothing_ForNonOrchestrationFunction(DurableFunctionType durableFunctionType)
{
var durableController = CreateDurableController(durableFunctionType);
var inputData = new[]
{
CreateParameterBinding(_contextParameterName, _orchestrationContext)
};
_mockPowerShellServices.Setup(_ => _.SetOrchestrationContext(
It.IsAny<ParameterBinding>(),
out It.Ref<IExternalOrchestrationInvoker>.IsAny))
.Returns(_orchestrationBindingInfo);
_mockPowerShellServices.Setup(_ => _.HasExternalDurableSDK()).Returns(false);
_mockPowerShellServices.Setup(_ => _.EnableExternalDurableSDK());
_mockPowerShellServices.Setup(_ => _.isExternalDurableSdkLoaded()).Returns(false);
_mockOrchestrationInvoker.Setup(_ => _.SetExternalInvoker(It.IsAny<IExternalOrchestrationInvoker>()));
durableController.InitializeBindings(inputData, out bool hasExternalSDK);
Assert.False(durableController.TryGetInputBindingParameterValue(_contextParameterName, out var value));
Assert.Null(value);
}
[Fact]
public void TryInvokeOrchestrationFunction_InvokesOrchestrationFunction()
{
var inputData = new[] { CreateParameterBinding(_contextParameterName, _orchestrationContext) };
var durableController = CreateDurableController(DurableFunctionType.OrchestrationFunction);
_mockPowerShellServices.Setup(_ => _.SetOrchestrationContext(
It.IsAny<ParameterBinding>(),
out It.Ref<IExternalOrchestrationInvoker>.IsAny))
.Returns(_orchestrationBindingInfo);
_mockPowerShellServices.Setup(_ => _.HasExternalDurableSDK()).Returns(false);
_mockPowerShellServices.Setup(_ => _.EnableExternalDurableSDK());
_mockPowerShellServices.Setup(_ => _.isExternalDurableSdkLoaded()).Returns(false);
_mockOrchestrationInvoker.Setup(_ => _.SetExternalInvoker(It.IsAny<IExternalOrchestrationInvoker>()));
durableController.InitializeBindings(inputData, out bool hasExternalSDK);
var expectedResult = new Hashtable();
_mockOrchestrationInvoker.Setup(
_ => _.Invoke(It.IsAny<OrchestrationBindingInfo>(), It.IsAny<IPowerShellServices>()))
.Returns(expectedResult);
var actualResult = durableController.InvokeOrchestrationFunction();
Assert.Same(expectedResult, actualResult);
_mockOrchestrationInvoker.Verify(
_ => _.Invoke(
It.Is<OrchestrationBindingInfo>(
bindingInfo => bindingInfo.Context.InstanceId == _orchestrationContext.InstanceId
&& bindingInfo.ParameterName == _contextParameterName),
_mockPowerShellServices.Object),
Times.Once);
}
[Fact]
public void AddPipelineOutputIfNecessary_AddsDollarReturn_ForActivityFunction()
{
var durableController = CreateDurableController(DurableFunctionType.ActivityFunction);
var pipelineItems = new Collection<object> { "Item1", "Item2", "Item3" };
var result = new Hashtable { { "FieldA", "ValueA" }, { "FieldB", "ValueB" } };
var originalResultCount = result.Count;
durableController.AddPipelineOutputIfNecessary(pipelineItems, result);
Assert.Equal(originalResultCount + 1, result.Count);
var dollarReturnValue = result[AzFunctionInfo.DollarReturn];
Assert.Equal(
(IEnumerable<object>)dollarReturnValue,
FunctionReturnValueBuilder.CreateReturnValueFromFunctionOutput(pipelineItems));
}
[Theory]
[InlineData(DurableFunctionType.None)]
[InlineData(DurableFunctionType.OrchestrationFunction)]
internal void AddPipelineOutputIfNecessary_DoesNotAddDollarReturn_ForNonActivityFunction(DurableFunctionType durableFunctionType)
{
var durableController = CreateDurableController(durableFunctionType);
var pipelineItems = new Collection<object> { "Item1", "Item2", "Item3" };
var result = new Hashtable { { "FieldA", "ValueA" }, { "FieldB", "ValueB" } };
durableController.AddPipelineOutputIfNecessary(pipelineItems, result);
Assert.False(result.ContainsKey(AzFunctionInfo.DollarReturn));
}
[Theory]
[InlineData(DurableFunctionType.None, false)]
[InlineData(DurableFunctionType.OrchestrationFunction, false)]
[InlineData(DurableFunctionType.ActivityFunction, true)]
internal void SuppressPipelineTracesForActivityFunctionOnly(DurableFunctionType durableFunctionType, bool shouldSuppressPipelineTraces)
{
var durableController = CreateDurableController(durableFunctionType);
Assert.Equal(shouldSuppressPipelineTraces, durableController.ShouldSuppressPipelineTraces());
}
[Theory]
[InlineData(DurableFunctionType.None)]
[InlineData(DurableFunctionType.OrchestrationFunction)]
internal void ExternalDurableSdkIsNotConfiguredByDefault(DurableFunctionType durableFunctionType)
{
var durableController = CreateDurableController(durableFunctionType);
var inputData = GetDurableBindings(durableFunctionType);
if (durableFunctionType == DurableFunctionType.None)
{
_mockPowerShellServices.Setup(_ => _.SetDurableClient(It.IsAny<object>()));
}
else
{
_mockPowerShellServices.Setup(_ => _.SetOrchestrationContext(
It.IsAny<ParameterBinding>(),
out It.Ref<IExternalOrchestrationInvoker>.IsAny)).Returns(_orchestrationBindingInfo);
_mockOrchestrationInvoker.Setup(_ => _.SetExternalInvoker(It.IsAny<IExternalOrchestrationInvoker>()));
}
_mockPowerShellServices.Setup(_ => _.HasExternalDurableSDK()).Returns(false);
_mockPowerShellServices.Setup(_ => _.EnableExternalDurableSDK()).Throws(new Exception("should not be called"));
_mockPowerShellServices.Setup(_ => _.isExternalDurableSdkLoaded()).Returns(false);
durableController.InitializeBindings(inputData, out var hasExternalSDK);
Assert.False(hasExternalSDK);
_mockPowerShellServices.Verify(_ => _.EnableExternalDurableSDK(), Times.Never);
}
[Theory]
[InlineData(DurableFunctionType.None)]
[InlineData(DurableFunctionType.OrchestrationFunction)]
internal void ExternalDurableSdkCanBeEnabled(DurableFunctionType durableFunctionType)
{
try
{
// opt-in to external DF SDK
Environment.SetEnvironmentVariable("ExternalDurablePowerShellSDK", "true");
var durableController = CreateDurableController(durableFunctionType);
var inputData = GetDurableBindings(durableFunctionType);
if (durableFunctionType == DurableFunctionType.None)
{
_mockPowerShellServices.Setup(_ => _.SetDurableClient(It.IsAny<object>()));
}
else
{
_mockPowerShellServices.Setup(_ => _.SetOrchestrationContext(
It.IsAny<ParameterBinding>(),
out It.Ref<IExternalOrchestrationInvoker>.IsAny)).Returns(_orchestrationBindingInfo);
_mockOrchestrationInvoker.Setup(_ => _.SetExternalInvoker(It.IsAny<IExternalOrchestrationInvoker>()));
}
_mockPowerShellServices.Setup(_ => _.HasExternalDurableSDK()).Returns(true);
_mockPowerShellServices.Setup(_ => _.EnableExternalDurableSDK());
_mockPowerShellServices.Setup(_ => _.isExternalDurableSdkLoaded()).Returns(true);
durableController.InitializeBindings(inputData, out var hasExternalSDK);
Assert.True(hasExternalSDK);
_mockPowerShellServices.Verify(_ => _.EnableExternalDurableSDK(), Times.Once);
}
finally
{
Environment.SetEnvironmentVariable("ExternalDurablePowerShellSDK", "false");
}
}
private DurableController CreateDurableController(
DurableFunctionType durableFunctionType,
string durableClientBindingName = null)
{
var durableFunctionInfo = new DurableFunctionInfo(durableFunctionType, durableClientBindingName);
return new DurableController(
durableFunctionInfo,
_mockPowerShellServices.Object,
_mockOrchestrationInvoker.Object,
_testLogger);
}
private static ParameterBinding CreateParameterBinding(string parameterName, object value)
{
return new ParameterBinding
{
Name = parameterName,
Data = new TypedData
{
String = JsonConvert.SerializeObject(value)
}
};
}
private static IList<ParameterBinding> GetDurableBindings(DurableFunctionType durableFunctionType)
{
ParameterBinding[] bindings;
if (durableFunctionType == DurableFunctionType.None) // DF Client case
{
var durableClient = new { FakeClientProperty = "FakeClientPropertyValue" };
bindings = new[]
{
CreateParameterBinding("AnotherParameter", "IgnoredValue"),
CreateParameterBinding("DurableClientBindingName", durableClient),
CreateParameterBinding("YetAnotherParameter", "IgnoredValue")
};
}
else //valid for orchestrators and acitvities
{
bindings = new[]
{
CreateParameterBinding("ParameterName", _orchestrationContext)
};
}
return bindings;
}
}
}