1'use strict'; 2 3const common = require('../common'); 4 5// Ensure that subscribing the 'data' event will not make the stream flow. 6// The 'data' event will require calling read() by hand. 7// 8// The test is written for the (somewhat rare) highWaterMark: 0 streams to 9// specifically catch any regressions that might occur with these streams. 10 11const assert = require('assert'); 12const { Readable } = require('stream'); 13 14const streamData = [ 'a', null ]; 15 16// Track the calls so we can assert their order later. 17const calls = []; 18const r = new Readable({ 19 read: common.mustCall(() => { 20 calls.push('_read:' + streamData[0]); 21 process.nextTick(() => { 22 calls.push('push:' + streamData[0]); 23 r.push(streamData.shift()); 24 }); 25 }, streamData.length), 26 highWaterMark: 0, 27 28 // Object mode is used here just for testing convenience. It really 29 // shouldn't affect the order of events. Just the data and its format. 30 objectMode: true, 31}); 32 33assert.strictEqual(r.readableFlowing, null); 34r.on('readable', common.mustCall(() => { 35 calls.push('readable'); 36}, 2)); 37assert.strictEqual(r.readableFlowing, false); 38r.on('data', common.mustCall((data) => { 39 calls.push('data:' + data); 40}, 1)); 41r.on('end', common.mustCall(() => { 42 calls.push('end'); 43})); 44assert.strictEqual(r.readableFlowing, false); 45 46// The stream emits the events asynchronously but that's not guaranteed to 47// happen on the next tick (especially since the _read implementation above 48// uses process.nextTick). 49// 50// We use setImmediate here to give the stream enough time to emit all the 51// events it's about to emit. 52setImmediate(() => { 53 54 // Only the _read, push, readable calls have happened. No data must be 55 // emitted yet. 56 assert.deepStrictEqual(calls, ['_read:a', 'push:a', 'readable']); 57 58 // Calling 'r.read()' should trigger the data event. 59 assert.strictEqual(r.read(), 'a'); 60 assert.deepStrictEqual( 61 calls, 62 ['_read:a', 'push:a', 'readable', 'data:a']); 63 64 // The next 'read()' will return null because hwm: 0 does not buffer any 65 // data and the _read implementation above does the push() asynchronously. 66 // 67 // Note: This 'null' signals "no data available". It isn't the end-of-stream 68 // null value as the stream doesn't know yet that it is about to reach the 69 // end. 70 // 71 // Using setImmediate again to give the stream enough time to emit all the 72 // events it wants to emit. 73 assert.strictEqual(r.read(), null); 74 setImmediate(() => { 75 76 // There's a new 'readable' event after the data has been pushed. 77 // The 'end' event will be emitted only after a 'read()'. 78 // 79 // This is somewhat special for the case where the '_read' implementation 80 // calls 'push' asynchronously. If 'push' was synchronous, the 'end' event 81 // would be emitted here _before_ we call read(). 82 assert.deepStrictEqual( 83 calls, 84 ['_read:a', 'push:a', 'readable', 'data:a', 85 '_read:null', 'push:null', 'readable']); 86 87 assert.strictEqual(r.read(), null); 88 89 // While it isn't really specified whether the 'end' event should happen 90 // synchronously with read() or not, we'll assert the current behavior 91 // ('end' event happening on the next tick after read()) so any changes 92 // to it are noted and acknowledged in the future. 93 assert.deepStrictEqual( 94 calls, 95 ['_read:a', 'push:a', 'readable', 'data:a', 96 '_read:null', 'push:null', 'readable']); 97 process.nextTick(() => { 98 assert.deepStrictEqual( 99 calls, 100 ['_read:a', 'push:a', 'readable', 'data:a', 101 '_read:null', 'push:null', 'readable', 'end']); 102 }); 103 }); 104}); 105