Skip to content

Commit 65d0539

Browse files
test: run tests against a Redis cluster
Related: - #3 - #10
1 parent 93e2819 commit 65d0539

File tree

6 files changed

+116
-60
lines changed

6 files changed

+116
-60
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ jobs:
2929
--health-retries 5
3030
ports:
3131
- 6379:6379
32-
32+
redis-cluster:
33+
image: grokzen/redis-cluster:7.0.10
34+
options: >-
35+
--health-cmd "redis-cli -p 7005 ping"
36+
--health-interval 10s
37+
--health-timeout 5s
38+
--health-retries 5
39+
ports:
40+
- "7000-7005:7000-7005"
3341
steps:
3442
- name: Checkout repository
3543
uses: actions/checkout@v3

compose.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
redis:
3+
image: redis:5
4+
ports:
5+
- "6379:6379"
6+
7+
redis-cluster:
8+
image: grokzen/redis-cluster:7.0.10
9+
ports:
10+
- "7000-7005:7000-7005"

docker-compose.yml

Lines changed: 0 additions & 5 deletions
This file was deleted.

package-lock.json

Lines changed: 49 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"format:check": "prettier --parser typescript --check lib/**/*.ts test/**/*.ts",
1818
"format:fix": "prettier --parser typescript --write lib/**/*.ts test/**/*.ts",
1919
"prepack": "npm run compile",
20-
"test": "npm run format:check && npm run compile && nyc mocha --bail --require ts-node/register test/**/*.ts"
20+
"test": "npm run format:check && npm run compile && npm run test:redis-standalone && npm run test:redis-cluster",
21+
"test:redis-standalone": "nyc mocha --require ts-node/register test/**/*.ts",
22+
"test:redis-cluster": "REDIS_CLUSTER=1 mocha --require ts-node/register test/**/*.ts"
2123
},
2224
"dependencies": {
2325
"@msgpack/msgpack": "~2.8.0",

test/util.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Server } from "socket.io";
22
import { Socket as ServerSocket } from "socket.io/dist/socket";
33
import { io as ioc, Socket as ClientSocket } from "socket.io-client";
4-
import { createClient } from "redis";
4+
import { createClient, createCluster } from "redis";
55
import { createServer } from "http";
66
import { createAdapter } from "../lib";
77
import { AddressInfo } from "net";
@@ -31,6 +31,49 @@ interface TestContext {
3131
cleanup: () => void;
3232
}
3333

34+
if (process.env.REDIS_CLUSTER === "1") {
35+
console.log("[INFO] testing in cluster mode");
36+
} else {
37+
console.log("[INFO] testing in standalone mode");
38+
}
39+
40+
async function initRedisClient() {
41+
if (process.env.REDIS_CLUSTER === "1") {
42+
const redisClient = createCluster({
43+
rootNodes: [
44+
{
45+
url: "redis://localhost:7000",
46+
},
47+
{
48+
url: "redis://localhost:7001",
49+
},
50+
{
51+
url: "redis://localhost:7002",
52+
},
53+
{
54+
url: "redis://localhost:7003",
55+
},
56+
{
57+
url: "redis://localhost:7004",
58+
},
59+
{
60+
url: "redis://localhost:7005",
61+
},
62+
],
63+
});
64+
65+
await redisClient.connect();
66+
67+
return redisClient;
68+
} else {
69+
const redisClient = createClient();
70+
71+
await redisClient.connect();
72+
73+
return redisClient;
74+
}
75+
}
76+
3477
export function setup() {
3578
const servers = [];
3679
const serverSockets = [];
@@ -39,9 +82,7 @@ export function setup() {
3982

4083
return new Promise<TestContext>(async (resolve) => {
4184
for (let i = 1; i <= NODES_COUNT; i++) {
42-
const redisClient = createClient();
43-
44-
await redisClient.connect();
85+
const redisClient = await initRedisClient();
4586

4687
const httpServer = createServer();
4788
const io = new Server(httpServer, {

0 commit comments

Comments
 (0)