Skip to content

Commit

Permalink
Update tests for and implementation of pipeTo for getReader change
Browse files Browse the repository at this point in the history
Now you cannot pipe from a closed or errored stream. That seems good?
  • Loading branch information
domenic committed Dec 15, 2014
1 parent b2d4194 commit 141cbfd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
3 changes: 2 additions & 1 deletion reference-implementation/lib/readable-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,19 @@ 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;

return new Promise((resolve, reject) => {
resolvePipeToPromise = resolve;
rejectPipeToPromise = reject;

source = this.getReader();
doPipe();
});

Expand Down
43 changes: 16 additions & 27 deletions reference-implementation/test/pipe-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,92 +128,81 @@ 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();
}
});

// Wait for ws to start.
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');
}
});

// Wait for ws to start.
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);
});

Expand Down

0 comments on commit 141cbfd

Please sign in to comment.