@@ -1793,16 +1793,91 @@ describe("endReadable() queues one tick for 'end' (nodejs/node#65310)", () => { |
1793 | 1793 | }); |
1794 | 1794 |
|
1795 | 1795 | describe("an EOF that nothing observes owes its 'readable' to a later listener (nodejs/node#65749)", () => { |
1796 | | - it("read() takes the 'readable' that is owed", async () => { |
1797 | | - // With autoDestroy, the stream is destroyed at 'end' and emits no 'readable' behind it in any case. |
1798 | | - const r = new Readable({ read() {}, autoDestroy: false }); |
| 1796 | + // Every tick of a stream runs before an immediate. |
| 1797 | + const immediate = () => new Promise(resolve => setImmediate(resolve)); |
| 1798 | + function watch(r) { |
| 1799 | + const events = []; |
| 1800 | + r.on("readable", () => events.push(`readable ${r.read()}`)); |
| 1801 | + r.on("end", () => events.push("end")); |
| 1802 | + return events; |
| 1803 | + } |
| 1804 | + |
| 1805 | + // The five cases of test-stream-readable-unobserved-eof-readable.js, which Node.js v26.9.0 has. |
| 1806 | + it("a 'readable' listener in the tick of the EOF gets 'readable', then 'end'", async () => { |
| 1807 | + const r = new Readable({ read() {} }); |
| 1808 | + r.push(null); |
| 1809 | + const events = watch(r); |
| 1810 | + await immediate(); |
| 1811 | + expect(events).toEqual(["readable null", "end"]); |
| 1812 | + }); |
| 1813 | + |
| 1814 | + it("a 'readable' listener that comes an immediate later gets 'readable', then 'end'", async () => { |
| 1815 | + const r = new Readable({ read() {} }); |
| 1816 | + r.push(null); |
| 1817 | + await immediate(); |
| 1818 | + const events = watch(r); |
| 1819 | + await immediate(); |
| 1820 | + expect(events).toEqual(["readable null", "end"]); |
| 1821 | + }); |
| 1822 | + |
| 1823 | + it("resume() an immediate later ends the stream", async () => { |
| 1824 | + const r = new Readable({ read() {} }); |
| 1825 | + r.push(null); |
| 1826 | + await immediate(); |
| 1827 | + const events = []; |
| 1828 | + r.on("end", () => events.push("end")); |
| 1829 | + r.resume(); |
| 1830 | + await immediate(); |
| 1831 | + expect(events).toEqual(["end"]); |
| 1832 | + }); |
| 1833 | + |
| 1834 | + it("read() an immediate later ends the stream", async () => { |
| 1835 | + const r = new Readable({ read() {} }); |
| 1836 | + r.push(null); |
| 1837 | + await immediate(); |
| 1838 | + const events = [`read ${r.read()}`]; |
| 1839 | + r.on("end", () => events.push("end")); |
| 1840 | + await immediate(); |
| 1841 | + expect(events).toEqual(["read null", "end"]); |
| 1842 | + }); |
| 1843 | + |
| 1844 | + it("a 'data' listener that comes an immediate later gets the chunk, then 'end'", async () => { |
| 1845 | + const r = new Readable({ read() {} }); |
| 1846 | + r.push("x"); |
1799 | 1847 | r.push(null); |
1800 | | - expect(r.read()).toBe(null); |
| 1848 | + await immediate(); |
1801 | 1849 | const events = []; |
| 1850 | + r.on("data", chunk => events.push(`data ${chunk}`)); |
| 1851 | + r.on("end", () => events.push("end")); |
| 1852 | + await immediate(); |
| 1853 | + expect(events).toEqual(["data x", "end"]); |
| 1854 | + }); |
| 1855 | + |
| 1856 | + // autoDestroy: false in the next two. A stream that 'end' destroyed emits no 'readable' in any case. |
| 1857 | + it("a read() that takes the last chunk takes the 'readable' that is owed", async () => { |
| 1858 | + const r = new Readable({ read() {}, autoDestroy: false }); |
| 1859 | + r.push("x"); |
| 1860 | + r.push(null); |
| 1861 | + const events = [`read ${r.read(1)}`]; |
1802 | 1862 | r.on("readable", () => events.push("readable")); |
1803 | 1863 | r.on("end", () => events.push("end")); |
1804 | | - // Every tick of the stream runs before an immediate. |
1805 | | - await new Promise(resolve => setImmediate(resolve)); |
| 1864 | + await immediate(); |
| 1865 | + expect(events).toEqual(["read x", "end"]); |
| 1866 | + }); |
| 1867 | + |
| 1868 | + // Node.js v26.11.1 gives ["end", "readable"]: the read(0) of resume() queues 'end' and leaves the 'readable' owed. |
| 1869 | + it("no 'readable' comes behind 'end' after resume() and pause()", async () => { |
| 1870 | + const r = new Readable({ read() {}, autoDestroy: false }); |
| 1871 | + r.push(null); |
| 1872 | + r.resume(); |
| 1873 | + r.pause(); |
| 1874 | + const events = []; |
| 1875 | + // This tick is behind the one of resume(), which queues 'end', and before the one of 'end'. |
| 1876 | + process.nextTick(() => { |
| 1877 | + r.on("readable", () => events.push("readable")); |
| 1878 | + r.on("end", () => events.push("end")); |
| 1879 | + }); |
| 1880 | + await immediate(); |
1806 | 1881 | expect(events).toEqual(["end"]); |
1807 | 1882 | }); |
1808 | 1883 | }); |
|