Wednesday 17 May 2017

How Avajs test cluster related modules?

I want to test a cluster related module, I found the test case execute once, and cluster.isMaster is always true, is there any way to resolve this except mock cluster?

test('test case', async t => {
  let options = {
    workers: 1,
  };
  let ClusterMaster = getClusterMaster();
  if(cluster.isMaster){
    // always master
    let instance = new ClusterMaster(options);
    instance.forkWorkers().then(()=>{console.log(1)});
  }else{
    // cannot entry this block
    console.log('worker running');
    try {
      http.Server((req, res) => {
        res.writeHead(200);
        res.end('hello world\n');
        process.send({cmd: 'notifyRequest'});
      }).listen(8000);
    } catch (e) {
      console.log(e);
    }
  }
});


via CoderZ

No comments:

Post a Comment