revise installing a license file
[platform/upstream/nodejs.git] / test / parallel / test-stream-big-packet.js
1 'use strict';
2 require('../common');
3 var assert = require('assert');
4 var util = require('util');
5 var stream = require('stream');
6
7 var passed = false;
8
9 function PassThrough() {
10   stream.Transform.call(this);
11 }
12 util.inherits(PassThrough, stream.Transform);
13 PassThrough.prototype._transform = function(chunk, encoding, done) {
14   this.push(chunk);
15   done();
16 };
17
18 function TestStream() {
19   stream.Transform.call(this);
20 }
21 util.inherits(TestStream, stream.Transform);
22 TestStream.prototype._transform = function(chunk, encoding, done) {
23   if (!passed) {
24     // Char 'a' only exists in the last write
25     passed = chunk.toString().indexOf('a') >= 0;
26   }
27   done();
28 };
29
30 var s1 = new PassThrough();
31 var s2 = new PassThrough();
32 var s3 = new TestStream();
33 s1.pipe(s3);
34 // Don't let s2 auto close which may close s3
35 s2.pipe(s3, {end: false});
36
37 // We must write a buffer larger than highWaterMark
38 var big = new Buffer(s1._writableState.highWaterMark + 1);
39 big.fill('x');
40
41 // Since big is larger than highWaterMark, it will be buffered internally.
42 assert(!s1.write(big));
43 // 'tiny' is small enough to pass through internal buffer.
44 assert(s2.write('tiny'));
45
46 // Write some small data in next IO loop, which will never be written to s3
47 // Because 'drain' event is not emitted from s1 and s1 is still paused
48 setImmediate(s1.write.bind(s1), 'later');
49
50 // Assert after two IO loops when all operations have been done.
51 process.on('exit', function() {
52   assert(passed, 'Large buffer is not handled properly by Writable Stream');
53 });