summaryrefslogtreecommitdiff
path: root/test/sequential
diff options
context:
space:
mode:
authorRich Trott <rtrott@gmail.com>2016-01-17 23:09:26 -0800
committerRich Trott <rtrott@gmail.com>2016-01-19 15:42:43 -0800
commit44aba1ab1c2656caf8f0f03a2f47b4c4b35aa4a2 (patch)
treef458da61dcc006caebfe625f4d2b9a6056f91fc5 /test/sequential
parentdf4d209ad5fb90de141e0c197a8aeed600c46f9e (diff)
downloadnode-new-44aba1ab1c2656caf8f0f03a2f47b4c4b35aa4a2.tar.gz
test: improve test-cluster-disconnect-suicide-race
Previously, test-cluster-disconnect-suicide-race had two issues: * Magic numbers: How many times to spawn a worker was determined through empirical experimentation. This means that as new platforms and new CPU/RAM configurations are tested, the magic numbers require more and more refinement. This brings us to... * Non-determinism: The test seems to fail all the time when the bug it tests for is present, but it's really a judgment based on sampling. "Oh, with 8 workers per CPU, it fails about 80% of the time. Let's try 16..." This revised version of the test takes a different approach. The fix for the bug that the test was written for means that the disconnect event will fire on a subsequent tick. So we check for that and the test still fails when the fix is not in the code base and succeeds when it is. Advantages of this approach include: * The test runs much faster. * The test should be reliable on any new platform regardless of CPU and RAM. PR-URL: https://github.com/nodejs/node/pull/4739 Ref: https://github.com/nodejs/node/pull/4674 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test/sequential')
-rw-r--r--test/sequential/test-cluster-disconnect-suicide-race.js41
1 files changed, 18 insertions, 23 deletions
diff --git a/test/sequential/test-cluster-disconnect-suicide-race.js b/test/sequential/test-cluster-disconnect-suicide-race.js
index e05c420e1f..6f23b23fd2 100644
--- a/test/sequential/test-cluster-disconnect-suicide-race.js
+++ b/test/sequential/test-cluster-disconnect-suicide-race.js
@@ -1,32 +1,27 @@
'use strict';
+
+// Test should fail in Node.js 5.4.1 and pass in later versions.
+
const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
-const os = require('os');
if (cluster.isMaster) {
- function forkWorker(action) {
- const worker = cluster.fork({ action });
- worker.on('disconnect', common.mustCall(() => {
- assert.strictEqual(worker.suicide, true);
- }));
+ cluster.on('exit', (worker, code) => {
+ assert.strictEqual(code, 0, 'worker exited with error');
+ });
+
+ return cluster.fork();
+}
- worker.on('exit', common.mustCall(() => {
- assert.strictEqual(worker.suicide, true);
- }));
- }
+var eventFired = false;
- const cpus = os.cpus().length;
- const tries = cpus > 8 ? 64 : cpus * 8;
+cluster.worker.disconnect();
- cluster.on('exit', common.mustCall((worker, code) => {
- assert.strictEqual(code, 0, 'worker exited with error');
- }, tries * 2));
-
- for (let i = 0; i < tries; ++i) {
- forkWorker('disconnect');
- forkWorker('kill');
- }
-} else {
- cluster.worker[process.env.action]();
-}
+process.nextTick(common.mustCall(() => {
+ assert.strictEqual(eventFired, false, 'disconnect event should wait for ack');
+}));
+
+cluster.worker.on('disconnect', common.mustCall(() => {
+ eventFired = true;
+}));