mirror of https://github.com/lukechilds/node.git
Browse Source
* add `mustCall` and `mustNotCall` to all callbacks * added `known_issue` for port binding Backport-PR-URL: https://github.com/nodejs/node/pull/14327 PR-URL: https://github.com/nodejs/node/pull/13100 Refs: https://github.com/nodejs/node/issues/13055 Refs: https://github.com/nodejs/node/pull/12999 Refs: https://github.com/nodejs/node/issues/13526 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>v6.x
committed by
Myles Borins
2 changed files with 128 additions and 36 deletions
@ -0,0 +1,56 @@ |
|||||
|
'use strict'; |
||||
|
const common = require('../common'); |
||||
|
|
||||
|
// This test should fail because at present `cluster` does not know how to share
|
||||
|
// a socket when `worker1` binds with `port: 0`, and others try to bind to the
|
||||
|
// assigned port number from `worker1`
|
||||
|
//
|
||||
|
// *Note*: since this is a `known_issue` we try to swallow all errors except
|
||||
|
// the one we are interested in
|
||||
|
|
||||
|
const assert = require('assert'); |
||||
|
const cluster = require('cluster'); |
||||
|
const dgram = require('dgram'); |
||||
|
const BYE = 'bye'; |
||||
|
|
||||
|
if (cluster.isMaster) { |
||||
|
const worker1 = cluster.fork(); |
||||
|
|
||||
|
// verify that Windows doesn't support this scenario
|
||||
|
worker1.on('error', (err) => { |
||||
|
if (err.code === 'ENOTSUP') throw err; |
||||
|
}); |
||||
|
|
||||
|
worker1.on('message', (msg) => { |
||||
|
if (typeof msg !== 'object') process.exit(0); |
||||
|
if (msg.message !== 'success') process.exit(0); |
||||
|
if (typeof msg.port1 !== 'number') process.exit(0); |
||||
|
|
||||
|
const worker2 = cluster.fork({ PRT1: msg.port1 }); |
||||
|
worker2.on('message', () => process.exit(0)); |
||||
|
worker2.on('exit', (code, signal) => { |
||||
|
// this is the droid we are looking for
|
||||
|
assert.strictEqual(code, 0); |
||||
|
assert.strictEqual(signal, null); |
||||
|
}); |
||||
|
|
||||
|
// cleanup anyway
|
||||
|
process.on('exit', () => { |
||||
|
worker1.send(BYE); |
||||
|
worker2.send(BYE); |
||||
|
}); |
||||
|
}); |
||||
|
// end master code
|
||||
|
} else { |
||||
|
// worker code
|
||||
|
process.on('message', (msg) => msg === BYE && process.exit(0)); |
||||
|
|
||||
|
// first worker will bind to '0', second will try the assigned port and fail
|
||||
|
const PRT1 = process.env.PRT1 || 0; |
||||
|
const socket1 = dgram.createSocket('udp4', () => {}); |
||||
|
socket1.on('error', PRT1 === 0 ? () => {} : assert.fail); |
||||
|
socket1.bind( |
||||
|
{ address: common.localhostIPv4, port: PRT1, exclusive: false }, |
||||
|
() => process.send({ message: 'success', port1: socket1.address().port }) |
||||
|
); |
||||
|
} |
@ -1,56 +1,92 @@ |
|||||
'use strict'; |
'use strict'; |
||||
const common = require('../common'); |
const common = require('../common'); |
||||
|
|
||||
|
// This test asserts the semantics of dgram::socket.bind({ exclusive })
|
||||
|
// when called from a cluster.Worker
|
||||
|
|
||||
const assert = require('assert'); |
const assert = require('assert'); |
||||
const cluster = require('cluster'); |
const cluster = require('cluster'); |
||||
const dgram = require('dgram'); |
const dgram = require('dgram'); |
||||
|
const BYE = 'bye'; |
||||
function noop() { } |
const WORKER2_NAME = 'wrker2'; |
||||
|
|
||||
if (cluster.isMaster) { |
if (cluster.isMaster) { |
||||
const worker1 = cluster.fork(); |
const worker1 = cluster.fork(); |
||||
|
|
||||
if (common.isWindows) { |
if (common.isWindows) { |
||||
const checkErrType = (er) => { |
worker1.on('error', common.mustCall((err) => { |
||||
assert.strictEqual(er.code, 'ENOTSUP'); |
console.log(err); |
||||
|
assert.strictEqual(err.code, 'ENOTSUP'); |
||||
worker1.kill(); |
worker1.kill(); |
||||
}; |
})); |
||||
|
|
||||
worker1.on('error', common.mustCall(checkErrType, 1)); |
|
||||
return; |
return; |
||||
} |
} |
||||
|
|
||||
worker1.on('message', (msg) => { |
worker1.on('message', common.mustCall((msg) => { |
||||
|
console.log(msg); |
||||
assert.strictEqual(msg, 'success'); |
assert.strictEqual(msg, 'success'); |
||||
const worker2 = cluster.fork(); |
|
||||
|
|
||||
worker2.on('message', (msg) => { |
const worker2 = cluster.fork({ WORKER2_NAME }); |
||||
assert.strictEqual(msg, 'socket2:EADDRINUSE'); |
worker2.on('message', common.mustCall((msg) => { |
||||
worker1.kill(); |
console.log(msg); |
||||
worker2.kill(); |
assert.strictEqual(msg, 'socket3:EADDRINUSE'); |
||||
}); |
|
||||
}); |
// finish test
|
||||
|
worker1.send(BYE); |
||||
|
worker2.send(BYE); |
||||
|
})); |
||||
|
worker2.on('exit', common.mustCall((code, signal) => { |
||||
|
assert.strictEqual(signal, null); |
||||
|
assert.strictEqual(code, 0); |
||||
|
})); |
||||
|
})); |
||||
|
worker1.on('exit', common.mustCall((code, signal) => { |
||||
|
assert.strictEqual(signal, null); |
||||
|
assert.strictEqual(code, 0); |
||||
|
})); |
||||
|
// end master code
|
||||
} else { |
} else { |
||||
const socket1 = dgram.createSocket('udp4', noop); |
// worker code
|
||||
const socket2 = dgram.createSocket('udp4', noop); |
process.on('message', common.mustCallAtLeast((msg) => { |
||||
|
if (msg === BYE) process.exit(0); |
||||
|
}), 1); |
||||
|
|
||||
socket1.on('error', (err) => { |
const isSecondWorker = process.env.WORKER2_NAME === WORKER2_NAME; |
||||
// no errors expected
|
const socket1 = dgram.createSocket('udp4', common.mustNotCall()); |
||||
process.send(`socket1:${err.code}`); |
const socket2 = dgram.createSocket('udp4', common.mustNotCall()); |
||||
}); |
const socket3 = dgram.createSocket('udp4', common.mustNotCall()); |
||||
|
|
||||
socket2.on('error', (err) => { |
socket1.on('error', (err) => assert.fail(undefined, undefined, err)); |
||||
// an error is expected on the second worker
|
socket2.on('error', (err) => assert.fail(undefined, undefined, err)); |
||||
process.send(`socket2:${err.code}`); |
|
||||
}); |
|
||||
|
|
||||
socket1.bind({ |
// First worker should bind, second should err
|
||||
address: 'localhost', |
const socket3OnBind = |
||||
port: common.PORT, |
isSecondWorker ? |
||||
exclusive: false |
common.mustNotCall() : |
||||
}, () => { |
common.mustCall(() => { |
||||
socket2.bind({ port: common.PORT + 1, exclusive: true }, () => { |
const port3 = socket3.address().port; |
||||
// the first worker should succeed
|
assert.strictEqual(typeof port3, 'number'); |
||||
process.send('success'); |
process.send('success'); |
||||
}); |
}); |
||||
|
// an error is expected only in the second worker
|
||||
|
const socket3OnError = |
||||
|
!isSecondWorker ? |
||||
|
common.mustNotCall() : |
||||
|
common.mustCall((err) => { |
||||
|
process.send(`socket3:${err.code}`); |
||||
}); |
}); |
||||
|
const address = common.localhostIPv4; |
||||
|
const opt1 = { address, port: 0, exclusive: false }; |
||||
|
const opt2 = { address, port: common.PORT, exclusive: false }; |
||||
|
const opt3 = { address, port: common.PORT + 1, exclusive: true }; |
||||
|
socket1.bind(opt1, common.mustCall(() => { |
||||
|
const port1 = socket1.address().port; |
||||
|
assert.strictEqual(typeof port1, 'number'); |
||||
|
socket2.bind(opt2, common.mustCall(() => { |
||||
|
const port2 = socket2.address().port; |
||||
|
assert.strictEqual(typeof port2, 'number'); |
||||
|
socket3.on('error', socket3OnError); |
||||
|
socket3.bind(opt3, socket3OnBind); |
||||
|
})); |
||||
|
})); |
||||
} |
} |
||||
|
Loading…
Reference in new issue