【发布时间】:2022-01-10 12:03:09
【问题描述】:
我已经使用 NodeJs 构建了一个 azure 函数。使用 Jest 进行测试。
场景: 从函数进行 API 调用时,如果第 3 方返回 Timeout,我需要再重试 1 次然后退出。这工作正常,服务器正在自动重试。
逻辑:
函数.json
"retry": {
"strategy": "exponentialBackoff",
"maxRetryCount": 1,
"minimumInterval": "00:00:10",
"maximumInterval": "00:00:40"
}
index.js - 在 try/catch 下
catch (err) {
let errorCode = err.errorCode || err.code || 500;
let errorMessage = err.message|| err.errorMessage;
if (errorMessage && errorMessage.indexOf("ETIMEDOUT") >= 0 ) {
errorCode = 429; //setting errorCode as 429 to retry automatically from azure function
let retryError = new Error("retrying requests");
retryError.code = 429;
throw retryError;
}else{
context.done();
}
};
index.test.js
测试套件无法运行 重试请求
一旦从主类抛出错误,测试类会在“let retryError = new Error("retrying requests");”这一行中断。
那么,如果服务器重试耗尽,仍然响应为 429,如何编写测试用例?
【问题讨论】:
标签: node.js jestjs azure-functions retry-logic