Skip to content

Commit 0ad88f6

Browse files
Ethan-Arrowoodrichardlau
authored andcommitted
test: fix flaky http-chunk-extensions-limit test
Replace the setInterval with a queueMicrotask to make test less flaky. Fixes: #51883 PR-URL: #51943 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Paolo Insogna <[email protected]>
1 parent 960d67c commit 0ad88f6

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

‎test/parallel/test-http-chunk-extensions-limit.js‎

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ const http = require('http');
55
constnet=require('net');
66
constassert=require('assert');
77

8+
// The maximum http chunk extension size is set in `src/node_http_parser.cc`.
9+
// These tests assert that once the extension size is reached, an HTTP 413
10+
// response is returned.
11+
// Currently, the max size is set to 16KiB (16384).
12+
813
// Verify that chunk extensions are limited in size when sent all together.
914
{
1015
constserver=http.createServer((req,res)=>{
@@ -17,7 +22,8 @@ const assert = require('assert');
1722
});
1823

1924
server.listen(0,()=>{
20-
constsock=net.connect(server.address().port);
25+
constport=server.address().port;
26+
constsock=net.connect(port);
2127
letdata='';
2228

2329
sock.on('data',(chunk)=>data+=chunk.toString('utf-8'));
@@ -29,15 +35,17 @@ const assert = require('assert');
2935

3036
sock.end(''+
3137
'GET / HTTP/1.1\r\n'+
32-
'Host: localhost:8080\r\n'+
38+
`Host: localhost:${port}\r\n`+
3339
'Transfer-Encoding: chunked\r\n\r\n'+
34-
'2;'+'A'.repeat(20000)+'=bar\r\nAA\r\n'+
35-
'0\r\n\r\n'
40+
'2;'+'a'.repeat(17000)+'\r\n'+// Chunk size + chunk ext + CRLF
41+
'AA\r\n'+// Chunk data
42+
'0\r\n'+// Last chunk
43+
'\r\n'// End of http message
3644
);
3745
});
3846
}
3947

40-
// Verify that chunk extensions are limited in size when sent in intervals.
48+
// Verify that chunk extensions are limited in size when sent in parts
4149
{
4250
constserver=http.createServer((req,res)=>{
4351
req.on('end',()=>{
@@ -49,24 +57,10 @@ const assert = require('assert');
4957
});
5058

5159
server.listen(0,()=>{
52-
constsock=net.connect(server.address().port);
53-
letremaining=20000;
60+
constport=server.address().port;
61+
constsock=net.connect(port);
5462
letdata='';
5563

56-
constinterval=setInterval(
57-
()=>{
58-
if(remaining>0){
59-
sock.write('A'.repeat(1000));
60-
}else{
61-
sock.write('=bar\r\nAA\r\n0\r\n\r\n');
62-
clearInterval(interval);
63-
}
64-
65-
remaining-=1000;
66-
},
67-
common.platformTimeout(20),
68-
).unref();
69-
7064
sock.on('data',(chunk)=>data+=chunk.toString('utf-8'));
7165

7266
sock.on('end',common.mustCall(function(){
@@ -76,10 +70,20 @@ const assert = require('assert');
7670

7771
sock.write(''+
7872
'GET / HTTP/1.1\r\n'+
79-
'Host: localhost:8080\r\n'+
73+
`Host: localhost:${port}\r\n`+
8074
'Transfer-Encoding: chunked\r\n\r\n'+
81-
'2;'
75+
'2;'// Chunk size + start of chunk-extension
8276
);
77+
78+
sock.write('A'.repeat(8500));// Write half of the chunk-extension
79+
80+
queueMicrotask(()=>{
81+
sock.write('A'.repeat(8500)+'\r\n'+// Remaining half of the chunk-extension
82+
'AA\r\n'+// Chunk data
83+
'0\r\n'+// Last chunk
84+
'\r\n'// End of http message
85+
);
86+
});
8387
});
8488
}
8589

@@ -95,7 +99,8 @@ const assert = require('assert');
9599
});
96100

97101
server.listen(0,()=>{
98-
constsock=net.connect(server.address().port);
102+
constport=server.address().port;
103+
constsock=net.connect(port);
99104
letdata='';
100105

101106
sock.on('data',(chunk)=>data+=chunk.toString('utf-8'));
@@ -120,7 +125,7 @@ const assert = require('assert');
120125

121126
sock.end(''+
122127
'GET / HTTP/1.1\r\n'+
123-
'Host: localhost:8080\r\n'+
128+
`Host: localhost:${port}\r\n`+
124129
'Transfer-Encoding: chunked\r\n\r\n'+
125130
'2;'+'A'.repeat(10000)+'=bar\r\nAA\r\n'+
126131
'2;'+'A'.repeat(10000)+'=bar\r\nAA\r\n'+

0 commit comments

Comments
(0)