Skip to content

Commit

Permalink
test: improve the code in test-pipe.js
Browse files Browse the repository at this point in the history
* 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: #10452
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
  • Loading branch information
edsadr authored and MylesBorins committed Feb 1, 2017
1 parent 4bbd50e commit 31aa877
Showing 1 changed file with 40 additions and 54 deletions.
94 changes: 40 additions & 54 deletions test/sequential/test-pipe.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 31aa877

Please sign in to comment.