我已经阅读了这两个关于单元测试和错误处理的 Microsoft 文档
单元测试: https ://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-unit-testing
我在单元测试中尝试执行以下操作:
1)有活动抛出异常
2) 验证 Orchestrator 将CallActivityRetryAsync根据我的重试选项进行调用,例如 3 次
3) 验证我的 try/catch 块捕获异常一次并调用我的错误处理程序。(旁白:异常是否包含在一个FunctionFailedException?)
因此,例如,根据 MS 文档,如果这是我的协调器:
[FunctionName("E1_HelloSequence")]
public static async Task<List<string>> Run(
[OrchestrationTrigger] IDurableOrchestrationContext context)
{
try
{
var outputs = new List<string>();
var retryOptions = new RetryOptions(
firstRetryInterval: TimeSpan.FromSeconds(5),
maxNumberOfAttempts: 3);
outputs.Add(await context.CallActivityRetryAsync<string>("E1_SayHello", retryOptions, "Tokyo"));
outputs.Add(await context.CallActivityRetryAsync<string>("E1_SayHello", retryOptions, "Seattle"));
// returns ["Hello Tokyo!", "Hello Seattle!"]
return outputs;
}
catch (Exception ex)
{
await context.CallActivityAsync<string>("E1_SayHello", "I Failed..."));
throw;
}
}
[FunctionName("E1_SayHello")]
public static string SayHello([ActivityTrigger] IDurableActivityContext context)
{
string name = context.GetInput<string>();
return $"Hello {name}!";
}
我的测试将是这样的:
public class HelloSequenceTests
{
[Fact]
public async Task Run_returns_multiple_greetings()
{
var mockContext = new Mock<IDurableOrchestrationContext>();
mockContext.Setup(x => x.CallActivityRetryAsync<string>("E1_SayHello", "Tokyo")).ReturnsAsync("Hello Tokyo!");
mockContext.Setup(x => x.CallActivityRetryAsync<string>("E1_SayHello", "Seattle")).ReturnsAsync("Hello Seattle!");
var result = await HelloSequence.Run(mockContext.Object);
Assert.Equal(2, result.Count);
Assert.Equal("Hello Tokyo!", result[0]);
Assert.Equal("Hello Seattle!", result[1]);
}
}
所以在上面的例子中,我将如何
1)让ActivityE1_SayHello抛出异常
2) 验证有 3 次重试
3)验证我的catch块执行了一次?