Skip to content

Commit ce386ff

Browse files
committed
fix(endpoint-cache): edge-case for has when value is empty
1 parent 2733bfb commit ce386ff

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

packages/endpoint-cache/src/EndpointCache.spec.ts

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe(EndpointCache.name, () => {
1313
const now = Date.now();
1414
const set = jest.fn();
1515
const get = jest.fn();
16+
const peek = jest.fn();
1617
const has = jest.fn();
1718
const clear = jest.fn();
1819

@@ -34,6 +35,7 @@ describe(EndpointCache.name, () => {
3435
((LRUCache as unknown) as jest.Mock).mockReturnValueOnce({
3536
set,
3637
get,
38+
peek,
3739
has,
3840
clear,
3941
});
@@ -52,7 +54,9 @@ describe(EndpointCache.name, () => {
5254
describe("get", () => {
5355
beforeEach(() => {
5456
has.mockReturnValue(true);
55-
get.mockReturnValue(getEndpointsWithExpiry(mockEndpoints));
57+
const endpointsWithExpiry = getEndpointsWithExpiry(mockEndpoints);
58+
peek.mockReturnValue(endpointsWithExpiry);
59+
get.mockReturnValue(endpointsWithExpiry);
5660
jest.spyOn(Date, "now").mockImplementation(() => now);
5761
});
5862

@@ -68,6 +72,18 @@ describe(EndpointCache.name, () => {
6872
expect(endpointCache.get(key)).toBeUndefined();
6973
expect(has).toHaveBeenCalledTimes(1);
7074
expect(has).toHaveBeenCalledWith(key);
75+
expect(peek).not.toHaveBeenCalled();
76+
expect(get).not.toHaveBeenCalled();
77+
});
78+
79+
it("returns undefined if cache has empty array", () => {
80+
has.mockReturnValueOnce(true);
81+
peek.mockReturnValueOnce([]);
82+
expect(endpointCache.get(key)).toBeUndefined();
83+
expect(has).toHaveBeenCalledTimes(1);
84+
expect(has).toHaveBeenCalledWith(key);
85+
expect(peek).toHaveBeenCalledTimes(1);
86+
expect(peek).toHaveBeenCalledWith(key);
7187
expect(get).not.toHaveBeenCalled();
7288
});
7389

@@ -148,10 +164,39 @@ describe(EndpointCache.name, () => {
148164
expect(set).toHaveBeenCalledWith(key, []);
149165
});
150166

151-
it("has", () => {
152-
endpointCache.has(key);
153-
expect(has).toHaveBeenCalledTimes(1);
154-
expect(has).toHaveBeenCalledWith(key);
167+
describe("has", () => {
168+
describe("returns false", () => {
169+
it("when key is not present", () => {
170+
has.mockReturnValueOnce(false);
171+
expect(endpointCache.has(key)).toEqual(false);
172+
expect(has).toHaveBeenCalledTimes(1);
173+
expect(has).toHaveBeenCalledWith(key);
174+
});
175+
176+
it("when key is present and value is empty", () => {
177+
has.mockReturnValueOnce(true);
178+
peek.mockReturnValueOnce([]);
179+
expect(endpointCache.has(key)).toEqual(false);
180+
expect(has).toHaveBeenCalledTimes(1);
181+
expect(has).toHaveBeenCalledWith(key);
182+
});
183+
184+
it("when key is present and value is undefined", () => {
185+
has.mockReturnValueOnce(true);
186+
peek.mockReturnValueOnce(undefined);
187+
expect(endpointCache.has(key)).toEqual(false);
188+
expect(has).toHaveBeenCalledTimes(1);
189+
expect(has).toHaveBeenCalledWith(key);
190+
});
191+
});
192+
193+
it("returns true when key is present and value is non-empty", () => {
194+
has.mockReturnValueOnce(true);
195+
peek.mockReturnValueOnce(getEndpointsWithExpiry(mockEndpoints));
196+
expect(endpointCache.has(key)).toEqual(true);
197+
expect(has).toHaveBeenCalledTimes(1);
198+
expect(has).toHaveBeenCalledWith(key);
199+
});
155200
});
156201

157202
it("clear", () => {

packages/endpoint-cache/src/EndpointCache.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,17 @@ export class EndpointCache {
8383
* @returns {boolean}
8484
*/
8585
has(key: string): boolean {
86-
return this.cache.has(key);
86+
if (!this.cache.has(key)) {
87+
return false;
88+
}
89+
90+
// Remove call for peek, once remove/delete support is added upstream
91+
// Refs: https://github.com/Yomguithereal/mnemonist/issues/143
92+
const endpoints = this.cache.peek(key);
93+
if (!endpoints) {
94+
return false;
95+
}
96+
return endpoints.length > 0;
8797
}
8898

8999
/**

0 commit comments

Comments
 (0)