Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve the code in test-pipe.js #10452

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 40 additions & 54 deletions test/sequential/test-pipe.js
Original file line number Diff line number Diff line change
@@ -1,110 +1,96 @@
'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.allocUnsafe(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);
});