Browse Source

test: improve the code in test-pipe.js

* use const and let instead of var
* use common.mustCall to control functions executions
* use assert.strictEqual instead of assert.equal
* use assert.ifError to handle errors
* use arrow functions
* remove console.log and process.stdout.write

PR-URL: https://github.com/nodejs/node/pull/10452
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
v6
Adrian Estrada 8 years ago
committed by Italo A. Casas
parent
commit
fc103bbdc8
  1. 94
      test/sequential/test-pipe.js

94
test/sequential/test-pipe.js

@ -1,110 +1,96 @@
'use strict'; 'use strict';
var common = require('../common'); const common = require('../common');
var assert = require('assert'); const assert = require('assert');
var http = require('http'); const http = require('http');
var net = require('net'); const net = require('net');
var webPort = common.PORT; const webPort = common.PORT;
var tcpPort = webPort + 1; const tcpPort = webPort + 1;
const bufferSize = 5 * 1024 * 1024;
var listenCount = 0; let listenCount = 0;
var gotThanks = false; let gotThanks = false;
var tcpLengthSeen = 0; let tcpLengthSeen = 0;
var bufferSize = 5 * 1024 * 1024;
/* /*
* 5MB of random buffer. * 5MB of random buffer.
*/ */
var buffer = Buffer.allocUnsafe(bufferSize); const buffer = Buffer.allocUnsafe(bufferSize);
for (var i = 0; i < buffer.length; i++) { for (let i = 0; i < buffer.length; i++) {
buffer[i] = parseInt(Math.random() * 10000) % 256; buffer[i] = parseInt(Math.random() * 10000) % 256;
} }
var web = http.Server(function(req, res) { const web = http.Server(common.mustCall((req, res) => {
web.close(); web.close();
console.log(req.headers); const socket = net.Stream();
var socket = net.Stream();
socket.connect(tcpPort); socket.connect(tcpPort);
socket.on('connect', function() { socket.on('connect', common.mustCall(() => {}));
console.log('socket connected');
});
req.pipe(socket); req.pipe(socket);
req.on('end', function() { req.on('end', common.mustCall(() => {
res.writeHead(200); res.writeHead(200);
res.write('thanks'); res.write('thanks');
res.end(); res.end();
console.log('response with \'thanks\''); }));
});
req.connection.on('error', function(e) { req.connection.on('error', (e) => {
console.log('http server-side error: ' + e.message); assert.ifError(e);
process.exit(1);
}); });
}); }));
web.listen(webPort, startClient); web.listen(webPort, startClient);
var tcp = net.Server(function(s) { const tcp = net.Server(common.mustCall((s) => {
tcp.close(); tcp.close();
console.log('tcp server connection'); let i = 0;
var i = 0;
s.on('data', function(d) { s.on('data', (d) => {
process.stdout.write('.');
tcpLengthSeen += d.length; tcpLengthSeen += d.length;
for (var j = 0; j < d.length; j++) { for (let j = 0; j < d.length; j++) {
assert.equal(buffer[i], d[j]); assert.strictEqual(buffer[i], d[j]);
i++; i++;
} }
}); });
s.on('end', function() { s.on('end', common.mustCall(() => {
console.log('tcp socket disconnect');
s.end(); s.end();
}); }));
s.on('error', function(e) { s.on('error', (e) => {
console.log('tcp server-side error: ' + e.message); assert.ifError(e);
process.exit(1);
}); });
}); }));
tcp.listen(tcpPort, startClient);
tcp.listen(tcpPort, startClient);
function startClient() { function startClient() {
listenCount++; listenCount++;
if (listenCount < 2) return; if (listenCount < 2) return;
console.log('Making request'); const req = http.request({
var req = http.request({
port: common.PORT, port: common.PORT,
method: 'GET', method: 'GET',
path: '/', path: '/',
headers: { 'content-length': buffer.length } headers: { 'content-length': buffer.length }
}, function(res) { }, common.mustCall((res) => {
console.log('Got response');
res.setEncoding('utf8'); res.setEncoding('utf8');
res.on('data', function(string) { res.on('data', common.mustCall((string) => {
assert.equal('thanks', string); assert.strictEqual('thanks', string);
gotThanks = true; gotThanks = true;
}); }));
}); }));
req.write(buffer); req.write(buffer);
req.end(); req.end();
console.error('ended request', req);
} }
process.on('exit', function() { process.on('exit', () => {
assert.ok(gotThanks); assert.ok(gotThanks);
assert.equal(bufferSize, tcpLengthSeen); assert.strictEqual(bufferSize, tcpLengthSeen);
}); });

Loading…
Cancel
Save