Skip to content

Commit 2203be5

Browse files
committed
add test for RootNodesUnavailableError
1 parent 8f88eb2 commit 2203be5

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

packages/client/lib/cluster/cluster-slots.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import RedisClient, { InstantiableRedisClient, RedisClientType } from '../client
22
import { RedisClusterMasterNode, RedisClusterReplicaNode } from '../commands/CLUSTER_NODES';
33
import { RedisClusterClientOptions, RedisClusterOptions } from '.';
44
import { RedisCommandArgument, RedisModules, RedisScripts } from '../commands';
5+
import { RootNodesUnavailableError } from '../errors';
56

67
// We need to use 'require', because it's not possible with Typescript to import
78
// function that are exported as 'module.exports = function`, without esModuleInterop
@@ -39,7 +40,7 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisSc
3940
if (await this.#discoverNodes(this.#clientOptionsDefaults(rootNode))) return;
4041
}
4142

42-
throw new Error('None of the root nodes is available');
43+
throw new RootNodesUnavailableError();
4344
}
4445

4546
async #discoverNodes(clientOptions?: RedisClusterClientOptions): Promise<boolean> {

packages/client/lib/cluster/index.spec.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { strict as assert } from 'assert';
22
import testUtils, { GLOBAL } from '../test-utils';
3+
import RedisCluster from '.';
34
import { ClusterSlotStates } from '../commands/CLUSTER_SETSLOT';
45
import { SQUARE_SCRIPT } from '../client/index.spec';
6+
import { RootNodesUnavailableError } from '../errors';
57

68
// We need to use 'require', because it's not possible with Typescript to import
79
// function that are exported as 'module.exports = function`, without esModuleInterop
@@ -10,20 +12,14 @@ const calculateSlot = require('cluster-key-slot');
1012

1113
describe('Cluster', () => {
1214
testUtils.testWithCluster('sendCommand', async cluster => {
13-
await cluster.connect();
14-
15-
try {
16-
await cluster.publish('channel', 'message');
17-
await cluster.set('a', 'b');
18-
await cluster.set('a{a}', 'bb');
19-
await cluster.set('aa', 'bb');
20-
await cluster.get('aa');
21-
await cluster.get('aa');
22-
await cluster.get('aa');
23-
await cluster.get('aa');
24-
} finally {
25-
await cluster.disconnect();
26-
}
15+
await cluster.publish('channel', 'message');
16+
await cluster.set('a', 'b');
17+
await cluster.set('a{a}', 'bb');
18+
await cluster.set('aa', 'bb');
19+
await cluster.get('aa');
20+
await cluster.get('aa');
21+
await cluster.get('aa');
22+
await cluster.get('aa');
2723
}, GLOBAL.CLUSTERS.OPEN);
2824

2925
testUtils.testWithCluster('multi', async cluster => {
@@ -51,6 +47,22 @@ describe('Cluster', () => {
5147
}
5248
});
5349

50+
it('should throw RootNodesUnavailableError', async () => {
51+
const cluster = RedisCluster.create({
52+
rootNodes: []
53+
});
54+
55+
try {
56+
await assert.rejects(
57+
cluster.connect(),
58+
RootNodesUnavailableError
59+
);
60+
} catch (err) {
61+
await cluster.disconnect();
62+
throw err;
63+
}
64+
});
65+
5466
testUtils.testWithCluster('should handle live resharding', async cluster => {
5567
const key = 'key',
5668
value = 'value';

packages/client/lib/errors.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@ export class AuthError extends Error {
3939
super(message);
4040
}
4141
}
42+
43+
export class RootNodesUnavailableError extends Error {
44+
constructor() {
45+
super('All the root nodes are unavailable');
46+
}
47+
}

0 commit comments

Comments
 (0)