Skip to content

Commit

Permalink
fix(captp): inband CTP_ABORT to reject all pending promises
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Oct 19, 2019
1 parent 79a930a commit fb3630a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
24 changes: 12 additions & 12 deletions lib/captp.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,17 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
}
imports.delete(promiseID);
},
CTP_ABORT(obj) {
const {exception} = obj;
unplug = true;
for (const pr of questions.values()) {
pr.rej(exception);
}
for (const pr of imports.values()) {
pr.rej(exception);
}
send(obj);
},
};

// Get a reference to the other side's bootstrap object.
Expand Down Expand Up @@ -216,16 +227,5 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
return false;
};

const disconnected = () => {
unplug = true;
const err = Error(`${ourId} disconnected`);
for (const pr of questions.values()) {
pr.rej(err);
}
for (const pr of imports.values()) {
pr.rej(err);
}
};

return harden({ dispatch, getBootstrap, disconnected });
return harden({ dispatch, getBootstrap });
}
12 changes: 9 additions & 3 deletions test/disco.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ import { harden, makeCapTP } from '../lib/captp';

test('try disconnecting captp', async t => {
try {
const { dispatch, getBootstrap, disconnected } = makeCapTP('us', obj => {}, () => harden({}));
const pr = t.rejects(getBootstrap(), Error, 'rejected after disconnect');
disconnected();
const objs = [];
const { dispatch, getBootstrap } = makeCapTP('us', obj => objs.push(obj), () => harden({}));
t.deepEqual(objs, [], 'expected no messages');
const bs = getBootstrap();
t.deepEqual(objs, [{ type: 'CTP_BOOTSTRAP', questionID: 1 }], 'expected bootstrap messages');
const pr = t.rejects(bs, Error, 'rejected after disconnect');
const abort = { type: 'CTP_ABORT', exception: Error('disconnect') };
dispatch(abort);
t.deepEqual(objs, [{ type: 'CTP_BOOTSTRAP', questionID: 1 }, abort], 'expected disconnect messages');
await pr;
} catch (e) {
t.isNot(e, e, 'unexpected exception');
Expand Down

0 comments on commit fb3630a

Please sign in to comment.