Skip to content

Commit

Permalink
adding in tests but will break
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwray committed Nov 20, 2024
1 parent ced5d8f commit 1b6cf3a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 26 deletions.
24 changes: 23 additions & 1 deletion docker-compose-arm64.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,26 @@ services:
- ALLOW_NONE_AUTHENTICATION=yes
ports:
- 2379:2379
- 2380:2380
- 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" ]
12 changes: 4 additions & 8 deletions packages/redis/src/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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<Value>(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));
}

/**
Expand Down Expand Up @@ -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'});

Expand Down
56 changes: 56 additions & 0 deletions packages/redis/test/cluster.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
17 changes: 0 additions & 17 deletions packages/redis/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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);
});
});

0 comments on commit 1b6cf3a

Please sign in to comment.