0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor(core): allow non-normative redis url

This commit is contained in:
Gao Sun 2024-04-18 18:31:15 +08:00
parent aa9b7aea5c
commit c4c22a50fb
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
2 changed files with 22 additions and 7 deletions

View file

@ -1,4 +1,7 @@
import { createMockUtils } from '@logto/shared/esm'; import { createMockUtils } from '@logto/shared/esm';
import Sinon from 'sinon';
import { EnvSet } from '#src/env-set/index.js';
const { jest } = import.meta; const { jest } = import.meta;
const { mockEsm } = createMockUtils(jest); const { mockEsm } = createMockUtils(jest);
@ -26,7 +29,7 @@ mockEsm('redis', () => ({
}), }),
})); }));
const { RedisCache, RedisClusterCache } = await import('./index.js'); const { RedisCache, RedisClusterCache, redisCacheFactory } = await import('./index.js');
describe('RedisCache', () => { describe('RedisCache', () => {
it('should successfully construct with no REDIS_URL', async () => { it('should successfully construct with no REDIS_URL', async () => {
@ -46,8 +49,13 @@ describe('RedisCache', () => {
it('should successfully construct with a Redis client', async () => { it('should successfully construct with a Redis client', async () => {
for (const url of ['1', 'redis://url']) { for (const url of ['1', 'redis://url']) {
jest.clearAllMocks(); jest.clearAllMocks();
const cache = new RedisCache(url); const stub = Sinon.stub(EnvSet, 'values').value({
...EnvSet.values,
redisUrl: url,
});
const cache = redisCacheFactory();
expect(cache instanceof RedisCache).toBeTruthy();
expect(cache.client).toBeTruthy(); expect(cache.client).toBeTruthy();
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
@ -61,14 +69,20 @@ describe('RedisCache', () => {
// Do sanity check only // Do sanity check only
expect(mockFunction).toBeCalledTimes(6); expect(mockFunction).toBeCalledTimes(6);
stub.restore();
} }
}); });
it('should successfully construct with a Redis Cluster client', async () => { it('should successfully construct with a Redis Cluster client', async () => {
for (const url of ['redis://url', 'redis:?host=h1&host=h2&host=h3']) { for (const url of ['redis://url?cluster=1', 'redis:?host=h1&host=h2&host=h3&cluster=true']) {
jest.clearAllMocks(); jest.clearAllMocks();
const cache = new RedisClusterCache(new URL(url)); const stub = Sinon.stub(EnvSet, 'values').value({
...EnvSet.values,
redisUrl: url,
});
const cache = redisCacheFactory();
expect(cache instanceof RedisClusterCache).toBeTruthy();
expect(cache.client).toBeTruthy(); expect(cache.client).toBeTruthy();
// eslint-disable-next-line no-await-in-loop // eslint-disable-next-line no-await-in-loop
@ -82,6 +96,7 @@ describe('RedisCache', () => {
// Do sanity check only // Do sanity check only
expect(mockFunction).toBeCalledTimes(6); expect(mockFunction).toBeCalledTimes(6);
stub.restore();
} }
}); });
}); });

View file

@ -135,12 +135,12 @@ export class RedisClusterCache extends RedisCacheBase {
} }
} }
const redisCacheFactory = (): RedisCacheBase => { export const redisCacheFactory = (): RedisCacheBase => {
const { redisUrl } = EnvSet.values; const { redisUrl } = EnvSet.values;
if (redisUrl) { if (redisUrl) {
const url = new URL(redisUrl); const url = trySafe(() => new URL(redisUrl));
if (yes(url.searchParams.get('redis_cluster'))) { if (url && yes(url.searchParams.get('cluster'))) {
return new RedisClusterCache(url); return new RedisClusterCache(url);
} }
} }