Skip to content

Commit 71c9174

Browse files
alexjeffburkeevanlucas
authored andcommitted
test: add tests for stream3 buffering using cork
adds 2 new tests for streams3 cork behavior, cork then uncork and cork then end PR-URL: #6493 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b1f58ed commit 71c9174

2 files changed

Lines changed: 177 additions & 0 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'use strict';
2+
require('../common');
3+
constassert=require('assert');
4+
conststream=require('stream');
5+
constWritable=stream.Writable;
6+
7+
// Test the buffering behaviour of Writable streams.
8+
//
9+
// The call to cork() triggers storing chunks which are flushed
10+
// on calling end() and the stream subsequently ended.
11+
//
12+
// node version target: 0.12
13+
14+
constexpectedChunks=['please','buffer','me','kindly'];
15+
varinputChunks=expectedChunks.slice(0);
16+
varseenChunks=[];
17+
varseenEnd=false;
18+
19+
varw=newWritable();
20+
// lets arrange to store the chunks
21+
w._write=function(chunk,encoding,cb){
22+
// stream end event is not seen before the last write
23+
assert.ok(!seenEnd);
24+
// default encoding given none was specified
25+
assert.equal(encoding,'buffer');
26+
27+
seenChunks.push(chunk);
28+
cb();
29+
};
30+
// lets record the stream end event
31+
w.on('finish',()=>{
32+
seenEnd=true;
33+
});
34+
35+
functionwriteChunks(remainingChunks,callback){
36+
varwriteChunk=remainingChunks.shift();
37+
varwriteState;
38+
39+
if(writeChunk){
40+
setImmediate(()=>{
41+
writeState=w.write(writeChunk);
42+
// we were not told to stop writing
43+
assert.ok(writeState);
44+
45+
writeChunks(remainingChunks,callback);
46+
});
47+
}else{
48+
callback();
49+
}
50+
}
51+
52+
// do an initial write
53+
w.write('stuff');
54+
// the write was immediate
55+
assert.equal(seenChunks.length,1);
56+
// reset the seen chunks
57+
seenChunks=[];
58+
59+
// trigger stream buffering
60+
w.cork();
61+
62+
// write the bufferedChunks
63+
writeChunks(inputChunks,()=>{
64+
// should not have seen anything yet
65+
assert.equal(seenChunks.length,0);
66+
67+
// trigger flush and ending the stream
68+
w.end();
69+
70+
// stream should not ended in current tick
71+
assert.ok(!seenEnd);
72+
73+
// buffered bytes should be seen in current tick
74+
assert.equal(seenChunks.length,4);
75+
76+
// did the chunks match
77+
for(vari=0,l=expectedChunks.length;i<l;i++){
78+
varseen=seenChunks[i];
79+
// there was a chunk
80+
assert.ok(seen);
81+
82+
varexpected=newBuffer(expectedChunks[i]);
83+
// it was what we expected
84+
assert.ok(seen.equals(expected));
85+
}
86+
87+
setImmediate(()=>{
88+
// stream should have ended in next tick
89+
assert.ok(seenEnd);
90+
});
91+
});
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
'use strict';
2+
require('../common');
3+
constassert=require('assert');
4+
conststream=require('stream');
5+
constWritable=stream.Writable;
6+
7+
// Test the buffering behaviour of Writable streams.
8+
//
9+
// The call to cork() triggers storing chunks which are flushed
10+
// on calling uncork() in the same tick.
11+
//
12+
// node version target: 0.12
13+
14+
constexpectedChunks=['please','buffer','me','kindly'];
15+
varinputChunks=expectedChunks.slice(0);
16+
varseenChunks=[];
17+
varseenEnd=false;
18+
19+
varw=newWritable();
20+
// lets arrange to store the chunks
21+
w._write=function(chunk,encoding,cb){
22+
// default encoding given none was specified
23+
assert.equal(encoding,'buffer');
24+
25+
seenChunks.push(chunk);
26+
cb();
27+
};
28+
// lets record the stream end event
29+
w.on('finish',()=>{
30+
seenEnd=true;
31+
});
32+
33+
functionwriteChunks(remainingChunks,callback){
34+
varwriteChunk=remainingChunks.shift();
35+
varwriteState;
36+
37+
if(writeChunk){
38+
setImmediate(()=>{
39+
writeState=w.write(writeChunk);
40+
// we were not told to stop writing
41+
assert.ok(writeState);
42+
43+
writeChunks(remainingChunks,callback);
44+
});
45+
}else{
46+
callback();
47+
}
48+
}
49+
50+
// do an initial write
51+
w.write('stuff');
52+
// the write was immediate
53+
assert.equal(seenChunks.length,1);
54+
// reset the chunks seen so far
55+
seenChunks=[];
56+
57+
// trigger stream buffering
58+
w.cork();
59+
60+
// write the bufferedChunks
61+
writeChunks(inputChunks,()=>{
62+
// should not have seen anything yet
63+
assert.equal(seenChunks.length,0);
64+
65+
// trigger writing out the buffer
66+
w.uncork();
67+
68+
// buffered bytes shoud be seen in current tick
69+
assert.equal(seenChunks.length,4);
70+
71+
// did the chunks match
72+
for(vari=0,l=expectedChunks.length;i<l;i++){
73+
varseen=seenChunks[i];
74+
// there was a chunk
75+
assert.ok(seen);
76+
77+
varexpected=newBuffer(expectedChunks[i]);
78+
// it was what we expected
79+
assert.ok(seen.equals(expected));
80+
}
81+
82+
setImmediate(()=>{
83+
// the stream should not have been ended
84+
assert.ok(!seenEnd);
85+
});
86+
});

0 commit comments

Comments
 (0)