From 141cbfd4af6b07076a5f8fff533f086e6ab73706 Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Mon, 15 Dec 2014 16:43:36 -0500 Subject: [PATCH] Update tests for and implementation of pipeTo for getReader change Now you cannot pipe from a closed or errored stream. That seems good? --- .../lib/readable-stream.js | 3 +- reference-implementation/test/pipe-to.js | 43 +++++++------------ 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/reference-implementation/lib/readable-stream.js b/reference-implementation/lib/readable-stream.js index 6316f5593..356cc9188 100644 --- a/reference-implementation/lib/readable-stream.js +++ b/reference-implementation/lib/readable-stream.js @@ -111,11 +111,11 @@ export default class ReadableStream { } pipeTo(dest, { preventClose, preventAbort, preventCancel } = {}) { - var source = this.getReader(); preventClose = Boolean(preventClose); preventAbort = Boolean(preventAbort); preventCancel = Boolean(preventCancel); + var source; var resolvePipeToPromise; var rejectPipeToPromise; @@ -123,6 +123,7 @@ export default class ReadableStream { resolvePipeToPromise = resolve; rejectPipeToPromise = reject; + source = this.getReader(); doPipe(); }); diff --git a/reference-implementation/test/pipe-to.js b/reference-implementation/test/pipe-to.js index 294c92ca9..c29bfcd44 100644 --- a/reference-implementation/test/pipe-to.js +++ b/reference-implementation/test/pipe-to.js @@ -128,34 +128,30 @@ test('Piping from a ReadableStream in readable state to a WritableStream in erro }); test('Piping from a ReadableStream in closed state to a WritableStream in writable state', t => { + t.plan(3); + var rs = new ReadableStream({ start(enqueue, close) { close(); }, pull() { t.fail('Unexpected pull call'); - t.end(); }, cancel(reason) { t.fail('Unexpected cancel call'); - t.end(); } }); t.equal(rs.state, 'closed'); - var closeCalled = false; var ws = new WritableStream({ write() { t.fail('Unexpected write call'); - t.end(); }, close() { - t.assert(!closeCalled); - closeCalled = true; + t.fail('Unexpected close call'); }, abort() { t.fail('Unexpected abort call'); - t.end(); } }); @@ -163,42 +159,39 @@ test('Piping from a ReadableStream in closed state to a WritableStream in writab setTimeout(() => { t.equal(ws.state, 'writable'); - rs.pipeTo(ws); - t.assert(closeCalled); - t.equal(ws.state, 'closing'); - t.end(); + rs.pipeTo(ws).then( + () => t.fail('pipeTo promise should not be fulfilled'), + e => t.equal(e.constructor, TypeError, 'pipeTo promise should be rejected with a TypeError') + ); }, 0); }); test('Piping from a ReadableStream in errored state to a WritableStream in writable state', t => { + t.plan(3); + + var theError = new Error('piping is too hard today'); var rs = new ReadableStream({ start(enqueue, close, error) { - error(); + error(theError); }, pull() { t.fail('Unexpected pull call'); - t.end(); }, cancel(reason) { t.fail('Unexpected cancel call'); - t.end(); } }); t.equal(rs.state, 'errored'); - var abortCalled = false; var ws = new WritableStream({ write() { t.fail('Unexpected write call'); - t.end(); }, close() { t.fail('Unexpected close call'); - t.end(); }, abort() { - t.assert(!abortCalled); - abortCalled = true; + t.fail('Unexpected abort call'); } }); @@ -206,14 +199,10 @@ test('Piping from a ReadableStream in errored state to a WritableStream in writa setTimeout(() => { t.equal(ws.state, 'writable'); - rs.pipeTo(ws); - - // Need to delay because pipeTo retrieves error from dest using ready. - setTimeout(() => { - t.assert(abortCalled); - t.equal(ws.state, 'errored'); - t.end(); - }, 0); + rs.pipeTo(ws).then( + () => t.fail('pipeTo promise should not be fulfilled'), + e => t.equal(e, theError, 'pipeTo promise should be rejected with the passed error') + ); }, 0); });