|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const Readable = require('stream').Readable; |
| 5 | + |
| 6 | +const readable = new Readable({ |
| 7 | + read(size) {} |
| 8 | +}); |
| 9 | + |
| 10 | +const state = readable._readableState; |
| 11 | + |
| 12 | +// Starting off with false initially. |
| 13 | +assert.strictEqual(state.reading, false); |
| 14 | +assert.strictEqual(state.readingMore, false); |
| 15 | + |
| 16 | +readable.on('data', common.mustCall((data) => { |
| 17 | + // while in a flowing state, should try to read more. |
| 18 | + if (state.flowing) |
| 19 | + assert.strictEqual(state.readingMore, true); |
| 20 | + |
| 21 | + // reading as long as we've not ended |
| 22 | + assert.strictEqual(state.reading, !state.ended); |
| 23 | +}, 2)); |
| 24 | + |
| 25 | +function onStreamEnd() { |
| 26 | + // End of stream; state.reading is false |
| 27 | + // And so should be readingMore. |
| 28 | + assert.strictEqual(state.readingMore, false); |
| 29 | + assert.strictEqual(state.reading, false); |
| 30 | +} |
| 31 | + |
| 32 | +readable.on('readable', common.mustCall(() => { |
| 33 | + // 'readable' always gets called before 'end' |
| 34 | + // since 'end' hasn't been emitted, more data could be incoming |
| 35 | + assert.strictEqual(state.readingMore, true); |
| 36 | + |
| 37 | + // if the stream has ended, we shouldn't be reading |
| 38 | + assert.strictEqual(state.ended, !state.reading); |
| 39 | + |
| 40 | + if (readable.read() === null) // reached end of stream |
| 41 | + process.nextTick(common.mustCall(onStreamEnd, 1)); |
| 42 | +}, 2)); |
| 43 | + |
| 44 | +readable.on('end', common.mustCall(onStreamEnd)); |
| 45 | + |
| 46 | +readable.push('pushed'); |
| 47 | + |
| 48 | +// stop emitting 'data' events |
| 49 | +readable.pause(); |
| 50 | + |
| 51 | +// read() should only be called while operating in paused mode |
| 52 | +readable.read(6); |
| 53 | + |
| 54 | +// reading |
| 55 | +assert.strictEqual(state.reading, true); |
| 56 | +assert.strictEqual(state.readingMore, true); |
| 57 | + |
| 58 | +// resume emitting 'data' events |
| 59 | +readable.resume(); |
| 60 | + |
| 61 | +// add chunk to front |
| 62 | +readable.unshift('unshifted'); |
| 63 | + |
| 64 | +// end |
| 65 | +readable.push(null); |
0 commit comments