简体   繁体   English

当所包含的应用服务器使用nodejs cluster.fork时,誓言单元测试已多次执行

[英]vows unit test got executed multiple times when the included app server uses nodejs cluster.fork

My app server uses node.js cluster API cluster.fork() to fork multiple child processes. 我的应用服务器使用node.js群集API cluster.fork()派生多个子进程。 This works fine. 这很好。

However, when I try to use vows for unit test, the test also got run multiple times because of the call to cluster.fork() inside my app server; 但是,当我尝试使用誓言进行单元测试时,由于对我的应用服务器内部的cluster.fork()的调用,该测试也多次运行。 as I instantiate the server inside my test code, as follows: 当我在测试代码中实例化服务器时,如下所示:

basic-test.js basic-test.js

var vows = require('vows');
var MyAppServer = require('../my_app');

// start the server 
var app = MyAppServer.start();    

var suite = vows.describe('My Tests');
suite.discuss('connections API')
    .addBatch({ ... })
    .export(module);

How do I prevent test code to run multiple times in this case ? 在这种情况下,如何防止测试代码多次运行? This test is included in npm test, so I need a way to instantiate my app server inside test itself. 该测试包含在npm测试中,因此我需要一种在测试本身内部实例化我的应用服务器的方法。

At the top you can do 在顶部,你可以做

var cluster = require('cluster');

Then wrap the suite in an if : 然后将套件包装在if

if (cluster.isMaster) {
    var suite = ...
    ...
}

For more info on isMaster , check the documentation 有关isMaster更多信息,请查看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM