Skip to content

Commit

Permalink
Node: allow standalone tests to use cluster_manager.py script
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamazon committed Feb 28, 2024
1 parent 44ef9a9 commit fc7e952
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 32 deletions.
2 changes: 0 additions & 2 deletions node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@
"babel-jest": "^28.1.3",
"eslint": "^8.36.0",
"eslint-plugin-tsdoc": "^0.2.17",
"find-free-port": "^2.0.0",
"jest": "^28.1.3",
"protobufjs-cli": "^1.1.1",
"redis-server": "^1.2.2",
"replace": "^1.2.2",
"ts-jest": "^28.0.8",
"typescript": "^4.9.5",
Expand Down
43 changes: 19 additions & 24 deletions node/tests/RedisClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
it,
} from "@jest/globals";
import { BufferReader, BufferWriter } from "protobufjs";
import RedisServer from "redis-server";
import { v4 as uuidv4 } from "uuid";
import {
BaseClientConfiguration,
Expand All @@ -21,41 +20,32 @@ import {
} from "..";
import { redis_request } from "../src/ProtobufMessage";
import { runBaseTests } from "./SharedTests";
import { flushallOnPort, transactionTest } from "./TestUtilities";
import { RedisCluster, flushallOnPort, transactionTest } from "./TestUtilities";
/* eslint-disable @typescript-eslint/no-var-requires */
const FreePort = require("find-free-port");

type Context = {
client: RedisClient;
};

const PORT_NUMBER = 3000;
const TIMEOUT = 10000;

describe("RedisClient", () => {
let server: RedisServer;
let testsFailed = 0;
let cluster: RedisCluster;
let port: number;
beforeAll(async () => {
port = await FreePort(PORT_NUMBER).then(
([free_port]: number[]) => free_port
);
server = await new Promise((resolve, reject) => {
const server = new RedisServer(port);
server.open(async (err: Error | null) => {
if (err) {
reject(err);
}

resolve(server);
});
});
});
cluster = await RedisCluster.createCluster(false, 1, 1);
port = cluster.ports()[0];
}, 20000);

afterEach(async () => {
await flushallOnPort(port);
await flushallOnPort(cluster.ports()[0]);
});

afterAll(() => {
server.close();
afterAll(async () => {
if (testsFailed === 0) {
await cluster.close();
}
});

const getAddress = (port: number) => {
Expand Down Expand Up @@ -204,12 +194,17 @@ describe("RedisClient", () => {
const options = getOptions(port, protocol);
options.protocol = protocol;
options.clientName = clientName;
testsFailed += 1;
const client = await RedisClient.createClient(options);

return { client, context: { client } };
},
close: async (context: Context) => {
close: (context: Context, testSucceeded: boolean) => {
if (testSucceeded) {
testsFailed -= 1;
}

context.client.close();
},
timeout: TIMEOUT,
});
});
2 changes: 1 addition & 1 deletion node/tests/RedisClusterClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("RedisClusterClient", () => {
let testsFailed = 0;
let cluster: RedisCluster;
beforeAll(async () => {
cluster = await RedisCluster.createCluster(3, 0);
cluster = await RedisCluster.createCluster(true, 3, 0);
}, 20000);

afterEach(async () => {
Expand Down
7 changes: 6 additions & 1 deletion node/tests/RedisModules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ describe("RedisModules", () => {
arg.startsWith("--load-module=")
);
const loadModuleValues = loadModuleArgs.map((arg) => arg.split("=")[1]);
cluster = await RedisCluster.createCluster(3, 0, loadModuleValues);
cluster = await RedisCluster.createCluster(
true,
3,
0,
loadModuleValues
);
}, 20000);

afterEach(async () => {
Expand Down
2 changes: 1 addition & 1 deletion node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ export function runBaseTests<Context>(config: {
"positiveInfinity"
)
).toEqual(0);

expect(await client.set(key2, "foo")).toEqual("OK");
await expect(
client.zcount(key2, "negativeInfinity", "positiveInfinity")
Expand Down
11 changes: 8 additions & 3 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ export function transactionTest(
baseTransaction.zcount(key8, { bound: 2 }, "positiveInfinity");
args.push(2);
baseTransaction.zpopmin(key8);
args.push({"member2": 3.0});
args.push({ member2: 3.0 });
baseTransaction.zpopmax(key8);
args.push({"member3": 3.5});
args.push({ member3: 3.5 });
return args;
}

Expand Down Expand Up @@ -185,12 +185,17 @@ export class RedisCluster {
}

public static createCluster(
cluster_mode: boolean,
shardCount: number,
replicaCount: number,
loadModule?: string[]
): Promise<RedisCluster> {
return new Promise<RedisCluster>((resolve, reject) => {
let command = `python3 ../utils/cluster_manager.py start --cluster-mode -r ${replicaCount} -n ${shardCount}`;
let command = `python3 ../utils/cluster_manager.py start -r ${replicaCount} -n ${shardCount}`;

if (cluster_mode) {
command += " --cluster-mode";
}

if (loadModule) {
if (loadModule.length === 0) {
Expand Down

0 comments on commit fc7e952

Please sign in to comment.