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>
v4.x
Adrian Estrada 8 years ago
committed by Myles Borins
parent
commit
31aa877003
No known key found for this signature in database GPG Key ID: 933B01F40B5CA946
  1. 94
      test/sequential/test-pipe.js

94
test/sequential/test-pipe.js

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

Loading…
Cancel
Save