forked from redis/node-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.spec.ts
115 lines (99 loc) · 3.41 KB
/
cluster.spec.ts
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
import { strict as assert } from 'assert';
import RedisCluster from './cluster';
import { defineScript } from './lua-script';
import { itWithCluster, itWithDedicatedCluster, TestRedisClusters, TEST_REDIS_CLUSTERES } from './test-utils';
import calculateSlot from 'cluster-key-slot';
import { ClusterSlotStates } from './commands/CLUSTER_SETSLOT';
describe('Cluster', () => {
it('sendCommand', async () => {
const cluster = RedisCluster.create({
rootNodes: TEST_REDIS_CLUSTERES[TestRedisClusters.OPEN],
useReplicas: true
});
await cluster.connect();
try {
await cluster.ping();
await cluster.set('a', 'b');
await cluster.set('a{a}', 'bb');
await cluster.set('aa', 'bb');
await cluster.get('aa');
await cluster.get('aa');
await cluster.get('aa');
await cluster.get('aa');
} finally {
await cluster.disconnect();
}
});
itWithCluster(TestRedisClusters.OPEN, 'multi', async cluster => {
const key = 'key';
assert.deepEqual(
await cluster.multi(key)
.ping()
.set(key, 'value')
.get(key)
.exec(),
['PONG', 'OK', 'value']
);
});
it('scripts', async () => {
const cluster = RedisCluster.create({
rootNodes: TEST_REDIS_CLUSTERES[TestRedisClusters.OPEN],
scripts: {
add: defineScript({
NUMBER_OF_KEYS: 0,
SCRIPT: 'return ARGV[1] + 1;',
transformArguments(number: number): Array<string> {
assert.equal(number, 1);
return [number.toString()];
},
transformReply(reply: number): number {
assert.equal(reply, 2);
return reply;
}
})
}
});
await cluster.connect();
try {
assert.equal(
await cluster.add(1),
2
);
} finally {
await cluster.disconnect();
}
});
itWithDedicatedCluster('should handle live resharding', async cluster => {
const key = 'key',
value = 'value';
await cluster.set(key, value);
const slot = calculateSlot(key),
from = cluster.getSlotMaster(slot),
to = cluster.getMasters().find(node => node.id !== from.id);
await to!.client.clusterSetSlot(slot, ClusterSlotStates.IMPORTING, from.id);
// should be able to get the key from the original node before it was migrated
assert.equal(
await cluster.get(key),
value
);
await from.client.clusterSetSlot(slot, ClusterSlotStates.MIGRATING, to!.id);
// should be able to get the key from the original node using the "ASKING" command
assert.equal(
await cluster.get(key),
value
);
const { port: toPort } = <any>to!.client.options!.socket;
await from.client.migrate(
'127.0.0.1',
toPort,
key,
0,
10
);
// should be able to get the key from the new node
assert.equal(
await cluster.get(key),
value
);
});
});