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 22, 2024
1 parent 44ef9a9 commit a2187a4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 48 deletions.
77 changes: 35 additions & 42 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,30 @@ 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 port: number;
let testsFailed = 0;
let cluster: RedisCluster;
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);
}, 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 All @@ -72,6 +60,25 @@ describe("RedisClient", () => {
};
};

runBaseTests<Context>({
init: async (protocol, clientName?) => {
const options = getOptions(cluster.ports()[0], protocol);
options.protocol = protocol;
options.clientName = clientName;
testsFailed += 1;
const client = await RedisClient.createClient(options);
return { client, context: { client } };
},
close: (context: Context, testSucceeded: boolean) => {
if (testSucceeded) {
testsFailed -= 1;
}

context.client.close();
},
timeout: TIMEOUT,
});

it("test protobuf encode/decode delimited", () => {
// This test is required in order to verify that the autogenerated protobuf
// files has been corrected and the encoding/decoding works as expected.
Expand Down Expand Up @@ -121,7 +128,7 @@ describe("RedisClient", () => {
"info without parameters",
async (protocol) => {
const client = await RedisClient.createClient(
getOptions(port, protocol)
getOptions(cluster.ports()[0], protocol)
);
const result = await client.info();
expect(result).toEqual(expect.stringContaining("# Server"));
Expand All @@ -137,7 +144,7 @@ describe("RedisClient", () => {
"simple select test",
async (protocol) => {
const client = await RedisClient.createClient(
getOptions(port, protocol)
getOptions(cluster.ports()[0], protocol)
);
let selectResult = await client.select(0);
expect(selectResult).toEqual("OK");
Expand All @@ -162,7 +169,7 @@ describe("RedisClient", () => {
`can send transactions_%p`,
async (protocol) => {
const client = await RedisClient.createClient(
getOptions(port, protocol)
getOptions(cluster.ports()[0], protocol)
);
const transaction = new Transaction();
const expectedRes = transactionTest(transaction);
Expand All @@ -178,10 +185,10 @@ describe("RedisClient", () => {
"can return null on WATCH transaction failures",
async (protocol) => {
const client1 = await RedisClient.createClient(
getOptions(port, protocol)
getOptions(cluster.ports()[0], protocol)
);
const client2 = await RedisClient.createClient(
getOptions(port, protocol)
getOptions(cluster.ports()[0], protocol)
);
const transaction = new Transaction();
transaction.get("key");
Expand All @@ -198,18 +205,4 @@ describe("RedisClient", () => {
client2.close();
}
);

runBaseTests<Context>({
init: async (protocol, clientName?) => {
const options = getOptions(port, protocol);
options.protocol = protocol;
options.clientName = clientName;
const client = await RedisClient.createClient(options);

return { client, context: { client } };
},
close: async (context: Context) => {
context.client.close();
},
});
});
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 a2187a4

Please sign in to comment.