summaryrefslogtreecommitdiff
path: root/test/parallel/test-stream2-large-read-stall.js
blob: 88fef4f24b6c301fa0a0266031e8097bf1259fdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
const common = require('../common');
const assert = require('assert');

// If everything aligns so that you do a read(n) of exactly the
// remaining buffer, then make sure that 'end' still emits.

const READSIZE = 100;
const PUSHSIZE = 20;
const PUSHCOUNT = 1000;
const HWM = 50;

const Readable = require('stream').Readable;
const r = new Readable({
  highWaterMark: HWM
});
const rs = r._readableState;

r._read = push;

r.on('readable', function() {
  console.error('>> readable');
  let ret;
  do {
    console.error('  > read(%d)', READSIZE);
    ret = r.read(READSIZE);
    console.error('  < %j (%d remain)', ret && ret.length, rs.length);
  } while (ret && ret.length === READSIZE);

  console.error('<< after read()',
                ret && ret.length,
                rs.needReadable,
                rs.length);
});

r.on('end', common.mustCall(function() {}));

let pushes = 0;
function push() {
  if (pushes > PUSHCOUNT)
    return;

  if (pushes++ === PUSHCOUNT) {
    console.error('   push(EOF)');
    return r.push(null);
  }

  console.error('   push #%d', pushes);
  if (r.push(Buffer.allocUnsafe(PUSHSIZE)))
    setTimeout(push, 1);
}

process.on('exit', function() {
  assert.equal(pushes, PUSHCOUNT + 1);
});