Skip to content

Commit

Permalink
fix(captp): make more code paths fail on disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Jul 29, 2020
1 parent e16335e commit 5c0c509
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
15 changes: 12 additions & 3 deletions packages/captp/lib/captp.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export { E, HandledPromise, Nat };
export function makeCapTP(ourId, rawSend, bootstrapObj = undefined) {
let unplug = false;
function send(...args) {
if (unplug) {
if (unplug !== false) {
throw unplug;
}
return rawSend(...args);
Expand Down Expand Up @@ -114,6 +114,9 @@ export function makeCapTP(ourId, rawSend, bootstrapObj = undefined) {
// as also being questions / remote handled promises
const handler = {
get(_o, prop) {
if (unplug !== false) {
throw unplug;
}
const [questionID, pr] = makeQuestion();
send({
type: 'CTP_CALL',
Expand All @@ -124,6 +127,9 @@ export function makeCapTP(ourId, rawSend, bootstrapObj = undefined) {
return harden(pr.p);
},
applyMethod(_o, prop, args) {
if (unplug !== false) {
throw unplug;
}
// Support: o~.[prop](...args) remote method invocation
const [questionID, pr] = makeQuestion();
send({
Expand Down Expand Up @@ -271,7 +277,10 @@ export function makeCapTP(ourId, rawSend, bootstrapObj = undefined) {
};

// Get a reference to the other side's bootstrap object.
const getBootstrap = () => {
const getBootstrap = async () => {
if (unplug !== false) {
throw unplug;
}
const [questionID, pr] = makeQuestion();
send({
type: 'CTP_BOOTSTRAP',
Expand All @@ -283,7 +292,7 @@ export function makeCapTP(ourId, rawSend, bootstrapObj = undefined) {

// Return a dispatch function.
const dispatch = obj => {
if (unplug) {
if (unplug !== false) {
return false;
}
const fn = handler[obj.type];
Expand Down
19 changes: 15 additions & 4 deletions packages/captp/test/disco.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,43 @@

import '@agoric/install-ses';
import { test } from 'tape-promise/tape';
import { makeCapTP } from '../lib/captp';
import { E, makeCapTP } from '../lib/captp';

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

0 comments on commit 5c0c509

Please sign in to comment.