-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
208 additions
and
33 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
182 changes: 182 additions & 0 deletions
182
src/v2/Artsy/Relay/middleware/cache/__tests__/Cache.jest.ts
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,182 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
import { Cache } from "../Cache" | ||
|
||
jest.mock("lib/environment") | ||
|
||
describe("Cache", () => { | ||
const getCache = (props = {}) => { | ||
return new Cache({ | ||
size: 10, | ||
ttl: 900000, | ||
...props, | ||
}) | ||
} | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
describe("enableServerSideCaching", () => { | ||
it("disables if disableServerSideCache=false", () => { | ||
const cache = getCache({ disableServerSideCache: true }) | ||
expect(cache.enableServerSideCache).toBe(false) | ||
}) | ||
|
||
it("enables if disableServerSideCache=true", () => { | ||
const cache = getCache({ disableServerSideCache: false }) | ||
expect(cache.enableServerSideCache).toBe(true) | ||
}) | ||
}) | ||
|
||
it("initializes a relay cache", () => { | ||
const spy = jest.spyOn(Cache.prototype, "initRelayCache") | ||
const cache = getCache() | ||
expect(spy).toHaveBeenCalled() | ||
expect(cache.relayCache).toBeTruthy() | ||
}) | ||
|
||
it("initializes a redis cache", () => { | ||
const spy = jest.spyOn(Cache.prototype, "initRedisCache") | ||
const cache = getCache() | ||
expect(spy).toHaveBeenCalled() | ||
expect(cache.redisCache).toBeTruthy() | ||
}) | ||
|
||
describe("redis errors", () => { | ||
it("throws error if ECONNREFUSED", () => { | ||
const cache = getCache() | ||
const err = cache.handleRedisError({ | ||
error: { | ||
code: "ECONNREFUSED", | ||
}, | ||
}) as Error | ||
expect(err.message).toContain("The server refused the connection") | ||
}) | ||
|
||
it("throws error if retrieval time exceeds PAGE_CACHE_RETRIEVAL_TIME", () => { | ||
process.env.PAGE_CACHE_RETRIEVAL_TIMEOUT_MS = "10" | ||
const cache = getCache() | ||
const err = cache.handleRedisError({ | ||
total_retry_time: 100000, | ||
}) as Error | ||
expect(err.message).toContain("Retry time exhausted") | ||
}) | ||
|
||
it("throws error if number of attempts exceeds MAX_RETRIES", () => { | ||
const cache = getCache() | ||
const err = cache.handleRedisError({ | ||
attempt: 11, | ||
}) as Error | ||
expect(err.message).toContain("Retry attempts exceeded") | ||
}) | ||
|
||
it("returns a retry time that backs off", () => { | ||
const cache = getCache() | ||
;[...new Array(10)].forEach(index => { | ||
const retryTime = cache.handleRedisError({ | ||
attempt: index, | ||
}) as Error | ||
expect(retryTime).toEqual(index * 100) | ||
}) | ||
}) | ||
}) | ||
|
||
it("returns a cache key", () => { | ||
const cache = getCache() | ||
expect(cache.getCacheKey("ArtistQuery", { slug: "picasso" })).toEqual( | ||
JSON.stringify({ | ||
queryId: "ArtistQuery", | ||
variables: { | ||
slug: "picasso", | ||
}, | ||
}) | ||
) | ||
}) | ||
|
||
describe("setting and getting cache", () => { | ||
describe("client", () => { | ||
it("sets / gets the cache by cacheKey", async () => { | ||
const cache = getCache() | ||
cache.enableServerSideCache = false | ||
const queryId = "ArtistQuery" | ||
const variables = { slug: "picasso" } | ||
const response = "found response" | ||
const options = { cacheConfig: { force: false } } | ||
cache.set(queryId, variables, [response], options) | ||
expect(await cache.get(queryId, variables)).toEqual( | ||
expect.objectContaining({ 0: response }) | ||
) | ||
}) | ||
}) | ||
|
||
describe("server", () => { | ||
it("does not set cache if enableServerSideCaching=false", async () => { | ||
const cache = getCache() | ||
cache.enableServerSideCache = false | ||
cache.redisCache.get = jest.fn() | ||
const queryId = "ArtistQuery" | ||
const variables = { slug: "picasso" } | ||
const response = "found response" | ||
const options = { cacheConfig: { force: false } } | ||
cache.set(queryId, variables, [response], options) | ||
await cache.get(queryId, variables) | ||
expect(cache.redisCache.get).not.toHaveBeenCalled() | ||
}) | ||
|
||
it("does not set cache if cacheConfig.force=true", async () => { | ||
const cache = getCache() | ||
cache.enableServerSideCache = true | ||
cache.redisCache.get = jest.fn() | ||
const queryId = "ArtistQuery" | ||
const variables = { slug: "picasso" } | ||
const response = "found response" | ||
const options = { cacheConfig: { force: true } } | ||
cache.set(queryId, variables, [response], options) | ||
await cache.get(queryId, variables) | ||
expect(cache.redisCache.get).not.toHaveBeenCalled() | ||
}) | ||
|
||
it("gets / sets the cache by cacheKey", async () => { | ||
const cache = getCache() | ||
cache.enableServerSideCache = true | ||
cache.relayCache.get = jest.fn() | ||
const queryId = "ArtistQuery" | ||
const variables = { slug: "picasso" } | ||
const response = { foo: "bar" } | ||
const options = { cacheConfig: { force: false } } | ||
|
||
const expireSpy = jest.fn(() => Promise.resolve()) | ||
cache.redisCache = { | ||
get: () => Promise.resolve(JSON.stringify(response)), | ||
set: () => Promise.resolve(), | ||
expire: expireSpy, | ||
} as any | ||
|
||
cache.set(queryId, variables, [response], options) | ||
const res = await cache.get(queryId, variables) | ||
expect(res).toEqual(response) | ||
expect(expireSpy).toHaveBeenCalledWith( | ||
cache.getCacheKey(queryId, variables), | ||
cache.cacheConfig.ttl | ||
) | ||
}) | ||
}) | ||
}) | ||
|
||
describe("cache.clear", () => { | ||
it("clears all caches", () => { | ||
const cache = getCache() | ||
cache.enableServerSideCache = true | ||
const relaySpy = jest.fn() | ||
cache.relayCache.clear = relaySpy | ||
const redisSpy = jest.fn() | ||
cache.redisCache.flushall = redisSpy | ||
cache.clear() | ||
expect(relaySpy).toHaveBeenCalled() | ||
expect(redisSpy).toHaveBeenCalled() | ||
}) | ||
}) | ||
}) |