Skip to content

Commit

Permalink
Parameterize Node tests for both RESP2 & RESP3. (#911)
Browse files Browse the repository at this point in the history
  • Loading branch information
nihohit authored Feb 11, 2024
1 parent 61d7f40 commit f01901a
Show file tree
Hide file tree
Showing 5 changed files with 315 additions and 282 deletions.
4 changes: 2 additions & 2 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export type BaseClientConfiguration = {
* Choose the Redis protocol to be used with the server.
* If not set, `RESP3` will be used.
*/
serverProtocol?: ProtocolVersion;
protocol?: ProtocolVersion;
/**
* Client name to be used for the client. Will be used with CLIENT SETNAME command during connection establishment.
*/
Expand Down Expand Up @@ -1087,7 +1087,7 @@ export class BaseClient {
username: options.credentials.username,
}
: undefined;
const protocol = options.serverProtocol as
const protocol = options.protocol as
| connection_request.ProtocolVersion
| undefined;
return {
Expand Down
119 changes: 72 additions & 47 deletions node/tests/RedisClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ describe("RedisClient", () => {
return [{ host: "localhost", port }];
};

const getOptions = (port: number): BaseClientConfiguration => {
const getOptions = (
port: number,
protocol: ProtocolVersion
): BaseClientConfiguration => {
return {
addresses: getAddress(port),
protocol,
};
};

Expand Down Expand Up @@ -113,39 +117,53 @@ describe("RedisClient", () => {
]);
});

it("info without parameters", async () => {
const client = await RedisClient.createClient(getOptions(port));
const result = await client.info();
expect(result).toEqual(expect.stringContaining("# Server"));
expect(result).toEqual(expect.stringContaining("# Replication"));
expect(result).toEqual(expect.not.stringContaining("# Latencystats"));
client.close();
});

it("simple select test", async () => {
const client = await RedisClient.createClient(getOptions(port));
let selectResult = await client.select(0);
expect(selectResult).toEqual("OK");

const key = uuidv4();
const value = uuidv4();
const result = await client.set(key, value);
expect(result).toEqual("OK");

selectResult = await client.select(1);
expect(selectResult).toEqual("OK");
expect(await client.get(key)).toEqual(null);
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"info without parameters",
async (protocol) => {
const client = await RedisClient.createClient(
getOptions(port, protocol)
);
const result = await client.info();
expect(result).toEqual(expect.stringContaining("# Server"));
expect(result).toEqual(expect.stringContaining("# Replication"));
expect(result).toEqual(
expect.not.stringContaining("# Latencystats")
);
client.close();
}
);

selectResult = await client.select(0);
expect(selectResult).toEqual("OK");
expect(await client.get(key)).toEqual(value);
client.close();
});
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"simple select test",
async (protocol) => {
const client = await RedisClient.createClient(
getOptions(port, protocol)
);
let selectResult = await client.select(0);
expect(selectResult).toEqual("OK");

const key = uuidv4();
const value = uuidv4();
const result = await client.set(key, value);
expect(result).toEqual("OK");

selectResult = await client.select(1);
expect(selectResult).toEqual("OK");
expect(await client.get(key)).toEqual(null);

selectResult = await client.select(0);
expect(selectResult).toEqual("OK");
expect(await client.get(key)).toEqual(value);
client.close();
}
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`can send transactions_%p`,
async () => {
const client = await RedisClient.createClient(getOptions(port));
async (protocol) => {
const client = await RedisClient.createClient(
getOptions(port, protocol)
);
const transaction = new Transaction();
const expectedRes = transactionTest(transaction);
transaction.select(0);
Expand All @@ -156,28 +174,35 @@ describe("RedisClient", () => {
}
);

it("can return null on WATCH transaction failures", async () => {
const client1 = await RedisClient.createClient(getOptions(port));
const client2 = await RedisClient.createClient(getOptions(port));
const transaction = new Transaction();
transaction.get("key");
const result1 = await client1.customCommand(["WATCH", "key"]);
expect(result1).toEqual("OK");
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"can return null on WATCH transaction failures",
async (protocol) => {
const client1 = await RedisClient.createClient(
getOptions(port, protocol)
);
const client2 = await RedisClient.createClient(
getOptions(port, protocol)
);
const transaction = new Transaction();
transaction.get("key");
const result1 = await client1.customCommand(["WATCH", "key"]);
expect(result1).toEqual("OK");

const result2 = await client2.set("key", "foo");
expect(result2).toEqual("OK");
const result2 = await client2.set("key", "foo");
expect(result2).toEqual("OK");

const result3 = await client1.exec(transaction);
expect(result3).toBeNull();
const result3 = await client1.exec(transaction);
expect(result3).toBeNull();

client1.close();
client2.close();
});
client1.close();
client2.close();
}
);

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

Expand Down
48 changes: 26 additions & 22 deletions node/tests/RedisClusterClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,23 @@ describe("RedisClusterClient", () => {
}
});

const getOptions = (ports: number[]): BaseClientConfiguration => {
const getOptions = (
ports: number[],
protocol: ProtocolVersion
): BaseClientConfiguration => {
return {
addresses: ports.map((port) => ({
host: "localhost",
port,
})),
protocol,
};
};

runBaseTests<Context>({
init: async (protocol, clientName?) => {
const options = getOptions(cluster.ports());
options.serverProtocol = protocol;
const options = getOptions(cluster.ports(), protocol);
options.protocol = protocol;
options.clientName = clientName;
testsFailed += 1;
const client = await RedisClusterClient.createClient(options);
Expand All @@ -81,11 +85,11 @@ describe("RedisClusterClient", () => {
timeout: TIMEOUT,
});

it(
"info with server and replication",
async () => {
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`info with server and replication_%p`,
async (protocol) => {
const client = await RedisClusterClient.createClient(
getOptions(cluster.ports())
getOptions(cluster.ports(), protocol)
);
const info_server = getFirstResult(
await client.info([InfoOptions.Server])
Expand Down Expand Up @@ -113,11 +117,11 @@ describe("RedisClusterClient", () => {
TIMEOUT
);

it(
"info with server and randomNode route",
async () => {
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`info with server and randomNode route_%p`,
async (protocol) => {
const client = await RedisClusterClient.createClient(
getOptions(cluster.ports())
getOptions(cluster.ports(), protocol)
);
const result = await client.info(
[InfoOptions.Server],
Expand All @@ -131,11 +135,11 @@ describe("RedisClusterClient", () => {
TIMEOUT
);

it(
"config get and config set transactions test",
async () => {
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`config get and config set transactions test_%p`,
async (protocol) => {
const client = await RedisClusterClient.createClient(
getOptions(cluster.ports())
getOptions(cluster.ports(), protocol)
);
const transaction = new ClusterTransaction();
transaction.configSet({ timeout: "1000" });
Expand All @@ -149,9 +153,9 @@ describe("RedisClusterClient", () => {

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`can send transactions_%p`,
async () => {
async (protocol) => {
const client = await RedisClusterClient.createClient(
getOptions(cluster.ports())
getOptions(cluster.ports(), protocol)
);
const transaction = new ClusterTransaction();
const expectedRes = transactionTest(transaction);
Expand All @@ -162,14 +166,14 @@ describe("RedisClusterClient", () => {
TIMEOUT
);

it(
"can return null on WATCH transaction failures",
async () => {
it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`can return null on WATCH transaction failures_%p`,
async (protocol) => {
const client1 = await RedisClusterClient.createClient(
getOptions(cluster.ports())
getOptions(cluster.ports(), protocol)
);
const client2 = await RedisClusterClient.createClient(
getOptions(cluster.ports())
getOptions(cluster.ports(), protocol)
);
const transaction = new ClusterTransaction();
transaction.get("key");
Expand Down
2 changes: 1 addition & 1 deletion node/tests/RedisModules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe("RedisModules", () => {
runBaseTests<Context>({
init: async (protocol, clientName) => {
const options = getOptions(cluster.ports());
options.serverProtocol = protocol;
options.protocol = protocol;
options.clientName = clientName;
testsFailed += 1;
const client = await RedisClusterClient.createClient(options);
Expand Down
Loading

0 comments on commit f01901a

Please sign in to comment.