-
-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #490 from hgrsd/fix-cache-false-positive
fix(cache): cache key comparison ignores milliseconds
- Loading branch information
Showing
2 changed files
with
119 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { Cache } from "../src/cache"; | ||
import { expect } from "chai"; | ||
import { IterArgs } from "../src/iterresult"; | ||
|
||
const dates = [ | ||
new Date("2021-01-01T00:00:00.000+00:00"), | ||
new Date("2021-01-02T00:00:00.000+00:00"), | ||
new Date("2021-01-03T00:00:00.000+00:00"), | ||
new Date("2021-01-04T00:00:00.000+00:00"), | ||
new Date("2021-01-05T00:00:00.000+00:00"), | ||
new Date("2021-01-06T00:00:00.000+00:00"), | ||
new Date("2021-01-07T00:00:00.000+00:00"), | ||
]; | ||
|
||
describe("Cache", () => { | ||
it("returns false for an empty cache", () => { | ||
const cache = new Cache(); | ||
const args: Partial<IterArgs> = { | ||
after: new Date("2021-01-01T00:00:00.000+00:00"), | ||
before: new Date("2021-01-08T00:00:00.000+00:00"), | ||
inc: true, | ||
}; | ||
|
||
expect(cache._cacheGet("between", args)).to.be.false; | ||
}); | ||
|
||
it("returns an empty array for a cached but empty set", () => { | ||
const cache = new Cache(); | ||
const args: Partial<IterArgs> = { | ||
after: new Date("2021-01-01T00:00:00.000+00:00"), | ||
before: new Date("2021-01-08T00:00:00.000+00:00"), | ||
inc: true, | ||
}; | ||
|
||
cache._cacheAdd("between", [], args); | ||
|
||
expect(cache._cacheGet("between", args)).to.eql([]); | ||
}); | ||
|
||
it('returns cached entries if the "what" and the args both match', () => { | ||
const cache = new Cache(); | ||
const args: Partial<IterArgs> = { | ||
after: new Date("2021-01-01T00:00:00.000+00:00"), | ||
before: new Date("2021-01-08T00:00:00.000+00:00"), | ||
inc: true, | ||
}; | ||
|
||
cache._cacheAdd("between", dates, args); | ||
|
||
expect(cache._cacheGet("between", args)).to.eql(dates); | ||
}); | ||
|
||
it('does not return cached entries if the "what" matches but the args do not', () => { | ||
const cache = new Cache(); | ||
const args: Partial<IterArgs> = { | ||
after: new Date("2021-01-01T00:00:00.000+00:00"), | ||
before: new Date("2021-01-08T00:00:00.000+00:00"), | ||
inc: true, | ||
}; | ||
|
||
cache._cacheAdd("between", dates, args); | ||
|
||
expect( | ||
cache._cacheGet("between", { | ||
...args, | ||
/** 1ms later than the args used for the insert */ | ||
after: new Date("2021-01-01T00:00:00.001+00:00"), | ||
}) | ||
).to.equal(false); | ||
}); | ||
|
||
it('does not return cached entries if args match but the "what" does not', () => { | ||
const cache = new Cache(); | ||
const args: Partial<IterArgs> = { | ||
after: new Date("2021-01-01T00:00:00.000+00:00"), | ||
before: new Date("2021-01-08T00:00:00.000+00:00"), | ||
inc: true, | ||
}; | ||
|
||
cache._cacheAdd("between", dates, args); | ||
|
||
expect(cache._cacheGet("after", args)).to.equal(false); | ||
}); | ||
|
||
it('reuses dates cached for the "all" method when querying using another method', () => { | ||
const cache = new Cache(); | ||
const args: Partial<IterArgs> = { | ||
after: new Date("2021-01-04T00:00:00.000+00:00"), | ||
before: new Date("2021-01-06T00:00:00.000+00:00"), | ||
inc: true, | ||
}; | ||
|
||
cache._cacheAdd("all", dates); | ||
|
||
expect(cache._cacheGet("between", args)).to.eql([ | ||
new Date("2021-01-04T00:00:00.000+00:00"), | ||
new Date("2021-01-05T00:00:00.000+00:00"), | ||
new Date("2021-01-06T00:00:00.000+00:00"), | ||
]); | ||
}); | ||
}); |