Skip to content

Commit

Permalink
test: run tests against a Redis cluster
Browse files Browse the repository at this point in the history
Related:

- #3
- #10
  • Loading branch information
darrachequesne committed Feb 13, 2024
1 parent 93e2819 commit 65d0539
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 60 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ jobs:
--health-retries 5
ports:
- 6379:6379

redis-cluster:
image: grokzen/redis-cluster:7.0.10
options: >-
--health-cmd "redis-cli -p 7005 ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- "7000-7005:7000-7005"
steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand Down
10 changes: 10 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
redis:
image: redis:5
ports:
- "6379:6379"

redis-cluster:
image: grokzen/redis-cluster:7.0.10
ports:
- "7000-7005:7000-7005"
5 changes: 0 additions & 5 deletions docker-compose.yml

This file was deleted.

98 changes: 49 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"format:check": "prettier --parser typescript --check lib/**/*.ts test/**/*.ts",
"format:fix": "prettier --parser typescript --write lib/**/*.ts test/**/*.ts",
"prepack": "npm run compile",
"test": "npm run format:check && npm run compile && nyc mocha --bail --require ts-node/register test/**/*.ts"
"test": "npm run format:check && npm run compile && npm run test:redis-standalone && npm run test:redis-cluster",
"test:redis-standalone": "nyc mocha --require ts-node/register test/**/*.ts",
"test:redis-cluster": "REDIS_CLUSTER=1 mocha --require ts-node/register test/**/*.ts"
},
"dependencies": {
"@msgpack/msgpack": "~2.8.0",
Expand Down
49 changes: 45 additions & 4 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Server } from "socket.io";
import { Socket as ServerSocket } from "socket.io/dist/socket";
import { io as ioc, Socket as ClientSocket } from "socket.io-client";
import { createClient } from "redis";
import { createClient, createCluster } from "redis";
import { createServer } from "http";
import { createAdapter } from "../lib";
import { AddressInfo } from "net";
Expand Down Expand Up @@ -31,6 +31,49 @@ interface TestContext {
cleanup: () => void;
}

if (process.env.REDIS_CLUSTER === "1") {
console.log("[INFO] testing in cluster mode");
} else {
console.log("[INFO] testing in standalone mode");
}

async function initRedisClient() {
if (process.env.REDIS_CLUSTER === "1") {
const redisClient = createCluster({
rootNodes: [
{
url: "redis://localhost:7000",
},
{
url: "redis://localhost:7001",
},
{
url: "redis://localhost:7002",
},
{
url: "redis://localhost:7003",
},
{
url: "redis://localhost:7004",
},
{
url: "redis://localhost:7005",
},
],
});

await redisClient.connect();

return redisClient;
} else {
const redisClient = createClient();

await redisClient.connect();

return redisClient;
}
}

export function setup() {
const servers = [];
const serverSockets = [];
Expand All @@ -39,9 +82,7 @@ export function setup() {

return new Promise<TestContext>(async (resolve) => {
for (let i = 1; i <= NODES_COUNT; i++) {
const redisClient = createClient();

await redisClient.connect();
const redisClient = await initRedisClient();

const httpServer = createServer();
const io = new Server(httpServer, {
Expand Down

0 comments on commit 65d0539

Please sign in to comment.