代码之家  ›  专栏  ›  技术社区  ›  Jayesh Dhandha

错误:节点js中的异步钩子堆栈已损坏

  •  1
  • Jayesh Dhandha  · 技术社区  · 6 年前

    我正在为我的node js应用程序编写测试 mocha

    在我的测试中,我模拟了1个函数(它正在调用http url),在某些情况下,我添加了1秒睡眠。

    由于摩卡测试中的睡眠功能,我的异常率低于零。在实际应用中效果良好。

    错误:异步挂钩堆栈已损坏(实际:18,预期: 19)1:V8::SnapshotCreator::默认构造函数闭包2:

    节点::调用范围::~调用范围3:

    节点::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::内部::WASM::签名映射::查找11:

    V8::内部::内置::可调用12:

    V8::内部::内置::可调用13:

    V8::内部::内置::可调用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));
        }
    }
    

    有什么帮助吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Jayesh Dhandha    6 年前

    我发现了上面列出的问题。

    基本上,存在250毫秒的超时,这导致了异步钩子生命周期中的问题。

    我改变了

    timeout.timeout('waitTimer', 250,
       function() {
         expect(res.statusCode).to.equal(200);
         done();
       }
    );
    

    timeout.timeout('waitTimer', 1250,
       function() {
         expect(res.statusCode).to.equal(200);
         done();
       }
    );
    

    谢谢!

    推荐文章