Skip to content

Commit

Permalink
Add tests for the new source/sink error cases
Browse files Browse the repository at this point in the history
The previous commits introduced many new places that we call in to user code, since we no longer validate that we are grabbing functions and then store them, but instead extract and invoke methods every time. This commit adds tests for a variety of pathological inputs, to ensure that the errors they throw are propagated or caught-and-exposed, as appropriate.
  • Loading branch information
domenic committed Jan 20, 2015
1 parent ff059c9 commit 959fc4e
Show file tree
Hide file tree
Showing 2 changed files with 553 additions and 0 deletions.
314 changes: 314 additions & 0 deletions reference-implementation/test/bad-underlying-sinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
var test = require('tape');

import WritableStream from '../lib/writable-stream';

test('Throwing underlying sink start getter', t => {
var theError = new Error('a unique string');

t.throws(() => {
new WritableStream({
get start() {
throw theError;
}
});
}, /a unique string/);
t.end();
});

test('Throwing underlying sink start method', t => {
var theError = new Error('a unique string');

t.throws(() => {
new WritableStream({
start() {
throw theError;
}
});
}, /a unique string/);
t.end();
});

test('Throwing underlying source write getter', t => {
t.plan(2);

var theError = new Error('a unique string');
var ws = new WritableStream({
get write() {
throw theError;
}
});

ws.write('a').then(
() => t.fail('write should not fulfill'),
r => t.equal(r, theError, 'write should reject with the thrown error')
);

ws.closed.then(
() => t.fail('closed should not fulfill'),
r => t.equal(r, theError, 'closed should reject with the thrown error')
);
});

test('Throwing underlying source write method', t => {
t.plan(2);

var theError = new Error('a unique string');
var ws = new WritableStream({
write() {
throw theError;
}
});

ws.write('a').then(
() => t.fail('write should not fulfill'),
r => t.equal(r, theError, 'write should reject with the thrown error')
);

ws.closed.then(
() => t.fail('closed should not fulfill'),
r => t.equal(r, theError, 'closed should reject with the thrown error')
);
});

test('Throwing underlying sink abort getter', t => {
t.plan(2);

var theError = new Error('a unique string');
var abortReason = new Error('different string');
var ws = new WritableStream({
get abort() {
throw theError;
}
});

ws.abort(abortReason).then(
() => t.fail('abort should not fulfill'),
r => t.equal(r, theError, 'abort should reject with the abort reason')
);

ws.closed.then(
() => t.fail('closed should not fulfill'),
r => t.equal(r, abortReason, 'closed should reject with the thrown error')
);
});

test('Throwing underlying sink abort method', t => {
t.plan(2);

var theError = new Error('a unique string');
var abortReason = new Error('different string');
var ws = new WritableStream({
abort() {
throw theError;
}
});

ws.abort(abortReason).then(
() => t.fail('abort should not fulfill'),
r => t.equal(r, theError, 'abort should reject with the abort reason')
);

ws.closed.then(
() => t.fail('closed should not fulfill'),
r => t.equal(r, abortReason, 'closed should reject with the thrown error')
);
});

test('Throwing underlying sink close getter', t => {
t.plan(1);

var theError = new Error('a unique string');
var ws = new WritableStream({
get close() {
throw theError;
}
});

ws.close().then(
() => t.fail('close should not fulfill'),
r => t.equal(r, theError, 'close should reject with the thrown error')
);
});

test('Throwing underlying sink close method', t => {
t.plan(1);

var theError = new Error('a unique string');
var ws = new WritableStream({
close() {
throw theError;
}
});

ws.close().then(
() => t.fail('close should not fulfill'),
r => t.equal(r, theError, 'close should reject with the thrown error')
);
});

test('Throwing underlying source strategy getter: initial construction', t => {
var theError = new Error('a unique string');

t.throws(() => {
new WritableStream({
get strategy() {
throw theError;
}
});
}, /a unique string/);
t.end();
});

test('Throwing underlying source strategy getter: first write', t => {
t.plan(2);

var counter = 0;
var theError = new Error('a unique string');
var ws = new WritableStream({
get strategy() {
++counter;
if (counter === 1) {
return {
size() {
return 1;
},
shouldApplyBackpressure() {
return true;
}
};
}

throw theError;
}
});

ws.write('a').then(
() => t.fail('write should not fulfill'),
r => t.equal(r, theError, 'write should reject with the thrown error')
);

ws.closed.then(
() => t.fail('closed should not fulfill'),
r => t.equal(r, theError, 'closed should reject with the thrown error')
);
});

test('Throwing underlying source strategy.size getter: initial construction', t => {
t.doesNotThrow(() => {
new WritableStream({
strategy: {
get size() {
throw new Error('boo');
},
shouldApplyBackpressure() {
return true;
}
}
});
});
t.end();
});

test('Throwing underlying source strategy.size method: initial construction', t => {
t.doesNotThrow(() => {
new WritableStream({
strategy: {
size() {
throw new Error('boo');
},
shouldApplyBackpressure() {
return true;
}
}
});
});
t.end();
});

test('Throwing underlying source strategy.size getter: first write', t => {
t.plan(2);

var theError = new Error('a unique string');
var ws = new WritableStream({
strategy: {
get size() {
throw theError;
},
shouldApplyBackpressure() {
return true;
}
}
});

ws.write('a').then(
() => t.fail('write should not fulfill'),
r => t.equal(r, theError, 'write should reject with the thrown error')
);

ws.closed.then(
() => t.fail('closed should not fulfill'),
r => t.equal(r, theError, 'closed should reject with the thrown error')
);
});

test('Throwing underlying source strategy.size method: first write', t => {
t.plan(2);

var theError = new Error('a unique string');
var ws = new WritableStream({
strategy: {
size() {
throw theError;
},
shouldApplyBackpressure() {
return true;
}
}
});

ws.write('a').then(
() => t.fail('write should not fulfill'),
r => t.equal(r, theError, 'write should reject with the thrown error')
);

ws.closed.then(
() => t.fail('closed should not fulfill'),
r => t.equal(r, theError, 'closed should reject with the thrown error')
);
});

test('Throwing underlying source strategy.shouldApplyBackpressure getter: initial construction', t => {
var theError = new Error('a unique string');

t.throws(() => {
new WritableStream({
strategy: {
size() {
return 1;
},
get shouldApplyBackpressure() {
throw theError;
}
}
});
}, /a unique string/);
t.end();
});

test('Throwing underlying source strategy.shouldApplyBackpressure method: initial construction', t => {
var theError = new Error('a unique string');

t.throws(() => {
new WritableStream({
strategy: {
size() {
return 1;
},
shouldApplyBackpressure() {
throw theError;
}
}
});
}, /a unique string/);
t.end();
});
Loading

0 comments on commit 959fc4e

Please sign in to comment.