/
connection.test.js
118 lines (81 loc) · 3.28 KB
/
connection.test.js
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
const ioredis = require("ioredis");
const QuidolRedisCache = require("./index.js");
jest.mock('ioredis');
const defaultOptions = QuidolRedisCache.getDefaults();
beforeEach(() => {
ioredis.mockClear();
ioredis.Cluster.mockClear();
QuidolRedisCache.resetConnectionMap();
});
test("should throw if an unknown type is provided", () => {
expect(() => {
new QuidolRedisCache({type: "unknown"});
}).toThrow();
});
// Standalone
test("should throw if no host and port are provided with stanalone connection", () => {
const redisOptions = {};
const options = {};
expect(() => {
new QuidolRedisCache({type: "standalone", redisOptions, options});
}).toThrow();
});
test("should accept standalone connection options", () => {
const redisOptions = { host: "localhost", port: 6379 };
const options = {};
const cache = new QuidolRedisCache({type: "standalone", redisOptions, options});
expect(ioredis).toHaveBeenCalledWith(redisOptions);
});
test("should default standalone connection options", () => {
const redisOptions = { host: "localhost", port: 6379 };
const options = defaultOptions;
const cache = new QuidolRedisCache({redisOptions});
expect(ioredis).toHaveBeenCalledWith({ ...redisOptions, ...options });
});
test("should pool standalone connection", () => {
const redisOptions = { host: "localhost", port: 6379 };
const options = defaultOptions;
const cache = [];
for (const i of Array(5).keys()) {
cache[i] = new QuidolRedisCache({redisOptions});
}
expect(ioredis).toHaveBeenCalledTimes(1);
});
// Sentinel
test("should accept sentinel connection options", () => {
const redisSentinelOptions = { host: "localhost", port: 6379 };
const options = {};
const cache = new QuidolRedisCache({type: "sentinel", redisSentinelOptions, options});
expect(ioredis).toHaveBeenCalledWith({ ...redisSentinelOptions, ...options });
});
test("should pool sentinel connection", () => {
const redisSentinelOptions = { host: "localhost", port: 6379 };
const options = defaultOptions;
const cache = [];
for (const i of Array(5).keys()) {
cache[i] = new QuidolRedisCache({type: "sentinel" , redisSentinelOptions});
}
expect(ioredis).toHaveBeenCalledTimes(1);
});
// Cluster
test("should accept cluster connection options", () => {
const redisClusterOptions = [{ host: "localhost", port: 6379 }];
const options = {};
const cache = new QuidolRedisCache({type: "cluster", redisClusterOptions, options});
expect(ioredis.Cluster).toHaveBeenCalledWith(redisClusterOptions, options);
});
test("should default cluster connection options", () => {
const redisClusterOptions = [{ host: "localhost", port: 6379 }];
const options = defaultOptions;
const cache = new QuidolRedisCache({type: "cluster", redisClusterOptions });
expect(ioredis.Cluster).toHaveBeenCalledWith(redisClusterOptions, options);
});
test("should pool cluster connection", () => {
const redisClusterOptions = [{ host: "localhost", port: 6379 }];
const options = defaultOptions;
const cache = [];
for (const i of Array(1).keys()) {
cache[i] = new QuidolRedisCache({type: "cluster" , redisClusterOptions});
}
expect(ioredis.Cluster).toHaveBeenCalledTimes(1);
});