Skip to content

Commit

Permalink
refactor!(daemon): Remove host makeWorker() (merge #2123)
Browse files Browse the repository at this point in the history
Progresses: #2086 

Removes the host's `makeWorker()` and replaces its use everywhere with the equivalent `provideWorker()`. The latter is essentially like the former but with more validation.
  • Loading branch information
rekmarks authored Mar 8, 2024
2 parents c87ae7a + 9fe0d6c commit eb900a7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/commands/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import { withEndoParty } from '../context.js';

export const spawn = async ({ petNames, partyNames }) =>
withEndoParty(partyNames, { os, process }, async ({ party }) =>
Promise.all(petNames.map(petName => E(party).makeWorker(petName))),
Promise.all(petNames.map(petName => E(party).provideWorker(petName))),
);
18 changes: 2 additions & 16 deletions packages/daemon/src/host.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { makeIteratorRef } from './reader-ref.js';
import { assertPetName, petNamePathFrom } from './pet-name.js';
import { makePetSitter } from './pet-sitter.js';
import { makeDeferredTasks } from './deferred-tasks.js';
import { parseFormulaIdentifier } from './formula-identifier.js';

const { quote: q } = assert;

Expand Down Expand Up @@ -130,7 +131,7 @@ export const makeHostMaker = ({
if (petName !== undefined) {
const formulaIdentifier = petStore.identifyLocal(petName);
if (formulaIdentifier !== undefined) {
if (formulaIdentifier.startsWith('guest:')) {
if (parseFormulaIdentifier(formulaIdentifier).type !== 'guest') {
throw new Error(
`Existing pet name does not designate a guest powers capability: ${q(
petName,
Expand Down Expand Up @@ -416,20 +417,6 @@ export const makeHostMaker = ({
return value;
};

/**
* @param {string} [petName]
* @returns {Promise<import('./types.js').EndoWorker>}
*/
const makeWorker = async petName => {
// Behold, recursion:
const { formulaIdentifier, value } = await incarnateWorker();
if (petName !== undefined) {
assertPetName(petName);
await petStore.write(petName, formulaIdentifier);
}
return /** @type {import('./types.js').EndoWorker} */ (value);
};

/**
* @param {string} [petName]
* @param {import('./types.js').MakeHostOrGuestOptions} [opts]
Expand Down Expand Up @@ -599,7 +586,6 @@ export const makeHostMaker = ({
store,
provideGuest,
provideHost,
makeWorker,
provideWorker,
evaluate,
makeUnconfined,
Expand Down
1 change: 0 additions & 1 deletion packages/daemon/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,6 @@ export interface EndoHost extends EndoDirectory {
opts?: MakeHostOrGuestOptions,
): Promise<EndoHost>;
makeDirectory(petName: string): Promise<EndoDirectory>;
makeWorker(petName: string): Promise<EndoWorker>;
provideWorker(petName: string): Promise<EndoWorker>;
evaluate(
workerPetName: string | undefined,
Expand Down
47 changes: 36 additions & 11 deletions packages/daemon/test/test-endo.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ test('lifecycle', async t => {
);
const bootstrap = getBootstrap();
const host = E(bootstrap).host();
await E(host).makeWorker('worker');
await E(host).provideWorker('worker');
await E(host).cancel('worker');
cancel(new Error('Cancelled'));
await closed.catch(() => {});
Expand All @@ -121,7 +121,7 @@ test('spawn and evaluate', async t => {
);
const bootstrap = getBootstrap();
const host = E(bootstrap).host();
await E(host).makeWorker('w1');
await E(host).provideWorker('w1');
const ten = await E(host).evaluate('w1', '10', [], []);
t.is(ten, 10);

Expand Down Expand Up @@ -150,6 +150,31 @@ test('anonymous spawn and evaluate', async t => {
await stop(locator);
});

test('cannot spawn worker with 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 { 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);
await t.throwsAsync(() => E(host).provideWorker('ten'), {
message: 'Not a worker "ten"',
});

await stop(locator);
});

test('persist spawn and evaluation', async t => {
const { promise: cancelled, reject: cancel } = makePromiseKit();
t.teardown(() => cancel(Error('teardown')));
Expand All @@ -168,7 +193,7 @@ test('persist spawn and evaluation', async t => {
const bootstrap = getBootstrap();
const host = E(bootstrap).host();

await E(host).makeWorker('w1');
await E(host).provideWorker('w1');

const ten = await E(host).evaluate('w1', '10', [], [], 'ten');
t.is(ten, 10);
Expand Down Expand Up @@ -261,7 +286,7 @@ test('closure state lost by restart', async t => {
);
const bootstrap = getBootstrap();
const host = E(bootstrap).host();
await E(host).makeWorker('w1');
await E(host).provideWorker('w1');

await E(host).evaluate(
'w1',
Expand Down Expand Up @@ -364,7 +389,7 @@ test('persist unconfined services and their requests', async t => {
);
const bootstrap = getBootstrap();
const host = E(bootstrap).host();
await E(host).makeWorker('user-worker');
await E(host).provideWorker('user-worker');
await E(host).evaluate(
'user-worker',
`
Expand All @@ -391,13 +416,13 @@ test('persist unconfined services and their requests', async t => {
);
const bootstrap = getBootstrap();
const host = E(bootstrap).host();
await E(host).makeWorker('w1');
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');

await E(host).makeWorker('w2');
await E(host).provideWorker('w2');
const answer = await E(host).evaluate(
'w2',
'E(service).ask()',
Expand Down Expand Up @@ -449,7 +474,7 @@ test('persist confined services and their requests', async t => {
);
const bootstrap = getBootstrap();
const host = E(bootstrap).host();
await E(host).makeWorker('user-worker');
await E(host).provideWorker('user-worker');
await E(host).evaluate(
'user-worker',
`
Expand All @@ -476,14 +501,14 @@ test('persist confined services and their requests', async t => {
);
const bootstrap = getBootstrap();
const host = E(bootstrap).host();
await E(host).makeWorker('w1');
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'),
);

await E(host).makeWorker('w2');
await E(host).provideWorker('w2');
const answer = await E(host).evaluate(
'w2',
'E(service).ask()',
Expand Down Expand Up @@ -906,7 +931,7 @@ test('make a host', async t => {
const bootstrap = getBootstrap();
const host = E(bootstrap).host();
const host2 = E(host).provideHost('fellow-host');
await E(host2).makeWorker('w1');
await E(host2).provideWorker('w1');
const ten = await E(host2).evaluate('w1', '10', [], []);
t.is(ten, 10);

Expand Down

0 comments on commit eb900a7

Please sign in to comment.