summaryrefslogtreecommitdiff
path: root/test/sequential/test-stream-writable-clear-buffer.js
blob: dc859e3fb6b36289facf5e2b67184f2a01cea8b6 (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
'use strict';
const common = require('../common');
const Stream = require('stream');
// This test ensures that the _writeableState.bufferedRequestCount and
// the actual buffered request count are the same
const assert = require('assert');

class StreamWritable extends Stream.Writable {
  constructor() {
    super({ objectMode: true });
  }

  // We need a timeout like on the original issue thread
  // otherwise the code will never reach our test case
  // this means this should go on the sequential folder.
  _write(chunk, encoding, cb) {
    setTimeout(cb, common.platformTimeout(10));
  }
}

const testStream = new StreamWritable();
testStream.cork();

for (let i = 1; i <= 5; i++) {
  testStream.write(i, function() {
    assert.strictEqual(
      testStream._writableState.bufferedRequestCount,
      testStream._writableState.getBuffer().length,
      'bufferedRequestCount variable is different from the actual length of' +
      ' the buffer');
  });
}

testStream.end();