• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23require('../common');
24const assert = require('assert');
25
26// This test verifies that passing a huge number to read(size)
27// will push up the highWaterMark, and cause the stream to read
28// more data continuously, but without triggering a nextTick
29// warning or RangeError.
30
31const Readable = require('stream').Readable;
32
33// Throw an error if we trigger a nextTick warning.
34process.throwDeprecation = true;
35
36const stream = new Readable({ highWaterMark: 2 });
37let reads = 0;
38let total = 5000;
39stream._read = function(size) {
40  reads++;
41  size = Math.min(size, total);
42  total -= size;
43  if (size === 0)
44    stream.push(null);
45  else
46    stream.push(Buffer.allocUnsafe(size));
47};
48
49let depth = 0;
50
51function flow(stream, size, callback) {
52  depth += 1;
53  const chunk = stream.read(size);
54
55  if (!chunk)
56    stream.once('readable', flow.bind(null, stream, size, callback));
57  else
58    callback(chunk);
59
60  depth -= 1;
61  console.log(`flow(${depth}): exit`);
62}
63
64flow(stream, 5000, function() {
65  console.log(`complete (${depth})`);
66});
67
68process.on('exit', function(code) {
69  assert.strictEqual(reads, 2);
70  // We pushed up the high water mark
71  assert.strictEqual(stream.readableHighWaterMark, 8192);
72  // Length is 0 right now, because we pulled it all out.
73  assert.strictEqual(stream.readableLength, 0);
74  assert(!code);
75  assert.strictEqual(depth, 0);
76  console.log('ok');
77});
78