mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Add tests to verify runMany behaviour in new possible incorrect configuration
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
1a8040e88f
commit
29a35ab71c
@ -223,7 +223,68 @@ describe("runManyFor", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return expect(() => runMany()).rejects.toThrow(
|
return expect(() => runMany()).rejects.toThrow(
|
||||||
/Unreachable runnable="some-runnable-1". The specified runAfters; all of "some-runnable-2"; are not part of this injection token/,
|
/Runnable "some-runnable-1" is unreachable for injection token "some-injection-token": run afters "some-runnable-2" are a part of different injection tokens./,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("given partially incorrect hierarchy, when running runnables, throws", () => {
|
||||||
|
const rootDi = createContainer("irrelevant");
|
||||||
|
|
||||||
|
const runMock = asyncFn<(...args: unknown[]) => void>();
|
||||||
|
|
||||||
|
const someInjectionToken = getInjectionToken<Runnable>({
|
||||||
|
id: "some-injection-token",
|
||||||
|
});
|
||||||
|
|
||||||
|
const someOtherInjectionToken = getInjectionToken<Runnable>({
|
||||||
|
id: "some-other-injection-token",
|
||||||
|
});
|
||||||
|
|
||||||
|
const someInjectable = getInjectable({
|
||||||
|
id: "some-runnable-1",
|
||||||
|
|
||||||
|
instantiate: (di) => ({
|
||||||
|
id: "some-runnable-1",
|
||||||
|
run: () => runMock("some-runnable-1"),
|
||||||
|
runAfter: [
|
||||||
|
di.inject(someOtherInjectable),
|
||||||
|
di.inject(someSecondInjectable),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
|
||||||
|
injectionToken: someInjectionToken,
|
||||||
|
});
|
||||||
|
|
||||||
|
const someSecondInjectable = getInjectable({
|
||||||
|
id: "some-runnable-2",
|
||||||
|
|
||||||
|
instantiate: () => ({
|
||||||
|
id: "some-runnable-2",
|
||||||
|
run: () => runMock("some-runnable-2"),
|
||||||
|
}),
|
||||||
|
|
||||||
|
injectionToken: someInjectionToken,
|
||||||
|
});
|
||||||
|
|
||||||
|
const someOtherInjectable = getInjectable({
|
||||||
|
id: "some-runnable-3",
|
||||||
|
|
||||||
|
instantiate: () => ({
|
||||||
|
id: "some-runnable-3",
|
||||||
|
run: () => runMock("some-runnable-3"),
|
||||||
|
}),
|
||||||
|
|
||||||
|
injectionToken: someOtherInjectionToken,
|
||||||
|
});
|
||||||
|
|
||||||
|
rootDi.register(someInjectable, someOtherInjectable, someSecondInjectable);
|
||||||
|
|
||||||
|
const runMany = runManyFor(rootDi)(
|
||||||
|
someInjectionToken,
|
||||||
|
);
|
||||||
|
|
||||||
|
return expect(() => runMany()).rejects.toThrow(
|
||||||
|
/Runnable "some-runnable-3" is not part of the injection token "some-injection-token"/,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -34,7 +34,7 @@ const computedNextEdge = (traversed: string[], graph: Map<string, Set<string>>,
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const verifyRunnablesAreDAG = <Param>(runnables: Runnable<Param>[]) => {
|
const verifyRunnablesAreDAG = <Param>(injectionToken: InjectionToken<Runnable<Param>, void>, runnables: Runnable<Param>[]) => {
|
||||||
const rootId = uuid.v4();
|
const rootId = uuid.v4();
|
||||||
const runnableGraph = new Map<string, Set<string>>();
|
const runnableGraph = new Map<string, Set<string>>();
|
||||||
const seenIds = new Set<string>();
|
const seenIds = new Set<string>();
|
||||||
@ -63,7 +63,9 @@ const verifyRunnablesAreDAG = <Param>(runnables: Runnable<Param>[]) => {
|
|||||||
if (!seenIds.has(id)) {
|
if (!seenIds.has(id)) {
|
||||||
const runnable = runnables.find(runnable => runnable.id === id);
|
const runnable = runnables.find(runnable => runnable.id === id);
|
||||||
|
|
||||||
assert(runnable, `Unknown runnable id="${id}", logic error`);
|
if (!runnable) {
|
||||||
|
throw new Error(`Runnable "${id}" is not part of the injection token "${injectionToken.id}"`);
|
||||||
|
}
|
||||||
|
|
||||||
const runAfters = [runnable.runAfter]
|
const runAfters = [runnable.runAfter]
|
||||||
.flat()
|
.flat()
|
||||||
@ -71,7 +73,7 @@ const verifyRunnablesAreDAG = <Param>(runnables: Runnable<Param>[]) => {
|
|||||||
.map(runnable => runnable.id)
|
.map(runnable => runnable.id)
|
||||||
.join('", "');
|
.join('", "');
|
||||||
|
|
||||||
throw new Error(`Unreachable runnable="${id}". The specified runAfters; all of "${runAfters}"; are not part of this injection token`);
|
throw new Error(`Runnable "${id}" is unreachable for injection token "${injectionToken.id}": run afters "${runAfters}" are a part of different injection tokens.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -96,7 +98,7 @@ export function runManyFor(di: DiContainerForInjection): RunMany {
|
|||||||
const executeRunnable = executeRunnableWith(param);
|
const executeRunnable = executeRunnableWith(param);
|
||||||
const allRunnables = di.injectMany(injectionToken);
|
const allRunnables = di.injectMany(injectionToken);
|
||||||
|
|
||||||
verifyRunnablesAreDAG(allRunnables);
|
verifyRunnablesAreDAG(injectionToken, allRunnables);
|
||||||
|
|
||||||
await Promise.all(allRunnables.map(executeRunnable));
|
await Promise.all(allRunnables.map(executeRunnable));
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user