简体   繁体   中英

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. 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; as I instantiate the server inside my test code, as follows:

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.

At the top you can do

var cluster = require('cluster');

Then wrap the suite in an if :

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

For more info on isMaster , check the documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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