Skip to content

Commit

Permalink
Node: add test teardown (valkey-io#1419)
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamazon authored May 16, 2024
1 parent 65f9635 commit ba2ed46
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 80 deletions.
53 changes: 13 additions & 40 deletions node/tests/RedisClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@ import {
} from "@jest/globals";
import { BufferReader, BufferWriter } from "protobufjs";
import { v4 as uuidv4 } from "uuid";
import {
BaseClientConfiguration,
ClosingError,
ProtocolVersion,
RedisClient,
Transaction,
} from "..";
import { ProtocolVersion, RedisClient, Transaction } from "..";
import { redis_request } from "../src/ProtobufMessage";
import { runBaseTests } from "./SharedTests";
import {
RedisCluster,
flushAndCloseClient,
getClientConfigurationOption,
parseCommandLineArgs,
parseEndpoints,
transactionTest,
Expand Down Expand Up @@ -51,20 +47,7 @@ describe("RedisClient", () => {
}, 20000);

afterEach(async () => {
// some tests don't initialize a client
if (client == undefined) {
return;
}

try {
await client.customCommand(["FLUSHALL"]);
} catch (e) {
expect((e as ClosingError).message).toMatch(
"Unable to execute requests; the client is closed. Please create a new client.",
);
}

client.close();
await flushAndCloseClient(false, cluster.getAddresses(), client);
});

afterAll(async () => {
Expand All @@ -73,19 +56,6 @@ describe("RedisClient", () => {
}
}, TIMEOUT);

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

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 @@ -135,7 +105,7 @@ describe("RedisClient", () => {
"info without parameters",
async (protocol) => {
client = await RedisClient.createClient(
getOptions(cluster.getAddresses(), protocol),
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const result = await client.info();
expect(result).toEqual(expect.stringContaining("# Server"));
Expand All @@ -150,7 +120,7 @@ describe("RedisClient", () => {
"simple select test",
async (protocol) => {
client = await RedisClient.createClient(
getOptions(cluster.getAddresses(), protocol),
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
let selectResult = await client.select(0);
expect(selectResult).toEqual("OK");
Expand All @@ -174,7 +144,7 @@ describe("RedisClient", () => {
`can send transactions_%p`,
async (protocol) => {
client = await RedisClient.createClient(
getOptions(cluster.getAddresses(), protocol),
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const transaction = new Transaction();
const expectedRes = await transactionTest(transaction);
Expand All @@ -189,10 +159,10 @@ describe("RedisClient", () => {
"can return null on WATCH transaction failures",
async (protocol) => {
const client1 = await RedisClient.createClient(
getOptions(cluster.getAddresses(), protocol),
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const client2 = await RedisClient.createClient(
getOptions(cluster.getAddresses(), protocol),
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const transaction = new Transaction();
transaction.get("key");
Expand All @@ -212,7 +182,10 @@ describe("RedisClient", () => {

runBaseTests<Context>({
init: async (protocol, clientName?) => {
const options = getOptions(cluster.getAddresses(), protocol);
const options = getClientConfigurationOption(
cluster.getAddresses(),
protocol,
);
options.protocol = protocol;
options.clientName = clientName;
testsFailed += 1;
Expand Down
55 changes: 16 additions & 39 deletions node/tests/RedisClusterClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import {
import { v4 as uuidv4 } from "uuid";

import {
BaseClientConfiguration,
ClosingError,
ClusterTransaction,
InfoOptions,
ProtocolVersion,
Expand All @@ -23,6 +21,8 @@ import {
import { runBaseTests } from "./SharedTests";
import {
RedisCluster,
flushAndCloseClient,
getClientConfigurationOption,
getFirstResult,
parseCommandLineArgs,
parseEndpoints,
Expand Down Expand Up @@ -50,20 +50,7 @@ describe("RedisClusterClient", () => {
}, 20000);

afterEach(async () => {
// some tests don't initialize a client
if (client == undefined) {
return;
}

try {
await client.customCommand(["FLUSHALL"]);
} catch (e) {
expect((e as ClosingError).message).toMatch(
"Unable to execute requests; the client is closed. Please create a new client.",
);
}

client.close();
await flushAndCloseClient(true, cluster.getAddresses(), client);
});

afterAll(async () => {
Expand All @@ -72,22 +59,12 @@ describe("RedisClusterClient", () => {
}
});

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

runBaseTests<Context>({
init: async (protocol, clientName?) => {
const options = getOptions(cluster.getAddresses(), protocol);
const options = getClientConfigurationOption(
cluster.getAddresses(),
protocol,
);
options.protocol = protocol;
options.clientName = clientName;
testsFailed += 1;
Expand All @@ -111,7 +88,7 @@ describe("RedisClusterClient", () => {
`info with server and replication_%p`,
async (protocol) => {
client = await RedisClusterClient.createClient(
getOptions(cluster.getAddresses(), protocol),
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const info_server = getFirstResult(
await client.info([InfoOptions.Server]),
Expand Down Expand Up @@ -142,7 +119,7 @@ describe("RedisClusterClient", () => {
`info with server and randomNode route_%p`,
async (protocol) => {
client = await RedisClusterClient.createClient(
getOptions(cluster.getAddresses(), protocol),
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const result = await client.info(
[InfoOptions.Server],
Expand All @@ -169,7 +146,7 @@ describe("RedisClusterClient", () => {
};

client = await RedisClusterClient.createClient(
getOptions(cluster.getAddresses(), protocol),
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const result = cleanResult(
(await client.customCommand(
Expand Down Expand Up @@ -214,7 +191,7 @@ describe("RedisClusterClient", () => {
`fail routing by address if no port is provided_%p`,
async (protocol) => {
client = await RedisClusterClient.createClient(
getOptions(cluster.getAddresses(), protocol),
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
expect(() =>
client.info(undefined, {
Expand All @@ -230,7 +207,7 @@ describe("RedisClusterClient", () => {
`config get and config set transactions test_%p`,
async (protocol) => {
client = await RedisClusterClient.createClient(
getOptions(cluster.getAddresses(), protocol),
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const transaction = new ClusterTransaction();
transaction.configSet({ timeout: "1000" });
Expand All @@ -245,7 +222,7 @@ describe("RedisClusterClient", () => {
`can send transactions_%p`,
async (protocol) => {
client = await RedisClusterClient.createClient(
getOptions(cluster.getAddresses(), protocol),
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const transaction = new ClusterTransaction();
const expectedRes = await transactionTest(transaction);
Expand All @@ -259,10 +236,10 @@ describe("RedisClusterClient", () => {
`can return null on WATCH transaction failures_%p`,
async (protocol) => {
const client1 = await RedisClusterClient.createClient(
getOptions(cluster.getAddresses(), protocol),
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const client2 = await RedisClusterClient.createClient(
getOptions(cluster.getAddresses(), protocol),
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const transaction = new ClusterTransaction();
transaction.get("key");
Expand All @@ -285,7 +262,7 @@ describe("RedisClusterClient", () => {
`echo with all nodes routing_%p`,
async (protocol) => {
client = await RedisClusterClient.createClient(
getOptions(cluster.getAddresses(), protocol),
getClientConfigurationOption(cluster.getAddresses(), protocol),
);
const message = uuidv4();
const echoDict = await client.echo(message, "allNodes");
Expand Down
57 changes: 56 additions & 1 deletion node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ import { beforeAll, expect } from "@jest/globals";
import { exec } from "child_process";
import parseArgs from "minimist";
import { v4 as uuidv4 } from "uuid";
import { ClusterTransaction, Logger, ReturnType, Transaction } from "..";
import {
BaseClient,
BaseClientConfiguration,
ClusterTransaction,
Logger,
ProtocolVersion,
RedisClient,
RedisClusterClient,
ReturnType,
Transaction,
} from "..";
import { checkIfServerVersionLessThan } from "./SharedTests";

beforeAll(() => {
Expand Down Expand Up @@ -117,6 +127,51 @@ export function parseCommandLineArgs() {
return parseArgs(process.argv.slice(2));
}

export async function testTeardown(
cluster_mode: boolean,
option: BaseClientConfiguration,
) {
const client = cluster_mode
? await RedisClusterClient.createClient(option)
: await RedisClient.createClient(option);

await client.customCommand(["FLUSHALL"]);
client.close();
}

export const getClientConfigurationOption = (
addresses: [string, number][],
protocol: ProtocolVersion,
timeout?: number,
): BaseClientConfiguration => {
return {
addresses: addresses.map(([host, port]) => ({
host,
port,
})),
protocol,
...(timeout && { requestTimeout: timeout }),
};
};

export async function flushAndCloseClient(
cluster_mode: boolean,
addresses: [string, number][],
client?: BaseClient,
) {
await testTeardown(
cluster_mode,
getClientConfigurationOption(addresses, ProtocolVersion.RESP3, 2000),
);

// some tests don't initialize a client
if (client == undefined) {
return;
}

client.close();
}

/**
* Compare two maps by converting them to JSON strings and checking for equality, including property order.
*
Expand Down

0 comments on commit ba2ed46

Please sign in to comment.