|
1 | 1 | 'use strict';
|
2 | 2 | const common = require('../common');
|
| 3 | + |
| 4 | +// This test asserts the semantics of dgram::socket.bind({ exclusive }) |
| 5 | +// when called from a cluster.Worker |
| 6 | + |
3 | 7 | const assert = require('assert');
|
4 | 8 | const cluster = require('cluster');
|
5 | 9 | const dgram = require('dgram');
|
6 |
| - |
7 |
| -function noop() { } |
| 10 | +const BYE = 'bye'; |
| 11 | +const WORKER2_NAME = 'wrker2'; |
8 | 12 |
|
9 | 13 | if (cluster.isMaster) {
|
10 | 14 | const worker1 = cluster.fork();
|
11 | 15 |
|
12 | 16 | if (common.isWindows) {
|
13 |
| - const checkErrType = (er) => { |
14 |
| - assert.strictEqual(er.code, 'ENOTSUP'); |
| 17 | + worker1.on('error', common.mustCall((err) => { |
| 18 | + console.log(err); |
| 19 | + assert.strictEqual(err.code, 'ENOTSUP'); |
15 | 20 | worker1.kill();
|
16 |
| - }; |
17 |
| - |
18 |
| - worker1.on('error', common.mustCall(checkErrType, 1)); |
| 21 | + })); |
19 | 22 | return;
|
20 | 23 | }
|
21 | 24 |
|
22 |
| - worker1.on('message', (msg) => { |
| 25 | + worker1.on('message', common.mustCall((msg) => { |
| 26 | + console.log(msg); |
23 | 27 | assert.strictEqual(msg, 'success');
|
24 |
| - const worker2 = cluster.fork(); |
25 | 28 |
|
26 |
| - worker2.on('message', (msg) => { |
27 |
| - assert.strictEqual(msg, 'socket2:EADDRINUSE'); |
28 |
| - worker1.kill(); |
29 |
| - worker2.kill(); |
30 |
| - }); |
31 |
| - }); |
| 29 | + const worker2 = cluster.fork({ WORKER2_NAME }); |
| 30 | + worker2.on('message', common.mustCall((msg) => { |
| 31 | + console.log(msg); |
| 32 | + assert.strictEqual(msg, 'socket3:EADDRINUSE'); |
| 33 | + |
| 34 | + // finish test |
| 35 | + worker1.send(BYE); |
| 36 | + worker2.send(BYE); |
| 37 | + })); |
| 38 | + worker2.on('exit', common.mustCall((code, signal) => { |
| 39 | + assert.strictEqual(signal, null); |
| 40 | + assert.strictEqual(code, 0); |
| 41 | + })); |
| 42 | + })); |
| 43 | + worker1.on('exit', common.mustCall((code, signal) => { |
| 44 | + assert.strictEqual(signal, null); |
| 45 | + assert.strictEqual(code, 0); |
| 46 | + })); |
| 47 | + // end master code |
32 | 48 | } else {
|
33 |
| - const socket1 = dgram.createSocket('udp4', noop); |
34 |
| - const socket2 = dgram.createSocket('udp4', noop); |
35 |
| - |
36 |
| - socket1.on('error', (err) => { |
37 |
| - // no errors expected |
38 |
| - process.send(`socket1:${err.code}`); |
39 |
| - }); |
40 |
| - |
41 |
| - socket2.on('error', (err) => { |
42 |
| - // an error is expected on the second worker |
43 |
| - process.send(`socket2:${err.code}`); |
44 |
| - }); |
45 |
| - |
46 |
| - socket1.bind({ |
47 |
| - address: 'localhost', |
48 |
| - port: common.PORT, |
49 |
| - exclusive: false |
50 |
| - }, () => { |
51 |
| - socket2.bind({ port: common.PORT + 1, exclusive: true }, () => { |
52 |
| - // the first worker should succeed |
| 49 | + // worker code |
| 50 | + process.on('message', common.mustCallAtLeast((msg) => { |
| 51 | + if (msg === BYE) process.exit(0); |
| 52 | + }), 1); |
| 53 | + |
| 54 | + const isSecondWorker = process.env.WORKER2_NAME === WORKER2_NAME; |
| 55 | + const socket1 = dgram.createSocket('udp4', common.mustNotCall()); |
| 56 | + const socket2 = dgram.createSocket('udp4', common.mustNotCall()); |
| 57 | + const socket3 = dgram.createSocket('udp4', common.mustNotCall()); |
| 58 | + |
| 59 | + socket1.on('error', (err) => assert.fail(undefined, undefined, err)); |
| 60 | + socket2.on('error', (err) => assert.fail(undefined, undefined, err)); |
| 61 | + |
| 62 | + // First worker should bind, second should err |
| 63 | + const socket3OnBind = |
| 64 | + isSecondWorker ? |
| 65 | + common.mustNotCall() : |
| 66 | + common.mustCall(() => { |
| 67 | + const port3 = socket3.address().port; |
| 68 | + assert.strictEqual(typeof port3, 'number'); |
53 | 69 | process.send('success');
|
54 | 70 | });
|
55 |
| - }); |
| 71 | + // an error is expected only in the second worker |
| 72 | + const socket3OnError = |
| 73 | + !isSecondWorker ? |
| 74 | + common.mustNotCall() : |
| 75 | + common.mustCall((err) => { |
| 76 | + process.send(`socket3:${err.code}`); |
| 77 | + }); |
| 78 | + const address = common.localhostIPv4; |
| 79 | + const opt1 = { address, port: 0, exclusive: false }; |
| 80 | + const opt2 = { address, port: common.PORT, exclusive: false }; |
| 81 | + const opt3 = { address, port: common.PORT + 1, exclusive: true }; |
| 82 | + socket1.bind(opt1, common.mustCall(() => { |
| 83 | + const port1 = socket1.address().port; |
| 84 | + assert.strictEqual(typeof port1, 'number'); |
| 85 | + socket2.bind(opt2, common.mustCall(() => { |
| 86 | + const port2 = socket2.address().port; |
| 87 | + assert.strictEqual(typeof port2, 'number'); |
| 88 | + socket3.on('error', socket3OnError); |
| 89 | + socket3.bind(opt3, socket3OnBind); |
| 90 | + })); |
| 91 | + })); |
56 | 92 | }
|
0 commit comments