From 1b6cf3a9154c9696113540e55010ddc2494a8952 Mon Sep 17 00:00:00 2001 From: Jared Wray Date: Tue, 19 Nov 2024 23:18:58 -0700 Subject: [PATCH] adding in tests but will break --- docker-compose-arm64.yaml | 24 ++++++++++++++- packages/redis/src/index.ts | 12 +++----- packages/redis/test/cluster.ts | 56 ++++++++++++++++++++++++++++++++++ packages/redis/test/test.ts | 17 ----------- 4 files changed, 83 insertions(+), 26 deletions(-) create mode 100644 packages/redis/test/cluster.ts diff --git a/docker-compose-arm64.yaml b/docker-compose-arm64.yaml index bc24b9cb..4fc28717 100644 --- a/docker-compose-arm64.yaml +++ b/docker-compose-arm64.yaml @@ -94,4 +94,26 @@ services: - ALLOW_NONE_AUTHENTICATION=yes ports: - 2379:2379 - - 2380:2380 \ No newline at end of file + - 2380:2380 + redis-node-1: + image: redis:latest + command: ["sh", "-c", "redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly no"] + ports: + - "7001:6379" + redis-node-2: + image: redis:latest + command: ["sh", "-c", "redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly no"] + ports: + - "7002:6379" + redis-node-3: + image: redis:latest + command: ["sh", "-c", "redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly no"] + ports: + - "7003:6379" + redis-setup: + image: redis:latest + depends_on: + - redis-node-1 + - redis-node-2 + - redis-node-3 + entrypoint: [ "sh", "-c", "sleep 5 && echo yes | redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 --cluster-replicas 0" ] \ No newline at end of file diff --git a/packages/redis/src/index.ts b/packages/redis/src/index.ts index 681b32bc..0bf9f176 100644 --- a/packages/redis/src/index.ts +++ b/packages/redis/src/index.ts @@ -1,6 +1,6 @@ import EventEmitter from 'node:events'; import { - createClient, type RedisClientType, type RedisClientOptions, type RedisClusterType, + createClient, createCluster, type RedisClientType, type RedisClientOptions, type RedisClusterType, type RedisClusterOptions, type RedisModules, type RedisFunctions, @@ -78,7 +78,7 @@ export default class KeyvRedis extends EventEmitter implements KeyvStoreAdapter } else if ((connect as any).connect !== undefined) { this._client = this.isClientCluster(connect as RedisClientConnectionType) ? connect as RedisClusterType : connect as RedisClientType; } else if (connect instanceof Object) { - this._client = createClient(connect as RedisClientOptions) as RedisClientType; + this._client = (connect as any).rootNodes === undefined ? createClient(connect as RedisClientOptions) as RedisClientType : createCluster(connect as RedisClusterOptions) as RedisClusterType; } } @@ -397,12 +397,7 @@ export default class KeyvRedis extends EventEmitter implements KeyvStoreAdapter * @returns {AsyncGenerator<[string, T | undefined], void, unknown>} - async iterator with key value pairs */ public async * iterator(namespace?: string): AsyncGenerator<[string, Value | undefined], void, unknown> { - if (this.isCluster()) { - /* c8 ignore next 2 */ - throw new Error('iterator is not supported for clusters at this time'); - } else { - yield * this.iteratorClient(namespace); - } + yield * (this.isCluster() ? this.iteratorCluster(namespace) : this.iteratorClient(namespace)); } /** @@ -567,6 +562,7 @@ export default class KeyvRedis extends EventEmitter implements KeyvStoreAdapter do { // Use sendCommand to execute SCAN directly on the shard node const client = createClient(node); + // eslint-disable-next-line no-await-in-loop, @typescript-eslint/naming-convention const response = await (client as RedisClientType).scan(Number.parseInt(cursor, 10), {MATCH: match, COUNT: batchSize, TYPE: 'string'}); diff --git a/packages/redis/test/cluster.ts b/packages/redis/test/cluster.ts new file mode 100644 index 00000000..94d61b24 --- /dev/null +++ b/packages/redis/test/cluster.ts @@ -0,0 +1,56 @@ +import {describe, test, expect} from 'vitest'; +import KeyvRedis, {createCluster} from '../src/index.js'; + +const defaultClusterOptions = { + rootNodes: [ + { + url: 'redis://localhost:7001', + }, + { + url: 'redis://localhost:7002', + }, + { + url: 'redis://localhost:7003', + }, + ], + useReplicas: true, +}; + +describe('KeyvRedis Cluster', () => { + test('should be able to connect to a cluster', async () => { + const cluster = createCluster(defaultClusterOptions); + + const keyvRedis = new KeyvRedis(cluster); + + expect(keyvRedis).toBeDefined(); + expect(keyvRedis.client).toEqual(cluster); + }); + + test('should be able to send in cluster options', async () => { + const keyvRedis = new KeyvRedis(defaultClusterOptions); + expect(keyvRedis.isCluster()).toBe(true); + }); + + test('shoudl be able to set the redis cluster client', async () => { + const cluster = createCluster(defaultClusterOptions); + + const keyvRedis = new KeyvRedis(); + expect(keyvRedis.isCluster()).toBe(false); + + keyvRedis.client = cluster; + expect(keyvRedis.client).toEqual(cluster); + expect(keyvRedis.isCluster()).toBe(true); + }); + + test('should be able to set a value', async () => { + const cluster = createCluster(defaultClusterOptions); + + const keyvRedis = new KeyvRedis(cluster); + + await keyvRedis.set('test-cl', 'test'); + + const result = await keyvRedis.get('test-cl'); + + expect(result).toBe('test'); + }); +}); diff --git a/packages/redis/test/test.ts b/packages/redis/test/test.ts index 662b4a40..4365fadc 100644 --- a/packages/redis/test/test.ts +++ b/packages/redis/test/test.ts @@ -83,8 +83,6 @@ describe('KeyvRedis', () => { test('should be able to get and set opts', async () => { const keyvRedis = new KeyvRedis(); keyvRedis.opts = {namespace: 'test', keyPrefixSeparator: ':1', clearBatchSize: 2000}; - const client = await keyvRedis.getClient() as RedisClientType; - console.log(client.options); expect(keyvRedis.opts).toEqual({ namespace: 'test', keyPrefixSeparator: ':1', clearBatchSize: 2000, dialect: 'redis', url: 'redis://localhost:6379', @@ -382,18 +380,3 @@ describe('KeyvRedis Iterators', () => { await keyvRedis.disconnect(); }); }); - -describe('KeyvRedis Cluster', () => { - test('should be able to connect to a cluster', async () => { - const cluster = createCluster({ - rootNodes: [ - { - url: 'redis://localhost:6379', - }, - ], - useReplicas: true, - }); - - const keyvRedis = new KeyvRedis(cluster); - }); -});