11'use strict';
2- var common = require('../common');
3- var assert = require('assert');
4-
5- var net = require('net');
6-
7- var tests_run = 0;
2+ const common = require('../common');
3+ const assert = require('assert');
4+ const net = require('net');
85
96function pingPongTest(port, host) {
10- var N = 1000;
7+ const N = 1000;
118 var count = 0;
129 var sentPongs = 0;
1310 var sent_final_ping = false;
1411
15- var server = net.createServer({ allowHalfOpen: true }, function(socket) {
16- console.log('connection: ' + socket.remoteAddress);
17- assert.equal(server, socket.server);
18- assert.equal(1, server.connections);
12+ const server = net.createServer(
13+ { allowHalfOpen: true },
14+ common.mustCall(onSocket)
15+ );
16+
17+ function onSocket(socket) {
18+ assert.strictEqual(socket.server, server);
19+ server.getConnections(common.mustCall(function(err, connections) {
20+ assert.ifError(err);
21+ assert.strictEqual(connections, 1);
22+ }));
1923
2024 socket.setNoDelay();
2125 socket.timeout = 0;
2226
2327 socket.setEncoding('utf8');
24- socket.on('data', function(data) {
28+ socket.on('data', common.mustCall( function(data) {
2529 // Since we never queue data (we're always waiting for the PING
2630 // before sending a pong) the writeQueueSize should always be less
2731 // than one message.
2832 assert.ok(0 <= socket.bufferSize && socket.bufferSize <= 4);
2933
30- assert.equal(true, socket.writable);
31- assert.equal(true, socket.readable);
32- assert.equal(true, count <= N);
33- assert.equal (data, 'PING');
34+ assert.strictEqual( socket.writable, true );
35+ assert.strictEqual( socket.readable, true );
36+ assert.ok( count <= N);
37+ assert.strictEqual (data, 'PING');
3438
35- socket.write('PONG', function() {
39+ socket.write('PONG', common.mustCall( function() {
3640 sentPongs++;
37- });
38- });
41+ })) ;
42+ }, N + 1) );
3943
40- socket.on('end', function() {
41- assert.equal(true, socket.allowHalfOpen);
42- assert.equal(true, socket.writable); // because allowHalfOpen
43- assert.equal(false, socket.readable);
44+ socket.on('end', common.mustCall( function() {
45+ assert.strictEqual( socket.allowHalfOpen, true );
46+ assert.strictEqual( socket.writable, true ); // because allowHalfOpen
47+ assert.strictEqual( socket.readable, false );
4448 socket.end();
45- });
49+ })) ;
4650
47- socket.on('error', function(e) {
48- throw e;
49- });
51+ socket.on('error', common.fail);
5052
51- socket.on('close', function() {
52- console.log('server socket.end');
53- assert.equal(false, socket.writable);
54- assert.equal(false, socket.readable);
53+ socket.on('close', common.mustCall(function() {
54+ assert.strictEqual(socket.writable, false);
55+ assert.strictEqual(socket.readable, false);
5556 socket.server.close();
56- });
57- });
57+ })) ;
58+ }
5859
5960
60- server.listen(port, host, function() {
61+ server.listen(port, host, common.mustCall( function() {
6162 if (this.address().port)
6263 port = this.address().port;
63- console.log(`server listening on ${port} ${host}`);
6464
65- var client = net.createConnection(port, host);
65+ const client = net.createConnection(port, host);
6666
6767 client.setEncoding('ascii');
68- client.on('connect', function() {
69- assert.equal(true, client.readable);
70- assert.equal(true, client.writable);
68+ client.on('connect', common.mustCall( function() {
69+ assert.strictEqual( client.readable, true );
70+ assert.strictEqual( client.writable, true );
7171 client.write('PING');
72- });
72+ })) ;
7373
74- client.on('data', function(data) {
75- assert.equal( 'PONG', data );
74+ client.on('data', common.mustCall( function(data) {
75+ assert.strictEqual(data, 'PONG');
7676 count += 1;
7777
7878 if (sent_final_ping) {
79- assert.equal(false, client.writable);
80- assert.equal(true, client.readable);
79+ assert.strictEqual( client.writable, false );
80+ assert.strictEqual( client.readable, true );
8181 return;
8282 } else {
83- assert.equal(true, client.writable);
84- assert.equal(true, client.readable);
83+ assert.strictEqual( client.writable, true );
84+ assert.strictEqual( client.readable, true );
8585 }
8686
8787 if (count < N) {
@@ -91,20 +91,16 @@ function pingPongTest(port, host) {
9191 client.write('PING');
9292 client.end();
9393 }
94- });
95-
96- client.on('close', function() {
97- console.log('client.end');
98- assert.equal(N + 1, count);
99- assert.equal(N + 1, sentPongs);
100- assert.equal(true, sent_final_ping);
101- tests_run += 1;
102- });
103-
104- client.on('error', function(e) {
105- throw e;
106- });
107- });
94+ }, N + 1));
95+
96+ client.on('close', common.mustCall(function() {
97+ assert.strictEqual(count, N + 1);
98+ assert.strictEqual(sentPongs, N + 1);
99+ assert.strictEqual(sent_final_ping, true);
100+ }));
101+
102+ client.on('error', common.fail);
103+ }));
108104}
109105
110106/* All are run at once, so run on different ports */
@@ -114,11 +110,3 @@ pingPongTest(0);
114110pingPongTest(0, 'localhost');
115111if (common.hasIPv6)
116112 pingPongTest(0, '::1');
117-
118- process.on('exit', function() {
119- if (common.hasIPv6)
120- assert.equal(4, tests_run);
121- else
122- assert.equal(3, tests_run);
123- console.log('done');
124- });
0 commit comments