1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const Readable = require('stream').Readable; 5 6function noop() {} 7 8function check(readable, data, fn) { 9 assert.strictEqual(readable.readableDidRead, false); 10 if (data === -1) { 11 readable.on('error', common.mustCall()); 12 readable.on('data', common.mustNotCall()); 13 readable.on('end', common.mustNotCall()); 14 } else { 15 readable.on('error', common.mustNotCall()); 16 if (data === -2) { 17 readable.on('end', common.mustNotCall()); 18 } else { 19 readable.on('end', common.mustCall()); 20 } 21 if (data > 0) { 22 readable.on('data', common.mustCallAtLeast(data)); 23 } else { 24 readable.on('data', common.mustNotCall()); 25 } 26 } 27 readable.on('close', common.mustCall()); 28 fn(); 29 setImmediate(() => { 30 assert.strictEqual(readable.readableDidRead, true); 31 }); 32} 33 34{ 35 const readable = new Readable({ 36 read() { 37 this.push(null); 38 } 39 }); 40 check(readable, 0, () => { 41 readable.read(); 42 }); 43} 44 45{ 46 const readable = new Readable({ 47 read() { 48 this.push(null); 49 } 50 }); 51 check(readable, 0, () => { 52 readable.resume(); 53 }); 54} 55 56{ 57 const readable = new Readable({ 58 read() { 59 this.push(null); 60 } 61 }); 62 check(readable, -2, () => { 63 readable.destroy(); 64 }); 65} 66 67{ 68 const readable = new Readable({ 69 read() { 70 this.push(null); 71 } 72 }); 73 74 check(readable, -1, () => { 75 readable.destroy(new Error()); 76 }); 77} 78 79{ 80 const readable = new Readable({ 81 read() { 82 this.push('data'); 83 this.push(null); 84 } 85 }); 86 87 check(readable, 1, () => { 88 readable.on('data', noop); 89 }); 90} 91 92{ 93 const readable = new Readable({ 94 read() { 95 this.push('data'); 96 this.push(null); 97 } 98 }); 99 100 check(readable, 1, () => { 101 readable.on('data', noop); 102 readable.off('data', noop); 103 }); 104} 105