Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(daemon): expose teardown for resetting without restarting #2057

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions packages/daemon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,22 @@ export { makeEndoClient } from './src/client.js';
export { makeRefReader, makeRefIterator } from './src/ref-reader.js';
export { makeReaderRef, makeIteratorRef } from './src/reader-ref.js';

const removePathInternal = async removalPath => {
// workaround for windows wonkiness
if (os.platform() === 'win32') {
const { windows: windowsDelete } = await import('rimraf');
return windowsDelete(removalPath);
}
return fs.promises.rm(removalPath, { recursive: true, force: true });
};

const removePath = async removalPath => {
return fs.promises
.rm(removalPath, { recursive: true, force: true })
.catch(cause => {
/** @type {object} */
const error = new Error(cause.message, { cause });
error.code = cause.code;
throw error;
});
return removePathInternal(removalPath).catch(cause => {
/** @type {object} */
const error = new Error(cause.message, { cause });
error.code = cause.code;
throw error;
});
};

const { username, homedir } = os.userInfo();
Expand Down Expand Up @@ -147,9 +154,7 @@ const enoentOk = error => {
};

export const clean = async (locator = defaultLocator) => {
if (process.platform !== 'win32') {
await removePath(locator.sockPath).catch(enoentOk);
}
await removePath(locator.sockPath).catch(enoentOk);
};

