测试同一函数的并发调用

时间:2016-01-18 19:49:33

标签: javascript node.js unit-testing concurrency

我有一个功能:

var inRendering = false;
function render() {
  if (inRendering) {
    requestAnimationFrame(render);
  } else {
    inRendering = true;
    requestAnimationFrame(function () {
      longAction();
      inRendering = false;
    });
  }
}

我必须进行单元测试。测试render的并发调用情况。

请帮帮我?在JavaScript中是否可以进行此类并发调用?感谢。

P.S。我写了一个显然不起作用的测试(见评论):https://gist.github.com/kuraga/b0aa3d66fc0620f03b11

1 个答案:

答案 0 :(得分:1)

您可以使用异步模块,特别是async.parallel,以便并行运行两个函数

例如:

async.parallel([
    function(callback){
        // run your function once
    },
    function(callback){
        // run your function the second time
    }
],
// optional callback
function(err, results){
    // the results array will equal ['one','two'] even though
    // the second function had a shorter timeout.
});