-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: emit listening event once when call listen twice
- Loading branch information
Showing
2 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const net = require('net'); | ||
const cluster = require('cluster'); | ||
const assert = require('assert'); | ||
|
||
if (cluster.isPrimary) { | ||
const worker = cluster.fork(); | ||
worker.on('exit', common.mustCall((code) => { | ||
assert.ok(code === 0); | ||
})); | ||
} else { | ||
const server = net.createServer(); | ||
server.listen(); | ||
try { | ||
// Currently, we can call `listen` twice in cluster worker, | ||
// if we can not call `listen` twice in the futrue, | ||
// just skip this test. | ||
server.listen(); | ||
} catch (e) { | ||
console.error(e); | ||
process.exit(0); | ||
} | ||
let i = 0; | ||
process.on('internalMessage', (msg) => { | ||
if (msg.cmd === 'NODE_CLUSTER') { | ||
if (++i === 2) { | ||
setImmediate(() => { | ||
server.close(() => { | ||
process.disconnect(); | ||
}); | ||
}); | ||
} | ||
} | ||
}); | ||
// Must only call once | ||
server.on('listening', common.mustCall()); | ||
} |