Skip to content

Commit 31aa877

Browse files
edsadrMylesBorins
authored andcommitted
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: #10452 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Brian White <[email protected]>
1 parent 4bbd50e commit 31aa877

File tree

1 file changed

+40
-54
lines changed

1 file changed

+40
-54
lines changed

‎test/sequential/test-pipe.js‎

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,97 @@
11
'use strict';
2-
varcommon=require('../common');
3-
varassert=require('assert');
4-
varhttp=require('http');
5-
varnet=require('net');
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
consthttp=require('http');
5+
constnet=require('net');
66

7-
varwebPort=common.PORT;
8-
vartcpPort=webPort+1;
7+
constwebPort=common.PORT;
8+
consttcpPort=webPort+1;
9+
constbufferSize=5*1024*1024;
910

10-
varlistenCount=0;
11-
vargotThanks=false;
12-
vartcpLengthSeen=0;
13-
varbufferSize=5*1024*1024;
11+
letlistenCount=0;
12+
letgotThanks=false;
13+
lettcpLengthSeen=0;
1414

1515

1616
/*
1717
* 5MB of random buffer.
1818
*/
19-
varbuffer=Buffer(bufferSize);
20-
for(vari=0;i<buffer.length;i++){
19+
constbuffer=Buffer.allocUnsafe(bufferSize);
20+
for(leti=0;i<buffer.length;i++){
2121
buffer[i]=parseInt(Math.random()*10000)%256;
2222
}
2323

2424

25-
varweb=http.Server(function(req,res){
25+
constweb=http.Server(common.mustCall((req,res)=>{
2626
web.close();
2727

28-
console.log(req.headers);
29-
30-
varsocket=net.Stream();
28+
constsocket=net.Stream();
3129
socket.connect(tcpPort);
3230

33-
socket.on('connect',function(){
34-
console.log('socket connected');
35-
});
31+
socket.on('connect',common.mustCall(()=>{}));
3632

3733
req.pipe(socket);
3834

39-
req.on('end',function(){
35+
req.on('end',common.mustCall(()=>{
4036
res.writeHead(200);
4137
res.write('thanks');
4238
res.end();
43-
console.log('response with \'thanks\'');
44-
});
39+
}));
4540

46-
req.connection.on('error',function(e){
47-
console.log('http server-side error: '+e.message);
48-
process.exit(1);
41+
req.connection.on('error',(e)=>{
42+
assert.ifError(e);
4943
});
50-
});
44+
}));
45+
5146
web.listen(webPort,startClient);
5247

5348

54-
vartcp=net.Server(function(s){
49+
consttcp=net.Server(common.mustCall((s)=>{
5550
tcp.close();
5651

57-
console.log('tcp server connection');
58-
59-
vari=0;
52+
leti=0;
6053

61-
s.on('data',function(d){
62-
process.stdout.write('.');
54+
s.on('data',(d)=>{
6355
tcpLengthSeen+=d.length;
64-
for(varj=0;j<d.length;j++){
65-
assert.equal(buffer[i],d[j]);
56+
for(letj=0;j<d.length;j++){
57+
assert.strictEqual(buffer[i],d[j]);
6658
i++;
6759
}
6860
});
6961

70-
s.on('end',function(){
71-
console.log('tcp socket disconnect');
62+
s.on('end',common.mustCall(()=>{
7263
s.end();
73-
});
64+
}));
7465

75-
s.on('error',function(e){
76-
console.log('tcp server-side error: '+e.message);
77-
process.exit(1);
66+
s.on('error',(e)=>{
67+
assert.ifError(e);
7868
});
79-
});
80-
tcp.listen(tcpPort,startClient);
69+
}));
8170

71+
tcp.listen(tcpPort,startClient);
8272

8373
functionstartClient(){
8474
listenCount++;
8575
if(listenCount<2)return;
8676

87-
console.log('Making request');
88-
89-
varreq=http.request({
77+
constreq=http.request({
9078
port: common.PORT,
9179
method: 'GET',
9280
path: '/',
9381
headers: {'content-length': buffer.length}
94-
},function(res){
95-
console.log('Got response');
82+
},common.mustCall((res)=>{
9683
res.setEncoding('utf8');
97-
res.on('data',function(string){
98-
assert.equal('thanks',string);
84+
res.on('data',common.mustCall((string)=>{
85+
assert.strictEqual('thanks',string);
9986
gotThanks=true;
100-
});
101-
});
87+
}));
88+
}));
10289
req.write(buffer);
10390
req.end();
104-
console.error('ended request',req);
10591
}
10692

107-
process.on('exit',function(){
93+
process.on('exit',()=>{
10894
assert.ok(gotThanks);
109-
assert.equal(bufferSize,tcpLengthSeen);
95+
assert.strictEqual(bufferSize,tcpLengthSeen);
11096
});
11197

0 commit comments

Comments
(0)