• 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';
23const common = require('../common');
24const assert = require('assert');
25
26// If everything aligns so that you do a read(n) of exactly the
27// remaining buffer, then make sure that 'end' still emits.
28
29const READSIZE = 100;
30const PUSHSIZE = 20;
31const PUSHCOUNT = 1000;
32const HWM = 50;
33
34const Readable = require('stream').Readable;
35const r = new Readable({
36  highWaterMark: HWM
37});
38const rs = r._readableState;
39
40r._read = push;
41
42r.on('readable', function() {
43  console.error('>> readable');
44  let ret;
45  do {
46    console.error(`  > read(${READSIZE})`);
47    ret = r.read(READSIZE);
48    console.error(`  < ${ret && ret.length} (${rs.length} remain)`);
49  } while (ret && ret.length === READSIZE);
50
51  console.error('<< after read()',
52                ret && ret.length,
53                rs.needReadable,
54                rs.length);
55});
56
57r.on('end', common.mustCall(function() {
58  assert.strictEqual(pushes, PUSHCOUNT + 1);
59}));
60
61let pushes = 0;
62function push() {
63  if (pushes > PUSHCOUNT)
64    return;
65
66  if (pushes++ === PUSHCOUNT) {
67    console.error('   push(EOF)');
68    return r.push(null);
69  }
70
71  console.error(`   push #${pushes}`);
72  if (r.push(Buffer.allocUnsafe(PUSHSIZE)))
73    setTimeout(push, 1);
74}
75