1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix runManyFor tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-12-01 10:18:31 -05:00
parent 9dbc6bc293
commit bbf6ec38bf
2 changed files with 24 additions and 4 deletions

View File

@ -223,7 +223,7 @@ describe("runManyFor", () => {
); );
return expect(() => runMany()).rejects.toThrow( return expect(() => runMany()).rejects.toThrow(
/Tried to get a composite but encountered missing parent ids: "some-runnable-2".\n\nAvailable parent ids are:\n"[0-9a-z-]+",\n"some-runnable-1"/, /Unreachable runnable="some-runnable-1". The specified runAfters; all of "some-runnable-2"; are not part of this injection token/,
); );
}); });

View File

@ -19,7 +19,8 @@ type Run<Param> = (parameter: Param) => Promise<void> | void;
export type RunMany = <Param>(injectionToken: InjectionToken<Runnable<Param>, void>) => Run<Param>; export type RunMany = <Param>(injectionToken: InjectionToken<Runnable<Param>, void>) => Run<Param>;
const computedNextEdge = (traversed: string[], graph: Map<string, Set<string>>, currentId: string) => { const computedNextEdge = (traversed: string[], graph: Map<string, Set<string>>, currentId: string, seenIds: Set<string>) => {
seenIds.add(currentId);
const currentNode = graph.get(currentId); const currentNode = graph.get(currentId);
assert(currentNode, `Runnable graph does not contain node with id="${currentId}"`); assert(currentNode, `Runnable graph does not contain node with id="${currentId}"`);
@ -29,13 +30,14 @@ const computedNextEdge = (traversed: string[], graph: Map<string, Set<string>>,
throw new Error(`Cycle in runnable graph: "${traversed.join(`" -> "`)}" -> "${nextId}"`); throw new Error(`Cycle in runnable graph: "${traversed.join(`" -> "`)}" -> "${nextId}"`);
} }
computedNextEdge([...traversed, nextId], graph, nextId); computedNextEdge([...traversed, nextId], graph, nextId, seenIds);
} }
}; };
const verifyRunnablesAreDAG = <Param>(runnables: Runnable<Param>[]) => { const verifyRunnablesAreDAG = <Param>(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>();
// Build the Directed graph // Build the Directed graph
for (const runnable of runnables) { for (const runnable of runnables) {
@ -52,8 +54,26 @@ const verifyRunnablesAreDAG = <Param>(runnables: Runnable<Param>[]) => {
} }
} }
getOrInsertSet(runnableGraph, rootId);
// Do a DFS to find any cycles // Do a DFS to find any cycles
computedNextEdge([], runnableGraph, rootId); computedNextEdge([], runnableGraph, rootId, seenIds);
for (const id of runnableGraph.keys()) {
if (!seenIds.has(id)) {
const runnable = runnables.find(runnable => runnable.id === id);
assert(runnable, `Unknown runnable id="${id}", logic error`);
const runAfters = [runnable.runAfter]
.flat()
.filter(isDefined)
.map(runnable => runnable.id)
.join('", "');
throw new Error(`Unreachable runnable="${id}". The specified runAfters; all of "${runAfters}"; are not part of this injection token`);
}
}
}; };
const executeRunnableWith = <Param>(param: Param) => { const executeRunnableWith = <Param>(param: Param) => {