diff --git a/packages/daemon/index.js b/packages/daemon/index.js index 7ff05b36ad..5ce14b6c94 100644 --- a/packages/daemon/index.js +++ b/packages/daemon/index.js @@ -149,15 +149,14 @@ export const clean = async (locator = defaultLocator) => { } }; -export const restart = async (locator = defaultLocator) => { +export const stop = async (locator = defaultLocator) => { await terminate(locator).catch(() => {}); await clean(locator); - return start(locator); }; -export const stop = async (locator = defaultLocator) => { - await terminate(locator).catch(() => {}); - await clean(locator); +export const restart = async (locator = defaultLocator) => { + await stop(locator); + return start(locator); }; export const purge = async (locator = defaultLocator) => { diff --git a/packages/daemon/src/formula-type.js b/packages/daemon/src/formula-type.js index 44dcbe9d17..78e7dc9c62 100644 --- a/packages/daemon/src/formula-type.js +++ b/packages/daemon/src/formula-type.js @@ -1,3 +1,5 @@ +// @ts-check + const { quote: q } = assert; // Note: Alphabetically sorted diff --git a/packages/daemon/test/test-endo.js b/packages/daemon/test/test-endo.js index faecb05fb7..d54ff63906 100644 --- a/packages/daemon/test/test-endo.js +++ b/packages/daemon/test/test-endo.js @@ -17,7 +17,6 @@ import { start, stop, restart, - clean, purge, makeEndoClient, makeReaderRef, @@ -51,19 +50,23 @@ const makeLocator = (...root) => { * @param {ReturnType} locator * @param {Promise} cancelled */ -const makeHostWithTestNetwork = async (locator, cancelled) => { - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); - +const makeHost = async (locator, cancelled) => { const { getBootstrap } = await makeEndoClient( 'client', locator.sockPath, cancelled, ); const bootstrap = getBootstrap(); + return { host: E(bootstrap).host() }; +}; + +/** + * @param {ReturnType} locator + * @param {Promise} cancelled + */ +const makeHostWithTestNetwork = async (locator, cancelled) => { + const { host } = await makeHost(locator, cancelled); - const host = E(bootstrap).host(); // Install test network const servicePath = path.join(dirname, 'src', 'networks', 'tcp-netstring.js'); const serviceLocation = url.pathToFileURL(servicePath).href; @@ -118,15 +121,66 @@ const doMakeBundle = async (host, filePath, callback) => { return result; }; -test('lifecycle', async t => { +let locatorPathId = 0; + +/** + * @param {string} testTitle - The title of the current test. + * @param {number} locatorNumber - The number of the current locator. If this + * is the n:th locator created for the current test, the locator number is n. + */ +const getLocatorDirectoryName = (testTitle, locatorNumber) => { + const defaultPath = testTitle.replace(/\s/giu, '-').replace(/[^\w-]/giu, ''); + + // We truncate the subdirectory name to 30 characters in an attempt to respect + // the maximum Unix domain socket path length. + // With our apologies to John Jacob Jingleheimerschmidt, for whom this may + // not be enough. + const basePath = + defaultPath.length <= 22 ? defaultPath : defaultPath.slice(0, 22); + const testId = String(locatorPathId).padStart(4, '0'); + const locatorId = String(locatorNumber).padStart(2, '0'); + const locatorSubDirectory = `${basePath}#${testId}-${locatorId}`; + + locatorPathId += 1; + + return locatorSubDirectory; +}; + +/** @param {import('ava').ExecutionContext} t */ +const prepareLocator = async t => { const { reject: cancel, promise: cancelled } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'lifecycle'); + const locator = makeLocator( + 'tmp', + getLocatorDirectoryName(t.title, t.context.length), + ); await stop(locator).catch(() => {}); await purge(locator); - await clean(locator); await start(locator); + + const contextObj = { cancel, cancelled, locator }; + t.context.push(contextObj); + return { ...contextObj }; +}; + +test.beforeEach(t => { + t.context = []; +}); + +test.afterEach.always(async t => { + await Promise.allSettled( + /** @type {Awaited>[]} */ (t.context).flatMap( + ({ cancel, cancelled, locator }) => { + cancel(Error('teardown')); + return [cancelled, stop(locator)]; + }, + ), + ); +}); + +test('lifecycle', async t => { + const { cancel, cancelled, locator } = await prepareLocator(t); + await stop(locator); await restart(locator); @@ -142,94 +196,39 @@ test('lifecycle', async t => { cancel(new Error('Cancelled')); await closed.catch(() => {}); - await stop(locator); t.pass(); }); test('spawn and evaluate', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'spawn-eval'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); await E(host).provideWorker('w1'); const ten = await E(host).evaluate('w1', '10', [], []); t.is(ten, 10); - - await stop(locator); }); test('anonymous spawn and evaluate', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'spawn-eval-anon'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); const ten = await E(host).evaluate('MAIN', '10', [], []); t.is(ten, 10); - - await stop(locator); }); test('anonymous spawn and evaluate with new worker', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'spawn-eval-anon-new-worker'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); const ten = await E(host).evaluate('NEW', '10', [], []); t.is(ten, 10); - - await stop(locator); }); // Regression test for https://github.com/endojs/endo/issues/2147 test('spawning a worker does not overwrite existing non-worker name', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'spawn-eval-name-reuse'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); const foo = await E(host).evaluate('MAIN', '10', [], [], 'foo'); t.is(foo, 10); @@ -240,27 +239,13 @@ test('spawning a worker does not overwrite existing non-worker name', async t => message: 'Cannot deliver "evaluate" to target; typeof target is "undefined"', }); - - await stop(locator); }); test('persist spawn and evaluation', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'persist-spawn-eval'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); { - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { host } = await makeHost(locator, cancelled); await E(host).provideWorker('w1'); @@ -285,63 +270,28 @@ test('persist spawn and evaluation', async t => { await restart(locator); { - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { host } = await makeHost(locator, cancelled); const retwenty = await E(host).lookup('twenty'); t.is(20, retwenty); } - - await stop(locator); }); test('store without name', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'store-without-name'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); const readerRef = makeReaderRef([new TextEncoder().encode('hello\n')]); const readable = await E(host).store(readerRef); const actualText = await E(readable).text(); t.is(actualText, 'hello\n'); - - await stop(locator); }); test('store with name', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'store-with-name'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); { - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { host } = await makeHost(locator, cancelled); const readerRef = makeReaderRef([new TextEncoder().encode('hello\n')]); const readable = await E(host).store(readerRef, 'hello-text'); const actualText = await E(readable).text(); @@ -349,38 +299,18 @@ test('store with name', async t => { } { - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { host } = await makeHost(locator, cancelled); const readable = await E(host).lookup('hello-text'); const actualText = await E(readable).text(); t.is(actualText, 'hello\n'); } - - await stop(locator); }); test('closure state lost by restart', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'restart-closures'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); { - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { host } = await makeHost(locator, cancelled); await E(host).provideWorker('w1'); await E(host).evaluate( @@ -430,13 +360,7 @@ test('closure state lost by restart', async t => { await restart(locator); { - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { host } = await makeHost(locator, cancelled); await E(host).lookup('w1'); const one = await E(host).evaluate( 'w1', @@ -460,31 +384,18 @@ test('closure state lost by restart', async t => { t.is(two, 2); t.is(three, 3); } - - await stop(locator); }); test('persist unconfined services and their requests', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'make-unconfined'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); const responderFinished = (async () => { const { promise: followerCancelled, reject: cancelFollower } = makePromiseKit(); cancelled.catch(cancelFollower); - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - followerCancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { host } = await makeHost(locator, followerCancelled); await E(host).provideWorker('user-worker'); + await E(host).evaluate( 'user-worker', ` @@ -504,15 +415,10 @@ test('persist unconfined services and their requests', async t => { })(); const requesterFinished = (async () => { - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { host } = await makeHost(locator, cancelled); await E(host).provideWorker('w1'); await E(host).provideGuest('o1'); + const servicePath = path.join(dirname, 'test', 'service.js'); const serviceLocation = url.pathToFileURL(servicePath).href; await E(host).makeUnconfined('w1', serviceLocation, 'o1', 's1'); @@ -534,42 +440,23 @@ test('persist unconfined services and their requests', async t => { await restart(locator); { - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { host } = await makeHost(locator, cancelled); const answer = await E(host).lookup('answer'); const number = await E(answer).value(); t.is(number, 42); } - - await stop(locator); }); test('persist confined services and their requests', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'make-bundle'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); const responderFinished = (async () => { const { promise: followerCancelled, reject: cancelFollower } = makePromiseKit(); cancelled.catch(cancelFollower); - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - followerCancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { host } = await makeHost(locator, followerCancelled); await E(host).provideWorker('user-worker'); + await E(host).evaluate( 'user-worker', ` @@ -589,15 +476,10 @@ test('persist confined services and their requests', async t => { })(); const requesterFinished = (async () => { - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { host } = await makeHost(locator, cancelled); await E(host).provideWorker('w1'); await E(host).provideGuest('o1'); + const servicePath = path.join(dirname, 'test', 'service.js'); await doMakeBundle(host, servicePath, bundleName => E(host).makeBundle('w1', bundleName, 'o1', 's1'), @@ -620,36 +502,17 @@ test('persist confined services and their requests', async t => { await restart(locator); { - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { host } = await makeHost(locator, cancelled); const answer = await E(host).lookup('answer'); const number = await E(answer).value(); t.is(number, 42); } - - await stop(locator); }); test('guest facet receives a message for host', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const locator = makeLocator('tmp', 'guest-sends-host'); - - await start(locator); - - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); const guest = E(host).provideGuest('guest'); await E(host).provideWorker('worker'); await E(host).evaluate('worker', '10', [], [], 'ten1'); @@ -687,26 +550,12 @@ test('guest facet receives a message for host', async t => { { type: 'package', who: 'SELF', dest: 'HOST' }, ], ); - - await stop(locator); }); test('direct cancellation', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const locator = makeLocator('tmp', 'cancellation-direct'); - - await start(locator); - t.teardown(() => stop(locator)); - - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); await E(host).provideWorker('worker'); const counterPath = path.join(dirname, 'test', 'counter.js'); @@ -772,21 +621,9 @@ test('direct cancellation', async t => { // Regression test 1 for https://github.com/endojs/endo/issues/2074 test('indirect cancellation via worker', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - - const locator = makeLocator('tmp', 'cancellation-indirect-worker'); - - await start(locator); - t.teardown(() => stop(locator)); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); await E(host).provideWorker('worker'); const counterPath = path.join(dirname, 'test', 'counter.js'); @@ -853,21 +690,8 @@ test('indirect cancellation via worker', async t => { // Regression test 2 for https://github.com/endojs/endo/issues/2074 test.failing('indirect cancellation via caplet', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - - const locator = makeLocator('tmp', 'cancellation-indirect-caplet'); - - await start(locator); - t.teardown(() => stop(locator)); - - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); await E(host).provideWorker('w1'); const counterPath = path.join(dirname, 'test', 'counter.js'); @@ -907,21 +731,9 @@ test.failing('indirect cancellation via caplet', async t => { }); test('cancel because of requested capability', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - - const locator = makeLocator('tmp', 'cancellation-via-request'); - - await start(locator); - t.teardown(() => stop(locator)); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); await E(host).provideWorker('worker'); await E(host).provideGuest('guest'); @@ -995,21 +807,9 @@ test('cancel because of requested capability', async t => { }); test('unconfined service can respond to cancellation', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - - const locator = makeLocator('tmp', 'cancellation-unconfined-response'); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - await start(locator); - t.teardown(() => stop(locator)); - - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); await E(host).provideWorker('worker'); const capletPath = path.join(dirname, 'test', 'context-consumer.js'); @@ -1032,21 +832,9 @@ test('unconfined service can respond to cancellation', async t => { }); test('confined service can respond to cancellation', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - - const locator = makeLocator('tmp', 'cancellation-confined-response'); - - await start(locator); - t.teardown(() => stop(locator)); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); await E(host).provideWorker('worker'); const capletPath = path.join(dirname, 'test', 'context-consumer.js'); @@ -1065,45 +853,19 @@ test('confined service can respond to cancellation', async t => { }); test('make a host', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'make-host'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); const host2 = E(host).provideHost('fellow-host'); await E(host2).provideWorker('w1'); const ten = await E(host2).evaluate('w1', '10', [], []); t.is(ten, 10); - - await stop(locator); }); test('name and reuse inspector', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'inspector-reuse'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); await E(host).provideWorker('worker'); const counterPath = path.join(dirname, 'test', 'counter.js'); @@ -1125,27 +887,13 @@ test('name and reuse inspector', async t => { ['inspector'], ); t.regex(String(worker), /Alleged: EndoWorker/u); - - await stop(locator); }); // Regression test for https://github.com/endojs/endo/issues/2021 test.failing('eval-mediated worker name', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'eval-worker-name'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); await E(host).provideWorker('worker'); const counterPath = path.join(dirname, 'test', 'counter.js'); @@ -1182,26 +930,12 @@ test.failing('eval-mediated worker name', async t => { ), 2, ); - - await stop(locator); }); test('lookup with single petname', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'lookup-single-petname'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); await E(host).provideGuest('guest'); const ten = await E(host).evaluate('MAIN', '10', [], [], 'ten'); t.is(ten, 10); @@ -1213,26 +947,12 @@ test('lookup with single petname', async t => { ['SELF'], ); t.is(resolvedValue, 10); - - await stop(locator); }); test('lookup with petname path (inspector)', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'lookup-petname-path-inspector'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); await E(host).evaluate('MAIN', '10', [], [], 'ten'); const resolvedValue = await E(host).evaluate( @@ -1242,26 +962,11 @@ test('lookup with petname path (inspector)', async t => { ['SELF'], ); t.is(resolvedValue, '10'); - - await stop(locator); }); test('lookup with petname path (caplet with lookup method)', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'lookup-petname-path-caplet'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); - - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); const lookupPath = path.join(dirname, 'test', 'lookup.js'); await E(host).makeUnconfined('MAIN', lookupPath, 'NONE', 'lookup'); @@ -1273,26 +978,11 @@ test('lookup with petname path (caplet with lookup method)', async t => { ['SELF'], ); t.is(resolvedValue, 'Looked up: name'); - - await stop(locator); }); test('lookup with petname path (value has no lookup method)', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'lookup-petname-path-no-method'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); - - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); await E(host).evaluate('MAIN', '10', [], [], 'ten'); await t.throwsAsync( @@ -1304,26 +994,11 @@ test('lookup with petname path (value has no lookup method)', async t => { ), { message: 'target has no method "lookup", has []' }, ); - - await stop(locator); }); test('evaluate name resolved by lookup path', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'name-resolved-by-lookup-path'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); - - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); await E(host).evaluate('MAIN', '10', [], [], 'ten'); @@ -1334,26 +1009,11 @@ test('evaluate name resolved by lookup path', async t => { [['INFO', 'ten', 'source']], ); t.is(resolvedValue, '10'); - - await stop(locator); }); test('list special names', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'list-names'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); - - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); const readerRef = makeReaderRef([new TextEncoder().encode('hello\n')]); await E(host).store(readerRef, 'hello-text'); @@ -1372,21 +1032,9 @@ test('list special names', async t => { }); test('guest cannot access host methods', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const locator = makeLocator('tmp', 'guest-cannot-host'); - - await start(locator); - t.teardown(() => stop(locator)); - - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); const guest = E(host).provideGuest('guest'); const guestsHost = E(guest).lookup('HOST'); await t.throwsAsync(() => E(guestsHost).lookup('SELF'), { @@ -1396,22 +1044,9 @@ test('guest cannot access host methods', async t => { t.is(revealedTarget, undefined); }); -test('read unknown nodeId', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'read-unknown-nodeid'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); - - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); +test('read unknown node id', async t => { + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); // write a bogus value for a bogus nodeId const node = await cryptoPowers.randomHex512(); @@ -1421,21 +1056,18 @@ test('read unknown nodeId', async t => { number, }); await E(host).write(['abc'], id); + // observe reification failure - t.throwsAsync(() => E(host).lookup('abc'), { + await t.throwsAsync(() => E(host).lookup('abc'), { message: /No peer found for node identifier /u, }); - - await stop(locator); }); test('read remote value', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locatorA = makeLocator('tmp', 'read-remote-value-a'); - const locatorB = makeLocator('tmp', 'read-remote-value-b'); - const hostA = await makeHostWithTestNetwork(locatorA, cancelled); - const hostB = await makeHostWithTestNetwork(locatorB, cancelled); + const { locator: locatorA, cancelled: cancelledA } = await prepareLocator(t); + const { locator: locatorB, cancelled: cancelledB } = await prepareLocator(t); + const hostA = await makeHostWithTestNetwork(locatorA, cancelledA); + const hostB = await makeHostWithTestNetwork(locatorB, cancelledB); // introduce nodes to each other await E(hostA).addPeerInfo(await E(hostB).getPeerInfo()); @@ -1450,54 +1082,25 @@ test('read remote value', async t => { const hostAValue = await E(hostA).lookup('greetings'); t.is(hostAValue, 'hello, world!'); - - await stop(locatorA); - await stop(locatorB); }); test('locate local value', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'locate-local-value'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); + const { host } = await makeHost(locator, cancelled); - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); const ten = await E(host).evaluate('MAIN', '10', [], [], 'ten'); t.is(ten, 10); const tenLocator = await E(host).locate('ten'); const parsedLocator = parseLocator(tenLocator); t.is(parsedLocator.formulaType, 'eval'); - - await stop(locator); }); test('locate local persisted value', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locator = makeLocator('tmp', 'locate-local-persisted-value'); - - await stop(locator).catch(() => {}); - await purge(locator); - await start(locator); + const { cancelled, locator } = await prepareLocator(t); { - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { host } = await makeHost(locator, cancelled); const ten = await E(host).evaluate('MAIN', '10', [], [], 'ten'); t.is(ten, 10); } @@ -1505,28 +1108,18 @@ test('locate local persisted value', async t => { await restart(locator); { - const { getBootstrap } = await makeEndoClient( - 'client', - locator.sockPath, - cancelled, - ); - const bootstrap = getBootstrap(); - const host = E(bootstrap).host(); + const { host } = await makeHost(locator, cancelled); const tenLocator = await E(host).locate('ten'); const parsedLocator = parseLocator(tenLocator); t.is(parsedLocator.formulaType, 'eval'); } - - await stop(locator); }); test('locate remote value', async t => { - const { promise: cancelled, reject: cancel } = makePromiseKit(); - t.teardown(() => cancel(Error('teardown'))); - const locatorA = makeLocator('tmp', 'locate-remote-value-a'); - const locatorB = makeLocator('tmp', 'locate-remote-value-b'); - const hostA = await makeHostWithTestNetwork(locatorA, cancelled); - const hostB = await makeHostWithTestNetwork(locatorB, cancelled); + const { locator: locatorA, cancelled: cancelledA } = await prepareLocator(t); + const { locator: locatorB, cancelled: cancelledB } = await prepareLocator(t); + const hostA = await makeHostWithTestNetwork(locatorA, cancelledA); + const hostB = await makeHostWithTestNetwork(locatorB, cancelledB); // introduce nodes to each other await E(hostA).addPeerInfo(await E(hostB).getPeerInfo()); @@ -1542,7 +1135,4 @@ test('locate remote value', async t => { const greetingsLocator = await E(hostA).locate('greetings'); const parsedGreetingsLocator = parseLocator(greetingsLocator); t.is(parsedGreetingsLocator.formulaType, 'remote'); - - await stop(locatorA); - await stop(locatorB); });