MCPcopy Index your code
hub / github.com/nodejs/node / primary

Function primary

test/parallel/test-cluster-dgram-1.js:40–91  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

38
39
40function 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
94function worker() {

Callers 1

Calls 5

createSocketMethod · 0.80
doSendFunction · 0.70
setupWorkerFunction · 0.70
onMethod · 0.45
fromMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…