• 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
26const Readable = require('stream').Readable;
27
28{
29  // First test, not reading when the readable is added.
30  // make sure that on('readable', ...) triggers a readable event.
31  const r = new Readable({
32    highWaterMark: 3
33  });
34
35  r._read = common.mustNotCall();
36
37  // This triggers a 'readable' event, which is lost.
38  r.push(Buffer.from('blerg'));
39
40  setTimeout(function() {
41    // We're testing what we think we are
42    assert(!r._readableState.reading);
43    r.on('readable', common.mustCall());
44  }, 1);
45}
46
47{
48  // Second test, make sure that readable is re-emitted if there's
49  // already a length, while it IS reading.
50
51  const r = new Readable({
52    highWaterMark: 3
53  });
54
55  r._read = common.mustCall();
56
57  // This triggers a 'readable' event, which is lost.
58  r.push(Buffer.from('bl'));
59
60  setTimeout(function() {
61    // Assert we're testing what we think we are
62    assert(r._readableState.reading);
63    r.on('readable', common.mustCall());
64  }, 1);
65}
66
67{
68  // Third test, not reading when the stream has not passed
69  // the highWaterMark but *has* reached EOF.
70  const r = new Readable({
71    highWaterMark: 30
72  });
73
74  r._read = common.mustNotCall();
75
76  // This triggers a 'readable' event, which is lost.
77  r.push(Buffer.from('blerg'));
78  r.push(null);
79
80  setTimeout(function() {
81    // Assert we're testing what we think we are
82    assert(!r._readableState.reading);
83    r.on('readable', common.mustCall());
84  }, 1);
85}
86
87{
88  // Pushing an empty string in non-objectMode should
89  // trigger next `read()`.
90  const underlyingData = ['', 'x', 'y', '', 'z'];
91  const expected = underlyingData.filter((data) => data);
92  const result = [];
93
94  const r = new Readable({
95    encoding: 'utf8',
96  });
97  r._read = function() {
98    process.nextTick(() => {
99      if (!underlyingData.length) {
100        this.push(null);
101      } else {
102        this.push(underlyingData.shift());
103      }
104    });
105  };
106
107  r.on('readable', () => {
108    const data = r.read();
109    if (data !== null) result.push(data);
110  });
111
112  r.on('end', common.mustCall(() => {
113    assert.deepStrictEqual(result, expected);
114  }));
115}
116
117{
118  // #20923
119  const r = new Readable();
120  r._read = function() {
121    // Actually doing thing here
122  };
123  r.on('data', function() {});
124
125  r.removeAllListeners();
126
127  assert.strictEqual(r.eventNames().length, 0);
128}
129