export const restart = async (locator = defaultLocator) => {
Expand All @@ -163,7 +168,10 @@ export const stop = async (locator = defaultLocator) => {
await clean(locator);
};

export const reset = async (locator = defaultLocator) => {
export const resetInternal = async (
locator = defaultLocator,
turnOff = false,
) => {
// Attempt to restore to a running state if currently running, based on
// whether we manage to terminate it.
const needsRestart = await terminate(locator).then(
Expand All @@ -184,7 +192,12 @@ export const reset = async (locator = defaultLocator) => {
removedCache,
]);

if (needsRestart) {
if (!turnOff && needsRestart) {
await start(locator);
}
};

export const reset = async (locator = defaultLocator) =>
resetInternal(locator, false);
export const teardown = async (locator = defaultLocator) =>
resetInternal(locator, true);
1 change: 1 addition & 0 deletions packages/daemon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@endo/stream-node": "^1.0.2",
"@endo/where": "^1.0.1",
"ses": "^1.1.0",
"rimraf": "^5.0.5",
"ws": "^8.13.0"
},
"devDependencies": {
Expand Down
65 changes: 41 additions & 24 deletions packages/daemon/test/test-endo.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
reset,
makeEndoClient,
makeReaderRef,
teardown,
} from '../index.js';

const { raw } = String;
Expand Down Expand Up @@ -74,8 +75,9 @@ const doMakeBundle = async (host, filePath, callback) => {

test('lifecycle', async t => {
const { reject: cancel, promise: cancelled } = makePromiseKit();
t.teardown(() => cancel(Error('teardown')));
const locator = makeLocator('tmp', 'lifecycle');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await stop(locator).catch(() => {});
await reset(locator);
Expand All @@ -102,8 +104,9 @@ test('lifecycle', async t => {

test('spawn and evaluate', async t => {
const { promise: cancelled, reject: cancel } = makePromiseKit();
t.teardown(() => cancel(Error('teardown')));
const locator = makeLocator('tmp', 'spawn-eval');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await stop(locator).catch(() => {});
await reset(locator);
Expand All @@ -125,8 +128,9 @@ test('spawn and evaluate', async t => {

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');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await stop(locator).catch(() => {});
await reset(locator);
Expand All @@ -147,8 +151,9 @@ test('anonymous spawn and evaluate', async t => {

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');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await stop(locator).catch(() => {});
await reset(locator);
Expand Down Expand Up @@ -204,8 +209,9 @@ test('persist spawn and evaluation', async t => {

test('store', async t => {
const { promise: cancelled, reject: cancel } = makePromiseKit();
t.teardown(() => cancel(Error('teardown')));
const locator = makeLocator('tmp', 'store');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await stop(locator).catch(() => {});
await reset(locator);
Expand Down Expand Up @@ -241,8 +247,9 @@ test('store', async t => {

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');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await stop(locator).catch(() => {});
await reset(locator);
Expand Down Expand Up @@ -341,8 +348,9 @@ test('closure state lost by restart', async t => {

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');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await stop(locator).catch(() => {});
await reset(locator);
Expand Down Expand Up @@ -425,8 +433,9 @@ test('persist unconfined services and their requests', async t => {

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');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await stop(locator).catch(() => {});
await reset(locator);
Expand Down Expand Up @@ -511,9 +520,9 @@ test('persist confined services and their requests', async t => {

test('guest facet receives a message for host', async t => {
const { promise: cancelled, reject: cancel } = makePromiseKit();
t.teardown(() => cancel(Error('teardown')));

const locator = makeLocator('tmp', 'guest-sends-host');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await start(locator);

Expand Down Expand Up @@ -562,9 +571,9 @@ test('guest facet receives a message for host', async t => {

test('direct termination', async t => {
const { promise: cancelled, reject: cancel } = makePromiseKit();
t.teardown(() => cancel(Error('teardown')));

const locator = makeLocator('tmp', 'termination-direct');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await start(locator);
t.teardown(() => stop(locator));
Expand Down Expand Up @@ -642,9 +651,9 @@ test('direct termination', async t => {

test('indirect termination', async t => {
const { promise: cancelled, reject: cancel } = makePromiseKit();
t.teardown(() => cancel(Error('teardown')));

const locator = makeLocator('tmp', 'termination-indirect');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await start(locator);
t.teardown(() => stop(locator));
Expand Down Expand Up @@ -721,9 +730,9 @@ test('indirect termination', async t => {

test('terminate because of requested capability', async t => {
const { promise: cancelled, reject: cancel } = makePromiseKit();
t.teardown(() => cancel(Error('teardown')));

const locator = makeLocator('tmp', 'termination-via-request');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await start(locator);
t.teardown(() => stop(locator));
Expand Down Expand Up @@ -808,8 +817,9 @@ test('terminate because of requested capability', 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');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await stop(locator).catch(() => {});
await reset(locator);
Expand All @@ -832,8 +842,9 @@ test('make a host', async t => {

test('name and reuse inspector', async t => {
const { promise: cancelled, reject: cancel } = makePromiseKit();
t.teardown(() => cancel(Error('teardown')));
const locator = makeLocator('tmp', 'inspector-reuse');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await stop(locator).catch(() => {});
await reset(locator);
Expand Down Expand Up @@ -875,8 +886,9 @@ test('name and reuse inspector', async t => {
// This behavior is undesirable. See: https://github.com/endojs/endo/issues/2021
test('eval-mediated worker name', async t => {
const { promise: cancelled, reject: cancel } = makePromiseKit();
t.teardown(() => cancel(Error('teardown')));
const locator = makeLocator('tmp', 'eval-worker-name');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await stop(locator).catch(() => {});
await reset(locator);
Expand Down Expand Up @@ -922,8 +934,9 @@ test('eval-mediated worker name', async t => {

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');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await stop(locator).catch(() => {});
await reset(locator);
Expand Down Expand Up @@ -953,8 +966,9 @@ test('lookup with single petname', async t => {

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');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await stop(locator).catch(() => {});
await reset(locator);
Expand Down Expand Up @@ -982,8 +996,9 @@ test('lookup with petname path (inspector)', async t => {

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');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await stop(locator).catch(() => {});
await reset(locator);
Expand Down Expand Up @@ -1013,8 +1028,9 @@ test('lookup with petname path (caplet with lookup method)', async t => {

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');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await stop(locator).catch(() => {});
await reset(locator);
Expand Down Expand Up @@ -1044,8 +1060,9 @@ test('lookup with petname path (value has no lookup method)', async t => {

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');
t.teardown(() => cancel(Error('teardown')));
t.teardown(() => teardown(locator));

await stop(locator).catch(() => {});
await reset(locator);
Expand Down
Loading
Loading