forked from valkey-io/valkey-glide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisClusterClient.test.ts
More file actions
855 lines (761 loc) · 32.6 KB
/
RedisClusterClient.test.ts
File metadata and controls
855 lines (761 loc) · 32.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/**
* Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
*/
import {
afterAll,
afterEach,
beforeAll,
describe,
expect,
it,
} from "@jest/globals";
import { v4 as uuidv4 } from "uuid";
import {
ClusterClientConfiguration,
ClusterTransaction,
GlideClusterClient,
InfoOptions,
ProtocolVersion,
Routes,
} from "..";
import { RedisCluster } from "../../utils/TestUtils.js";
import { FlushMode } from "../build-ts/src/commands/FlushMode";
import { checkIfServerVersionLessThan, runBaseTests } from "./SharedTests";
import {
checkClusterResponse,
checkSimple,
flushAndCloseClient,
generateLuaLibCode,
getClientConfigurationOption,
getFirstResult,
intoArray,
intoString,
parseCommandLineArgs,
parseEndpoints,
transactionTest,
} from "./TestUtilities";
type Context = {
client: GlideClusterClient;
};
const TIMEOUT = 50000;
describe("GlideClusterClient", () => {
let testsFailed = 0;
let cluster: RedisCluster;
let client: GlideClusterClient;
beforeAll(async () => {
const clusterAddresses = parseCommandLineArgs()["cluster-endpoints"];
// Connect to cluster or create a new one based on the parsed addresses
cluster = clusterAddresses
? RedisCluster.initFromExistingCluster(
parseEndpoints(clusterAddresses),
)
: // setting replicaCount to 1 to facilitate tests routed to replicas
await RedisCluster.createCluster(true, 3, 1);
}, 20000);
afterEach(async () => {
await flushAndCloseClient(true, cluster.getAddresses(), client);
});
afterAll(async () => {
if (testsFailed === 0) {
await cluster.close();
}
});
runBaseTests<Context>({
init: async (protocol, clientName?) => {
const options = getClientConfigurationOption(
cluster.getAddresses(),
protocol,
);
options.protocol = protocol;
options.clientName = clientName;
testsFailed += 1;
client = await GlideClusterClient.createClient(options);
return {
context: {
client,
},
client,
};
},
close: (context: Context, testSucceeded: boolean) => {
if (testSucceeded) {
testsFailed -= 1;
}
},
timeout: TIMEOUT,
});
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`info with server and replication_%p`,
async (protocol) => {
client = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const info_server = getFirstResult(
await client.info([InfoOptions.Server]),
);
expect(intoString(info_server)).toEqual(
expect.stringContaining("# Server"),
);
const infoReplicationValues = Object.values(
await client.info([InfoOptions.Replication]),
);
const replicationInfo = intoArray(infoReplicationValues);
for (const item of replicationInfo) {
expect(item).toContain("role:master");
expect(item).toContain("# Replication");
}
},
TIMEOUT,
);
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`info with server and randomNode route_%p`,
async (protocol) => {
client = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const result = await client.info(
[InfoOptions.Server],
"randomNode",
);
expect(intoString(result)).toEqual(
expect.stringContaining("# Server"),
);
expect(intoString(result)).toEqual(
expect.not.stringContaining("# Errorstats"),
);
},
TIMEOUT,
);
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`route by address reaches correct node_%p`,
async (protocol) => {
// returns the line that contains the word "myself", up to that point. This is done because the values after it might change with time.
const cleanResult = (value: string) => {
return (
value
.split("\n")
.find((line: string) => line.includes("myself"))
?.split("myself")[0] ?? ""
);
};
client = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const result = cleanResult(
intoString(
await client.customCommand(
["cluster", "nodes"],
"randomNode",
),
),
);
// check that routing without explicit port works
const host = result.split(" ")[1].split("@")[0] ?? "";
if (!host) {
throw new Error("No host could be parsed");
}
const secondResult = cleanResult(
intoString(
await client.customCommand(["cluster", "nodes"], {
type: "routeByAddress",
host,
}),
),
);
expect(result).toEqual(secondResult);
const [host2, port] = host.split(":");
// check that routing with explicit port works
const thirdResult = cleanResult(
intoString(
await client.customCommand(["cluster", "nodes"], {
type: "routeByAddress",
host: host2,
port: Number(port),
}),
),
);
expect(result).toEqual(thirdResult);
},
TIMEOUT,
);
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`fail routing by address if no port is provided_%p`,
async (protocol) => {
client = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
expect(() =>
client.info(undefined, {
type: "routeByAddress",
host: "foo",
}),
).toThrowError();
},
TIMEOUT,
);
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`config get and config set transactions test_%p`,
async (protocol) => {
client = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const transaction = new ClusterTransaction();
transaction.configSet({ timeout: "1000" });
transaction.configGet(["timeout"]);
const result = await client.exec(transaction);
expect(intoString(result)).toEqual(
intoString(["OK", { timeout: "1000" }]),
);
},
TIMEOUT,
);
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`can send transactions_%p`,
async (protocol) => {
client = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const transaction = new ClusterTransaction();
const expectedRes = await transactionTest(transaction);
const result = await client.exec(transaction);
expect(intoString(result)).toEqual(intoString(expectedRes));
},
TIMEOUT,
);
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`can return null on WATCH transaction failures_%p`,
async (protocol) => {
const client1 = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const client2 = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const transaction = new ClusterTransaction();
transaction.get("key");
const result1 = await client1.customCommand(["WATCH", "key"]);
expect(result1).toEqual("OK");
const result2 = await client2.set("key", "foo");
expect(result2).toEqual("OK");
const result3 = await client1.exec(transaction);
expect(result3).toBeNull();
client1.close();
client2.close();
},
TIMEOUT,
);
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`echo with all nodes routing_%p`,
async (protocol) => {
client = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const message = uuidv4();
const echoDict = await client.echo(message, "allNodes");
expect(typeof echoDict).toBe("object");
expect(intoArray(echoDict)).toEqual(
expect.arrayContaining(intoArray([message])),
);
},
TIMEOUT,
);
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`check that multi key command returns a cross slot error`,
async (protocol) => {
const client = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const versionLessThan7 =
await checkIfServerVersionLessThan("7.0.0");
const promises: Promise<unknown>[] = [
client.blpop(["abc", "zxy", "lkn"], 0.1),
client.rename("abc", "zxy"),
client.brpop(["abc", "zxy", "lkn"], 0.1),
client.smove("abc", "zxy", "value"),
client.renamenx("abc", "zxy"),
client.sinter(["abc", "zxy", "lkn"]),
client.sintercard(["abc", "zxy", "lkn"]),
client.sinterstore("abc", ["zxy", "lkn"]),
client.zinterstore("abc", ["zxy", "lkn"]),
client.zdiff(["abc", "zxy", "lkn"]),
client.zdiffWithScores(["abc", "zxy", "lkn"]),
client.zdiffstore("abc", ["zxy", "lkn"]),
client.sunionstore("abc", ["zxy", "lkn"]),
client.sunion(["abc", "zxy", "lkn"]),
client.pfcount(["abc", "zxy", "lkn"]),
client.sdiff(["abc", "zxy", "lkn"]),
client.sdiffstore("abc", ["zxy", "lkn"]),
// TODO all rest multi-key commands except ones tested below
];
if (!versionLessThan7) {
promises.push(client.zintercard(["abc", "zxy", "lkn"]));
}
for (const promise of promises) {
try {
await promise;
} catch (e) {
expect((e as Error).message.toLowerCase()).toContain(
"crossslot",
);
}
}
client.close();
},
);
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`check that multi key command routed to multiple nodes`,
async (protocol) => {
const client = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
await client.exists(["abc", "zxy", "lkn"]);
await client.unlink(["abc", "zxy", "lkn"]);
await client.del(["abc", "zxy", "lkn"]);
await client.mget(["abc", "zxy", "lkn"]);
await client.mset({ abc: "1", zxy: "2", lkn: "3" });
// TODO touch
client.close();
},
);
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"object freq transaction test_%p",
async (protocol) => {
const client = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const key = uuidv4();
const maxmemoryPolicyKey = "maxmemory-policy";
const config = await client.configGet([maxmemoryPolicyKey]);
const maxmemoryPolicy = String(config[maxmemoryPolicyKey]);
try {
const transaction = new ClusterTransaction();
transaction.configSet({
[maxmemoryPolicyKey]: "allkeys-lfu",
});
transaction.set(key, "foo");
transaction.objectFreq(key);
const response = await client.exec(transaction);
expect(response).not.toBeNull();
if (response != null) {
expect(response.length).toEqual(3);
expect(response[0]).toEqual("OK");
expect(response[1]).toEqual("OK");
expect(response[2]).toBeGreaterThanOrEqual(0);
}
} finally {
expect(
await client.configSet({
[maxmemoryPolicyKey]: maxmemoryPolicy,
}),
).toEqual("OK");
}
client.close();
},
);
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"object idletime transaction test_%p",
async (protocol) => {
const client = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const key = uuidv4();
const maxmemoryPolicyKey = "maxmemory-policy";
const config = await client.configGet([maxmemoryPolicyKey]);
const maxmemoryPolicy = String(config[maxmemoryPolicyKey]);
try {
const transaction = new ClusterTransaction();
transaction.configSet({
// OBJECT IDLETIME requires a non-LFU maxmemory-policy
[maxmemoryPolicyKey]: "allkeys-random",
});
transaction.set(key, "foo");
transaction.objectIdletime(key);
const response = await client.exec(transaction);
expect(response).not.toBeNull();
if (response != null) {
expect(response.length).toEqual(3);
// transaction.configSet({[maxmemoryPolicyKey]: "allkeys-random"});
expect(response[0]).toEqual("OK");
// transaction.set(key, "foo");
expect(response[1]).toEqual("OK");
// transaction.objectIdletime(key);
expect(response[2]).toBeGreaterThanOrEqual(0);
}
} finally {
expect(
await client.configSet({
[maxmemoryPolicyKey]: maxmemoryPolicy,
}),
).toEqual("OK");
}
client.close();
},
);
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"object refcount transaction test_%p",
async (protocol) => {
const client = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const key = uuidv4();
const transaction = new ClusterTransaction();
transaction.set(key, "foo");
transaction.objectRefcount(key);
const response = await client.exec(transaction);
expect(response).not.toBeNull();
if (response != null) {
expect(response.length).toEqual(2);
expect(response[0]).toEqual("OK"); // transaction.set(key, "foo");
expect(response[1]).toBeGreaterThanOrEqual(1); // transaction.objectRefcount(key);
}
client.close();
},
);
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`lolwut test_%p`,
async (protocol) => {
client = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
// test with multi-node route
const result1 = await client.lolwut({}, "allNodes");
expect(intoString(result1)).toEqual(
expect.stringContaining("Redis ver. "),
);
const result2 = await client.lolwut(
{ version: 2, parameters: [10, 20] },
"allNodes",
);
expect(intoString(result2)).toEqual(
expect.stringContaining("Redis ver. "),
);
// test with single-node route
const result3 = await client.lolwut({}, "randomNode");
expect(intoString(result3)).toEqual(
expect.stringContaining("Redis ver. "),
);
const result4 = await client.lolwut(
{ version: 2, parameters: [10, 20] },
"randomNode",
);
expect(intoString(result4)).toEqual(
expect.stringContaining("Redis ver. "),
);
// transaction tests
const transaction = new ClusterTransaction();
transaction.lolwut();
transaction.lolwut({ version: 5 });
transaction.lolwut({ parameters: [1, 2] });
transaction.lolwut({ version: 6, parameters: [42] });
const results = await client.exec(transaction);
if (results) {
for (const element of results) {
expect(intoString(element)).toEqual(
expect.stringContaining("Redis ver. "),
);
}
} else {
throw new Error("Invalid LOLWUT transaction test results.");
}
client.close();
},
TIMEOUT,
);
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"flushdb flushall dbsize test_%p",
async (protocol) => {
const client = await GlideClusterClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
expect(await client.dbsize()).toBeGreaterThanOrEqual(0);
checkSimple(await client.set(uuidv4(), uuidv4())).toEqual("OK");
expect(await client.dbsize()).toBeGreaterThan(0);
checkSimple(await client.flushall()).toEqual("OK");
expect(await client.dbsize()).toEqual(0);
checkSimple(await client.set(uuidv4(), uuidv4())).toEqual("OK");
expect(await client.dbsize()).toEqual(1);
checkSimple(await client.flushdb(FlushMode.ASYNC)).toEqual("OK");
expect(await client.dbsize()).toEqual(0);
checkSimple(await client.set(uuidv4(), uuidv4())).toEqual("OK");
expect(await client.dbsize()).toEqual(1);
checkSimple(await client.flushdb(FlushMode.SYNC)).toEqual("OK");
expect(await client.dbsize()).toEqual(0);
client.close();
},
);
describe.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"Protocol is RESP2 = %s",
(protocol) => {
describe.each([true, false])(
"Single node route = %s",
(singleNodeRoute) => {
it(
"function load",
async () => {
if (await checkIfServerVersionLessThan("7.0.0"))
return;
const client =
await GlideClusterClient.createClient(
getClientConfigurationOption(
cluster.getAddresses(),
protocol,
),
);
try {
const libName =
"mylib1C" + uuidv4().replaceAll("-", "");
const funcName =
"myfunc1c" + uuidv4().replaceAll("-", "");
const code = generateLuaLibCode(
libName,
new Map([[funcName, "return args[1]"]]),
true,
);
const route: Routes = singleNodeRoute
? { type: "primarySlotKey", key: "1" }
: "allPrimaries";
// TODO use commands instead of customCommand once implemented
// verify function does not yet exist
const functionList = await client.customCommand(
[
"FUNCTION",
"LIST",
"LIBRARYNAME",
libName,
],
);
checkClusterResponse(
functionList as object,
singleNodeRoute,
(value) => expect(value).toEqual([]),
);
// load the library
checkSimple(
await client.functionLoad(code),
).toEqual(libName);
// call functions from that library to confirm that it works
let fcall = await client.customCommand(
["FCALL", funcName, "0", "one", "two"],
route,
);
checkClusterResponse(
fcall as object,
singleNodeRoute,
(value) =>
checkSimple(value).toEqual("one"),
);
fcall = await client.customCommand(
["FCALL_RO", funcName, "0", "one", "two"],
route,
);
checkClusterResponse(
fcall as object,
singleNodeRoute,
(value) =>
checkSimple(value).toEqual("one"),
);
// re-load library without replace
await expect(
client.functionLoad(code),
).rejects.toThrow(
`Library '${libName}' already exists`,
);
// re-load library with replace
checkSimple(
await client.functionLoad(code, true),
).toEqual(libName);
// overwrite lib with new code
const func2Name =
"myfunc2c" + uuidv4().replaceAll("-", "");
const newCode = generateLuaLibCode(
libName,
new Map([
[funcName, "return args[1]"],
[func2Name, "return #args"],
]),
true,
);
checkSimple(
await client.functionLoad(newCode, true),
).toEqual(libName);
fcall = await client.customCommand(
["FCALL", func2Name, "0", "one", "two"],
route,
);
checkClusterResponse(
fcall as object,
singleNodeRoute,
(value) => expect(value).toEqual(2),
);
fcall = await client.customCommand(
["FCALL_RO", func2Name, "0", "one", "two"],
route,
);
checkClusterResponse(
fcall as object,
singleNodeRoute,
(value) => expect(value).toEqual(2),
);
} finally {
expect(await client.functionFlush()).toEqual(
"OK",
);
client.close();
}
},
TIMEOUT,
);
},
);
},
);
describe.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"Protocol is RESP2 = %s",
(protocol) => {
describe.each([true, false])(
"Single node route = %s",
(singleNodeRoute) => {
it(
"function flush",
async () => {
if (await checkIfServerVersionLessThan("7.0.0"))
return;
const client =
await GlideClusterClient.createClient(
getClientConfigurationOption(
cluster.getAddresses(),
protocol,
),
);
try {
const libName =
"mylib1C" + uuidv4().replaceAll("-", "");
const funcName =
"myfunc1c" + uuidv4().replaceAll("-", "");
const code = generateLuaLibCode(
libName,
new Map([[funcName, "return args[1]"]]),
true,
);
const route: Routes = singleNodeRoute
? { type: "primarySlotKey", key: "1" }
: "allPrimaries";
// TODO use commands instead of customCommand once implemented
// verify function does not yet exist
const functionList1 =
await client.customCommand([
"FUNCTION",
"LIST",
"LIBRARYNAME",
libName,
]);
checkClusterResponse(
functionList1 as object,
singleNodeRoute,
(value) => expect(value).toEqual([]),
);
// load the library
checkSimple(
await client.functionLoad(
code,
undefined,
route,
),
).toEqual(libName);
// flush functions
expect(
await client.functionFlush(
FlushMode.SYNC,
route,
),
).toEqual("OK");
expect(
await client.functionFlush(
FlushMode.ASYNC,
route,
),
).toEqual("OK");
// TODO use commands instead of customCommand once implemented
// verify function does not exist
const functionList2 =
await client.customCommand([
"FUNCTION",
"LIST",
"LIBRARYNAME",
libName,
]);
checkClusterResponse(
functionList2 as object,
singleNodeRoute,
(value) => expect(value).toEqual([]),
);
// Attempt to re-load library without overwriting to ensure FLUSH was effective
checkSimple(
await client.functionLoad(
code,
undefined,
route,
),
).toEqual(libName);
} finally {
expect(await client.functionFlush()).toEqual(
"OK",
);
client.close();
}
},
TIMEOUT,
);
},
);
},
);
it.each([
[true, ProtocolVersion.RESP3],
[false, ProtocolVersion.RESP3],
])("simple pubsub test", async (sharded, protocol) => {
if (sharded && (await checkIfServerVersionLessThan("7.2.0"))) {
return;
}
const channel = "test-channel";
const shardedChannel = "test-channel-sharded";
const channelsAndPatterns: Partial<
Record<ClusterClientConfiguration.PubSubChannelModes, Set<string>>
> = {
[ClusterClientConfiguration.PubSubChannelModes.Exact]: new Set([
channel,
]),
};
if (sharded) {
channelsAndPatterns[
ClusterClientConfiguration.PubSubChannelModes.Sharded
] = new Set([shardedChannel]);
}
const config: ClusterClientConfiguration = getClientConfigurationOption(
cluster.getAddresses(),
protocol,
);
config.pubsubSubscriptions = {
channelsAndPatterns: channelsAndPatterns,
};
client = await GlideClusterClient.createClient(config);
const message = uuidv4();
await client.publish(message, channel);
const sleep = new Promise((resolve) => setTimeout(resolve, 1000));
await sleep;
let pubsubMsg = await client.getPubSubMessage();
expect(pubsubMsg.channel.toString()).toBe(channel);
expect(pubsubMsg.message.toString()).toBe(message);
expect(pubsubMsg.pattern).toBeNull();
if (sharded) {
await client.publish(message, shardedChannel, true);
await sleep;
pubsubMsg = await client.getPubSubMessage();
console.log(pubsubMsg);
expect(pubsubMsg.channel.toString()).toBe(shardedChannel);
expect(pubsubMsg.message.toString()).toBe(message);
expect(pubsubMsg.pattern).toBeNull();
}
client.close();
});
});