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

Function pingPongTest

test/parallel/test-net-pingpong.js:27–126  ·  view source on GitHub ↗
(port, host)

Source from the content-addressed store, hash-verified

25const net = require('net');
26
27function pingPongTest(port, host) {
28 const N = 1000;
29 let count = 0;
30 let sentPongs = 0;
31 let sent_final_ping = false;
32
33 const server = net.createServer(
34 { allowHalfOpen: true },
35 common.mustCall(onSocket)
36 );
37
38 function onSocket(socket) {
39 assert.strictEqual(socket.server, server);
40 assert.strictEqual(
41 server,
42 server.getConnections(common.mustSucceed((connections) => {
43 assert.strictEqual(connections, 1);
44 }))
45 );
46
47 socket.setNoDelay();
48 socket.timeout = 0;
49
50 socket.setEncoding('utf8');
51 socket.on('data', common.mustCall(function(data) {
52 // Since we never queue data (we're always waiting for the PING
53 // before sending a pong) the writeQueueSize should always be less
54 // than one message.
55 assert.ok(socket.bufferSize >= 0 && socket.bufferSize <= 4);
56
57 assert.strictEqual(socket.writable, true);
58 assert.strictEqual(socket.readable, true);
59 assert.ok(count <= N);
60 assert.strictEqual(data, 'PING');
61
62 socket.write('PONG', common.mustCall(function() {
63 sentPongs++;
64 }));
65 }, N + 1));
66
67 socket.on('end', common.mustCall(function() {
68 assert.strictEqual(socket.allowHalfOpen, true);
69 assert.strictEqual(socket.writable, true); // Because allowHalfOpen
70 assert.strictEqual(socket.readable, false);
71 socket.end();
72 }));
73
74 socket.on('error', common.mustNotCall());
75
76 socket.on('close', common.mustCall(function() {
77 assert.strictEqual(socket.writable, false);
78 assert.strictEqual(socket.readable, false);
79 socket.server.close();
80 }));
81 }
82
83
84 server.listen(port, host, common.mustCall(function() {

Callers 2

Calls 7

listenMethod · 0.80
addressMethod · 0.65
createConnectionMethod · 0.45
setEncodingMethod · 0.45
onMethod · 0.45
writeMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…