Skip to content

Commit

Permalink
fix: 🐛 wait for routing table in EVAL command
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jan 16, 2024
1 parent 52ccd19 commit ea2d3d0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/cluster/RedisCluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ export class RedisCluster implements Printable {
args: (string | Uint8Array)[],
opts?: CmdOpts,
): Promise<unknown> {
if (!this._routerReady) await this.whenRouterReady();
const script = this.scripts.get(id);
if (!script) throw new Error('SCRIPT_NOT_REGISTERED');
const isWrite = true;
Expand Down
38 changes: 38 additions & 0 deletions src/cluster/__tests__/scripts.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {RedisCluster} from '../RedisCluster';

const setup = () => {
const cluster = new RedisCluster({
seeds: [{host: '127.0.0.1', port: 7000}],
});
return {cluster};
};

let keyCnt = 1;
const getKey = () => '{redis-cluster-script}-test-' + keyCnt++;

if (process.env.TEST_LOCAL_CLUSTER) {
test('can execute a script', async () => {
const {cluster} = setup();
cluster.start();
const scriptId = 'hello-scripting-cluster-test-' + Date.now();
cluster.scripts.set(scriptId, "return 'Hello, scripting!'");
const res = await cluster.eval(scriptId, 0, [], [], {utf8Res: true});
expect(res).toBe('Hello, scripting!');
cluster.stop();
});

test('can run a with keys and arguments', async () => {
const {cluster} = setup();
cluster.start();
const scriptName = '{add-script-cluster}-' + Date.now();
cluster.scripts.set(scriptName, `return tonumber(redis.call("get",KEYS[1])) + tonumber(ARGV[1])`);
const key = getKey();
await cluster.cmd(['SET', key, '1']);
const res = await cluster.eval(scriptName, 1, [key], ['2'], {utf8Res: true});
expect(res).toBe(3);
cluster.stop();
});

} else {
test.todo('To enable cluster tests, set TEST_LOCAL_CLUSTER=1 in your environment variables.');
}
2 changes: 0 additions & 2 deletions src/standalone/StandaloneClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,10 @@ export class StandaloneClient {
const call = responses[i];
if (call) decoder.tryUtf8 = !!call.utf8Res;
const msg = decoder.read();
// console.log(msg);
if (msg === undefined) break;
if (msg instanceof RespPush) {
this.onPush.emit(msg);
const val = msg.val;
// console.log('push', Buffer.from(val[0] as any).toString());
if (isPushMessage(val)) {
const fanout = this.subs.get(val[1] as Uint8Array);
if (fanout) fanout.emit(val[2] as Uint8Array);
Expand Down

0 comments on commit ea2d3d0

Please sign in to comment.