()
| 38 | |
| 39 | |
| 40 | function primary() { |
| 41 | let listening = 0; |
| 42 | |
| 43 | // Fork 4 workers. |
| 44 | for (let i = 0; i < NUM_WORKERS; i++) |
| 45 | cluster.fork(); |
| 46 | |
| 47 | // Wait until all workers are listening. |
| 48 | cluster.on('listening', common.mustCall((worker, address) => { |
| 49 | if (++listening < NUM_WORKERS) |
| 50 | return; |
| 51 | |
| 52 | // Start sending messages. |
| 53 | const buf = Buffer.from('hello world'); |
| 54 | const socket = dgram.createSocket('udp4'); |
| 55 | let sent = 0; |
| 56 | doSend(); |
| 57 | |
| 58 | function doSend() { |
| 59 | socket.send(buf, 0, buf.length, address.port, address.address, afterSend); |
| 60 | } |
| 61 | |
| 62 | function afterSend() { |
| 63 | sent++; |
| 64 | if (sent < NUM_WORKERS * PACKETS_PER_WORKER) { |
| 65 | doSend(); |
| 66 | } else { |
| 67 | socket.close(); |
| 68 | } |
| 69 | } |
| 70 | }, NUM_WORKERS)); |
| 71 | |
| 72 | // Set up event handlers for every worker. Each worker sends a message when |
| 73 | // it has received the expected number of packets. After that it disconnects. |
| 74 | for (const key in cluster.workers) { |
| 75 | if (Object.hasOwn(cluster.workers, key)) |
| 76 | setupWorker(cluster.workers[key]); |
| 77 | } |
| 78 | |
| 79 | function setupWorker(worker) { |
| 80 | let received = 0; |
| 81 | |
| 82 | worker.on('message', common.mustCall((msg) => { |
| 83 | received = msg.received; |
| 84 | worker.disconnect(); |
| 85 | })); |
| 86 | |
| 87 | worker.on('exit', common.mustCall(() => { |
| 88 | assert.strictEqual(received, PACKETS_PER_WORKER); |
| 89 | })); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | |
| 94 | function worker() { |
no test coverage detected
searching dependent graphs…