node.js - 错误: async hook stack has become corrupted in node js

标签 node.js unit-testing mocha.js

我正在使用 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/

相关文章:

node.js - 如何使用 node-http-proxy 启用 HSTS?

javascript - 使用 Chartist 在 x 轴显示长标签

python - 从python客户端发送加密消息到nodejs服务器

node.js - 仅限移动设备 CORS 错误 : Origin is not allowed by Access-Control-Allow-Origin In Node. js

typescript - 如何更新 React/TypeScript 项目中的快照?

ios - 为什么 UIView 在手动实例化到模拟 UIViewController 后为 nil?

c# - 如何对 try 主体中具有系统调用的异常处理程序进行单元测试?

javascript - 如何使用正则表达式匹配文件名来运行 mocha 测试?

mocha.js - mocha 中的 'language chain' 方法实际上有什么作用吗?

node.js - 关于使用 Node 和 mongo 迁移到 TDD