Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test master behaviour in a Node.JS cluster?

Suppose you are running a cluster in Node.JS and you wish to unit-test it. For instance, you'd like to make sure that if a worker dies the cluster takes some action, such as forking another worker and possibly some related job. Or that, under certain conditions, additional workers are spawned.

I suppose that in order to do this one must launch the cluster and have somehow access to its internal state; then (for instance) force workers to get stuck, and check the state after a delay. If so, how to export the state?

like image 744
nicolagi Avatar asked Jul 31 '12 09:07

nicolagi


People also ask

What is the difference between cluster fork () vs Child_process fork () in node JS?

In a single thread, the individual instance of node. js runs specifically and to take advantage of various ecosystems, a cluster of node. js is launched, to distribute the load. With the help of a cluster module, child processes can be created very easily sharing the server ports.

Is it possible to cluster multiple node processes?

A cluster module executes the same Node. js process multiple times. Therefore, the first thing you need to do is to identify what portion of the code is for the master process and what portion is for the workers.

Which one is better node js built-in cluster or PM2 clustering?

If your application is stateless then it is easy to use pm2 cluster mode as it does not require much effort (or no effort) in code changes. But if your application uses local data, sessions or using sockets then it is recommended to use Node. js inbuilt cluster module and start your application normally using pm2.

What is the difference between cluster and Worker_threads packages in node JS?

Clusters of Node. js processes can be used to run multiple instances of Node. js that can distribute workloads among their application threads. When process isolation is not needed, use the worker_threads module instead, which allows running multiple application threads within a single Node.


1 Answers

You'll have to architect your master to return a reference to its cluster object. In your tests, you can kill one of its workers with cluster.workers[2].kill(). The worker object also has a reference to the child's process object, which you can use to simulate various conditions. You may have to use a setTimeout to ensure the master has the time to do its thing.

The above methods however still creates forks, which may be undesirable in a testing scenario. Your other option is to use a mocking library (SinonJS et al) to mock out cluster's fork method, and then spy the number of calls it gets. You can simulate worker death by using cluster.emit('exit') on the master cluster object.

Note: I'm not sure if this is an issue only with me, but cluster.emit always seems to emit twice for me, for some reason.

like image 80
abject_error Avatar answered Nov 07 '22 00:11

abject_error