import { jest } from '@jest/globals' import Redis from '../../src/Cache/Redis.js' let _connectionId = 1 function createRedisConnection() { const connection = { connectionId: _connectionId++, isOpen: false, close: jest.fn(() => { connection.isOpen = false }), connect: jest.fn(async () => { connection.isOpen = true return { mget: jest.fn(() => { return true }), } }), } return connection as any } async function simpleRedisTest(config: any, concurrent?: boolean) { const RedisMock = jest.fn(() => createRedisConnection()) Redis['ClusterFactory'] = RedisMock // Double call is intentional const [provider, provider2] = concurrent ? await Promise.all([Redis.connection(config), Redis.connection(config)]) : [await Redis.connection(config), await Redis.connection(config)] // client checks expect(RedisMock).toHaveBeenNthCalledWith(1, { defaults: { username: config.username, ...(config.password ? { password: config.password } : {}), socket: { tls: config.enableTLS, connectTimeout: 10000, }, }, rootNodes: [ { url: `redis://${config.username}:${config.username}@${config.hostname}:6379`, disableOfflineQueue: true, }, ], }) // Does not have double connection expect(provider['connectionId']).toEqual(provider2['connectionId']) expect(provider.connect).toHaveBeenCalledTimes(1) expect(provider2.connect).toHaveBeenCalledTimes(1) } describe('Redis (cluster)', () => { beforeEach(async () => { Redis['clearSingletons']() _connectionId = 1 }) test('Simple redis - do not connect twice', async () => { await simpleRedisTest({ hostname: 'redis://localhost', username: 'gabe', password: 'mypassword', enableTLS: true, clusterMode: true, type: 'redis', }) }) test('Simple redis (no SSL) - do not connect twice', async () => { await simpleRedisTest({ hostname: 'redis://localhost', username: 'gabe', password: 'mypassword', enableTLS: false, clusterMode: true, type: 'redis', }) }) test('Simple redis (passwordless) - do not connect twice', async () => { await simpleRedisTest({ hostname: 'redis://localhost', username: 'gabe', enableTLS: false, clusterMode: true, type: 'redis', }) }) test('Concurrent redis - do not connect twice', async () => { await simpleRedisTest( { hostname: 'redis://localhost', username: 'gabe', enableTLS: false, clusterMode: true, type: 'redis', }, true ) }) test('Cluster then standalone returns different clients', async () => { const clientFactory = jest.fn(() => createRedisConnection()) const clusterFactory = jest.fn(() => createRedisConnection()) Redis['ClientFactory'] = clientFactory Redis['ClusterFactory'] = clusterFactory const cluster = await Redis.connection({ hostname: 'redis://localhost', username: 'gabe', enableTLS: false, clusterMode: true, type: 'redis', }) const standalone = await Redis.connection({ hostname: 'redis://localhost', username: 'gabe', enableTLS: false, type: 'redis', }) expect(cluster['connectionId']).not.toEqual(standalone['connectionId']) expect(clusterFactory).toHaveBeenCalledTimes(1) expect(clientFactory).toHaveBeenCalledTimes(1) }) })