Skip to content

Commit

Permalink
fix(topsort): circular reference detection
Browse files Browse the repository at this point in the history
adds a fix for circular reference detection to correctly restore the parent list before going deeper.
  • Loading branch information
marcj committed Jan 29, 2024
1 parent 89cd2d5 commit 1c01cc1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/topsort/src/group-array-sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class GroupArraySort<T = string, TYPE = string> extends BaseImplementatio
let minLevel = -1;

if (element.dependencies.length) {
parents = parents || new Set<T>();
parents = parents ? new Set(parents) : new Set();
parents.add(element.item);

for (const dependency of element.dependencies) {
Expand Down
14 changes: 13 additions & 1 deletion packages/topsort/tests/group-sort.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('bench', () => {
const count = 10_000;
const items = getElementsGroup(count);

bench(10, `ArraySort Warmup ${count}`, () => {
bench(10, `GroupArraySort Warmup ${count}`, () => {
const sorter = new GroupArraySort();
sorter.set(items);
sorter.sort();
Expand Down Expand Up @@ -61,6 +61,18 @@ test('circular exception', () => {
}
});

test('parent circular', () => {
const sorter = new GroupArraySort();

sorter.add('service1', 'service', ['account1']);
sorter.add('account1', 'account', []);
sorter.add('deploy1', 'deploy', ['node1', 'node1', 'service1']);
sorter.add('node1', 'node', ['account1']);

// this must not fail
sorter.sort();
});

test('dependency in same', () => {
const sorter = new GroupArraySort();
sorter.add('car1', 'car', ['brand1']);
Expand Down

0 comments on commit 1c01cc1

Please sign in to comment.