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