我正在使用 mocha 为我的 Node JS 应用程序编写测试
在我的测试中,我模拟了 1 个函数(正在调用 HTTP Url),并且对于某些条件,我添加了 1 秒的 sleep 。
由于 Mocha 测试中的 sleep 功能,我遇到了异常。实际应用中效果很好。
Error: async hook stack has become corrupted (actual: 18, expected: 19) 1: v8::SnapshotCreator::default constructor closure 2:
node::CallbackScope::~CallbackScope 3:
node::CallbackScope::~CallbackScope 4: RAND_query_egd_bytes 5:
RAND_query_egd_bytes 6: uv_timer_get_repeat 7: uv_run 8:
000007FEF8771261 9: 000007FEF87710B6 10:
v8::internal::wasm::SignatureMap::Find 11:
v8::internal::Builtins::CallableFor 12:
v8::internal::Builtins::CallableFor 13:
v8::internal::Builtins::CallableFor 14: 000002E6363043C1
下面是我的moched函数代码。
somefunction() //In Mock test
{
let current_time = Math.round((new Date()).getTime() / 1000);
if (last_execution_time == current_time) {
admin_delete_user_count++;
if (admin_delete_user_count >= 3) {
callback({
stack: "TooManyRequestsException: Rate exceeded",
"code": "TooManyRequestsException",
"statusCode": 400
}, undefined);
return;
}
} else {
admin_delete_user_count = 0;
last_execution_time = Math.round((new Date()).getTime() / 1000);
}
callback(undefined, "Test");
}
下面是我的 Node js 应用程序中的实际函数,它在我从上面的代码发送异常后导致了问题。
somefunction() // In Real applicatio
{
if (err) {
if (err.code == "TooManyRequestsException") {
logger.log("INFO", "TooManyRequestsException, So wait a while for 1 second and retry");
index = index - 1;
sleep(1000); // THIS IS CAUSING THE ISSUE
}
} else {
console.log("deletedUsers:" + JSON.stringify(deletedUsers));
}
}
有什么帮助吗?
最佳答案
我发现了上面列出的问题。
基本上有 250 毫秒的超时,这导致了异步钩子(Hook)生命周期的问题。
我改变了
timeout.timeout('waitTimer', 250,
function() {
expect(res.statusCode).to.equal(200);
done();
}
);
到
timeout.timeout('waitTimer', 1250,
function() {
expect(res.statusCode).to.equal(200);
done();
}
);
谢谢!
关于node.js - 错误: async hook stack has become corrupted in node js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50664093/