-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: correctly truncate seenEvents (#282)
The logic to truncate a set was incorrect and could produce and almost inifinite loop as it was decreasing the counter rather than increasing it. We extracted the logic to its own module, where it can be tested.
- Loading branch information
Showing
4 changed files
with
35 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { truncateSet } from "./set"; | ||
|
||
describe("truncateSet", () => { | ||
test("original set below maxSize", () => { | ||
const s = new Set([1, 2, 3]); | ||
expect(truncateSet(s, 4)).toBe(s); | ||
}); | ||
|
||
test("resulting set keeps last inserted items", () => { | ||
const s = new Set([1, 2, 3]); | ||
s.add(4); | ||
expect(truncateSet(s, 3)).toEqual(new Set([2, 3, 4])); | ||
expect(truncateSet(s, 2)).toEqual(new Set([3, 4])); | ||
expect(truncateSet(s, 1)).toEqual(new Set([4])); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function truncateSet<T>(set: Set<T>, maxSize: number): Set<T> { | ||
if (set.size <= maxSize) { | ||
return set; | ||
} | ||
const iterator = set.values(); | ||
for (let i = 0; i < set.size - maxSize; ++i) { | ||
iterator.next(); | ||
} | ||
return new Set(iterator); | ||
} |