Browse Source

test: refactor test-net-pingpong

* var -> const.
* Verify that callbacks are called with common.mustCall.
* Replace usage of deprecated `server.connections`.
* Use common.fail instead of rethrowing errors.
* Remove console.log statements.
* assert.equal -> assert.strictEqual.
* Correct order of arguments in assert.strictEqual.

PR-URL: https://github.com/nodejs/node/pull/9812
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
v4.x
Michaël Zasso 8 years ago
committed by Myles Borins
parent
commit
8694811ef0
  1. 124
      test/parallel/test-net-pingpong.js

124
test/parallel/test-net-pingpong.js

@ -1,87 +1,87 @@
'use strict'; 'use strict';
var common = require('../common'); const common = require('../common');
var assert = require('assert'); const assert = require('assert');
const net = require('net');
var net = require('net');
var tests_run = 0;
function pingPongTest(port, host) { function pingPongTest(port, host) {
var N = 1000; const N = 1000;
var count = 0; var count = 0;
var sentPongs = 0; var sentPongs = 0;
var sent_final_ping = false; var sent_final_ping = false;
var server = net.createServer({ allowHalfOpen: true }, function(socket) { const server = net.createServer(
console.log('connection: ' + socket.remoteAddress); { allowHalfOpen: true },
assert.equal(server, socket.server); common.mustCall(onSocket)
assert.equal(1, server.connections); );
function onSocket(socket) {
assert.strictEqual(socket.server, server);
server.getConnections(common.mustCall(function(err, connections) {
assert.ifError(err);
assert.strictEqual(connections, 1);
}));
socket.setNoDelay(); socket.setNoDelay();
socket.timeout = 0; socket.timeout = 0;
socket.setEncoding('utf8'); socket.setEncoding('utf8');
socket.on('data', function(data) { socket.on('data', common.mustCall(function(data) {
// Since we never queue data (we're always waiting for the PING // Since we never queue data (we're always waiting for the PING
// before sending a pong) the writeQueueSize should always be less // before sending a pong) the writeQueueSize should always be less
// than one message. // than one message.
assert.ok(0 <= socket.bufferSize && socket.bufferSize <= 4); assert.ok(0 <= socket.bufferSize && socket.bufferSize <= 4);
assert.equal(true, socket.writable); assert.strictEqual(socket.writable, true);
assert.equal(true, socket.readable); assert.strictEqual(socket.readable, true);
assert.equal(true, count <= N); assert.ok(count <= N);
assert.equal(data, 'PING'); assert.strictEqual(data, 'PING');
socket.write('PONG', function() { socket.write('PONG', common.mustCall(function() {
sentPongs++; sentPongs++;
}); }));
}); }, N + 1));
socket.on('end', function() { socket.on('end', common.mustCall(function() {
assert.equal(true, socket.allowHalfOpen); assert.strictEqual(socket.allowHalfOpen, true);
assert.equal(true, socket.writable); // because allowHalfOpen assert.strictEqual(socket.writable, true); // because allowHalfOpen
assert.equal(false, socket.readable); assert.strictEqual(socket.readable, false);
socket.end(); socket.end();
}); }));
socket.on('error', function(e) { socket.on('error', common.fail);
throw e;
});
socket.on('close', function() { socket.on('close', common.mustCall(function() {
console.log('server socket.end'); assert.strictEqual(socket.writable, false);
assert.equal(false, socket.writable); assert.strictEqual(socket.readable, false);
assert.equal(false, socket.readable);
socket.server.close(); socket.server.close();
}); }));
}); }
server.listen(port, host, function() { server.listen(port, host, common.mustCall(function() {
if (this.address().port) if (this.address().port)
port = this.address().port; port = this.address().port;
console.log(`server listening on ${port} ${host}`);
var client = net.createConnection(port, host); const client = net.createConnection(port, host);
client.setEncoding('ascii'); client.setEncoding('ascii');
client.on('connect', function() { client.on('connect', common.mustCall(function() {
assert.equal(true, client.readable); assert.strictEqual(client.readable, true);
assert.equal(true, client.writable); assert.strictEqual(client.writable, true);
client.write('PING'); client.write('PING');
}); }));
client.on('data', function(data) { client.on('data', common.mustCall(function(data) {
assert.equal('PONG', data); assert.strictEqual(data, 'PONG');
count += 1; count += 1;
if (sent_final_ping) { if (sent_final_ping) {
assert.equal(false, client.writable); assert.strictEqual(client.writable, false);
assert.equal(true, client.readable); assert.strictEqual(client.readable, true);
return; return;
} else { } else {
assert.equal(true, client.writable); assert.strictEqual(client.writable, true);
assert.equal(true, client.readable); assert.strictEqual(client.readable, true);
} }
if (count < N) { if (count < N) {
@ -91,20 +91,16 @@ function pingPongTest(port, host) {
client.write('PING'); client.write('PING');
client.end(); client.end();
} }
}); }, N + 1));
client.on('close', function() { client.on('close', common.mustCall(function() {
console.log('client.end'); assert.strictEqual(count, N + 1);
assert.equal(N + 1, count); assert.strictEqual(sentPongs, N + 1);
assert.equal(N + 1, sentPongs); assert.strictEqual(sent_final_ping, true);
assert.equal(true, sent_final_ping); }));
tests_run += 1;
}); client.on('error', common.fail);
}));
client.on('error', function(e) {
throw e;
});
});
} }
/* All are run at once, so run on different ports */ /* All are run at once, so run on different ports */
@ -114,11 +110,3 @@ pingPongTest(0);
pingPongTest(0, 'localhost'); pingPongTest(0, 'localhost');
if (common.hasIPv6) if (common.hasIPv6)
pingPongTest(0, '::1'); pingPongTest(0, '::1');
process.on('exit', function() {
if (common.hasIPv6)
assert.equal(4, tests_run);
else
assert.equal(3, tests_run);
console.log('done');
});

Loading…
Cancel
Save