Skip to content

Commit

Permalink
Node: Fix tests. (valkey-io#2056)
Browse files Browse the repository at this point in the history
* Fix tests.

Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand authored Jul 31, 2024
1 parent b9e7cef commit 33e30e9
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 45 deletions.
6 changes: 4 additions & 2 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,11 @@ import {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
type PromiseFunction = (value?: any) => void;
type ErrorFunction = (error: RedisError) => void;
export type ReturnTypeMap = { [key: string]: ReturnType };
export type ReturnTypeRecord = { [key: string]: ReturnType };
export type ReturnTypeMap = Map<string, ReturnType>;
export type ReturnTypeAttribute = {
value: ReturnType;
attributes: ReturnTypeMap;
attributes: ReturnTypeRecord;
};
export enum ProtocolVersion {
/** Use RESP2 to communicate with the server nodes. */
Expand All @@ -205,6 +206,7 @@ export type ReturnType =
| bigint
| Buffer
| Set<ReturnType>
| ReturnTypeRecord
| ReturnTypeMap
| ReturnTypeAttribute
| ReturnType[];
Expand Down
22 changes: 19 additions & 3 deletions node/tests/PubSub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import {
TimeoutError,
} from "..";
import RedisCluster from "../../utils/TestUtils";
import { flushAndCloseClient } from "./TestUtilities";
import {
flushAndCloseClient,
parseCommandLineArgs,
parseEndpoints,
} from "./TestUtilities";

export type TGlideClient = GlideClient | GlideClusterClient;

Expand All @@ -41,8 +45,20 @@ describe("PubSub", () => {
let cmeCluster: RedisCluster;
let cmdCluster: RedisCluster;
beforeAll(async () => {
cmdCluster = await RedisCluster.createCluster(false, 1, 1);
cmeCluster = await RedisCluster.createCluster(true, 3, 1);
const standaloneAddresses =
parseCommandLineArgs()["standalone-endpoints"];
const clusterAddresses = parseCommandLineArgs()["cluster-endpoints"];
// Connect to cluster or create a new one based on the parsed addresses
cmdCluster = standaloneAddresses
? await RedisCluster.initFromExistingCluster(
parseEndpoints(standaloneAddresses),
)
: await RedisCluster.createCluster(false, 1, 1);
cmeCluster = clusterAddresses
? await RedisCluster.initFromExistingCluster(
parseEndpoints(clusterAddresses),
)
: await RedisCluster.createCluster(true, 3, 1);
}, 40000);
afterEach(async () => {
await flushAndCloseClient(false, cmdCluster.getAddresses());
Expand Down
3 changes: 1 addition & 2 deletions node/tests/RedisClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "@jest/globals";
import { BufferReader, BufferWriter } from "protobufjs";
import { v4 as uuidv4 } from "uuid";
import { GlideClient, ProtocolVersion, Transaction } from "..";
import { GlideClient, ProtocolVersion, Transaction, ListDirection } from "..";
import { RedisCluster } from "../../utils/TestUtils.js";
import { FlushMode } from "../build-ts/src/Commands";
import { command_request } from "../src/ProtobufMessage";
Expand All @@ -29,7 +29,6 @@ import {
transactionTest,
validateTransactionResponse,
} from "./TestUtilities";
import { ListDirection } from "..";

/* eslint-disable @typescript-eslint/no-var-requires */

Expand Down
55 changes: 28 additions & 27 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ import { SingleNodeRoute } from "../build-ts/src/GlideClusterClient";
import {
Client,
GetAndSetRandomValue,
checkSimple,
compareMaps,
getFirstResult,
intoString,
} from "./TestUtilities";

export type BaseClient = GlideClient | GlideClusterClient;
Expand Down Expand Up @@ -284,12 +282,21 @@ export function runBaseTests<Context>(config: {
/// we execute set and info so the commandstats will show `cmdstat_set::calls` greater than 1
/// after the configResetStat call we initiate an info command and the the commandstats won't contain `cmdstat_set`.
await client.set("foo", "bar");
const oldResult = await client.info([InfoOptions.Commandstats]);
const oldResultAsString = intoString(oldResult);
expect(oldResultAsString).toContain("cmdstat_set");
const oldResult =
client instanceof GlideClient
? await client.info([InfoOptions.Commandstats])
: Object.values(
await client.info([InfoOptions.Commandstats]),
).join();
expect(oldResult).toContain("cmdstat_set");
expect(await client.configResetStat()).toEqual("OK");

const result = await client.info([InfoOptions.Commandstats]);
const result =
client instanceof GlideClient
? await client.info([InfoOptions.Commandstats])
: Object.values(
await client.info([InfoOptions.Commandstats]),
).join();
expect(result).not.toContain("cmdstat_set");
}, protocol);
},
Expand Down Expand Up @@ -338,14 +345,14 @@ export function runBaseTests<Context>(config: {

expect(await client.msetnx(keyValueMap1)).toEqual(true);

checkSimple(
await client.mget([key1, key2, nonExistingKey]),
).toEqual([value, value, null]);
expect(await client.mget([key1, key2, nonExistingKey])).toEqual(
[value, value, null],
);

expect(await client.msetnx(keyValueMap2)).toEqual(false);

expect(await client.get(key3)).toEqual(null);
checkSimple(await client.get(key2)).toEqual(value);
expect(await client.get(key2)).toEqual(value);

// empty map and RequestError is thrown
const emptyMap = {};
Expand Down Expand Up @@ -1694,7 +1701,7 @@ export function runBaseTests<Context>(config: {
expect(await client.lpush(key2, lpushArgs2)).toEqual(2);

// Move from LEFT to LEFT with blocking
checkSimple(
expect(
await client.blmove(
key1,
key2,
Expand All @@ -1705,7 +1712,7 @@ export function runBaseTests<Context>(config: {
).toEqual("1");

// Move from LEFT to RIGHT with blocking
checkSimple(
expect(
await client.blmove(
key1,
key2,
Expand All @@ -1715,16 +1722,16 @@ export function runBaseTests<Context>(config: {
),
).toEqual("2");

checkSimple(await client.lrange(key2, 0, -1)).toEqual([
expect(await client.lrange(key2, 0, -1)).toEqual([
"1",
"3",
"4",
"2",
]);
checkSimple(await client.lrange(key1, 0, -1)).toEqual([]);
expect(await client.lrange(key1, 0, -1)).toEqual([]);

// Move from RIGHT to LEFT non-existing destination with blocking
checkSimple(
expect(
await client.blmove(
key2,
key1,
Expand All @@ -1734,15 +1741,15 @@ export function runBaseTests<Context>(config: {
),
).toEqual("2");

checkSimple(await client.lrange(key2, 0, -1)).toEqual([
expect(await client.lrange(key2, 0, -1)).toEqual([
"1",
"3",
"4",
]);
checkSimple(await client.lrange(key1, 0, -1)).toEqual(["2"]);
expect(await client.lrange(key1, 0, -1)).toEqual(["2"]);

// Move from RIGHT to RIGHT with blocking
checkSimple(
expect(
await client.blmove(
key2,
key1,
Expand All @@ -1752,14 +1759,8 @@ export function runBaseTests<Context>(config: {
),
).toEqual("4");

checkSimple(await client.lrange(key2, 0, -1)).toEqual([
"1",
"3",
]);
checkSimple(await client.lrange(key1, 0, -1)).toEqual([
"2",
"4",
]);
expect(await client.lrange(key2, 0, -1)).toEqual(["1", "3"]);
expect(await client.lrange(key1, 0, -1)).toEqual(["2", "4"]);

// Non-existing source key with blocking
expect(
Expand All @@ -1774,7 +1775,7 @@ export function runBaseTests<Context>(config: {

// Non-list source key with blocking
const key3 = "{key}-3" + uuidv4();
checkSimple(await client.set(key3, "value")).toEqual("OK");
expect(await client.set(key3, "value")).toEqual("OK");
await expect(
client.blmove(
key3,
Expand Down
33 changes: 22 additions & 11 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
ListDirection,
ProtocolVersion,
ReturnType,
ReturnTypeMap,
ScoreFilter,
SignedEncoding,
SortOrder,
Expand Down Expand Up @@ -374,12 +375,8 @@ export function checkFunctionListResponse(
string,
string | string[]
>;
const name = (
functionInfo["name"] as unknown as Buffer
).toString(); // not a string - suprise
const flags = (
functionInfo["flags"] as unknown as Buffer[]
).map((f) => f.toString());
const name = functionInfo["name"] as string;
const flags = functionInfo["flags"] as string[];
expect(functionInfo["description"]).toEqual(
functionDescriptions.get(name),
);
Expand Down Expand Up @@ -412,9 +409,23 @@ export function validateTransactionResponse(
for (let i = 0; i < expectedResponseData.length; i++) {
const [testName, expectedResponse] = expectedResponseData[i];

if (intoString(response?.[i]) != intoString(expectedResponse)) {
try {
expect(response?.[i]).toEqual(expectedResponse);
} catch (e) {
const expected =
expectedResponse instanceof Map
? JSON.stringify(Array.from(expectedResponse.entries()))
: JSON.stringify(expectedResponse);
const actual =
response?.[i] instanceof Map
? JSON.stringify(
Array.from(
(response?.[i] as ReturnTypeMap)?.entries(),
),
)
: JSON.stringify(response?.[i]);
failedChecks.push(
`${testName} failed, expected <${JSON.stringify(expectedResponse)}>, actual <${JSON.stringify(response?.[i])}>`,
`${testName} failed, expected <${expected}>, actual <${actual}>`,
);
}
}
Expand Down Expand Up @@ -634,7 +645,7 @@ export async function transactionTest(
baseTransaction.smismember(key7, ["bar", "foo", "baz"]);
responseData.push([
'smismember(key7, ["bar", "foo", "baz"])',
[true, true, false],
[true, false, false],
]);
}

Expand Down Expand Up @@ -904,11 +915,11 @@ export async function transactionTest(
baseTransaction.zrandmember(key21);
responseData.push(["zrandmember(key21)", "one"]);
baseTransaction.zrandmemberWithCount(key21, 1);
responseData.push(["zrandmemberWithCountWithScores(key21, 1)", "one"]);
responseData.push(["zrandmemberWithCount(key21, 1)", ["one"]]);
baseTransaction.zrandmemberWithCountWithScores(key21, 1);
responseData.push([
"zrandmemberWithCountWithScores(key21, 1)",
[Buffer.from("one"), 1.0],
[["one", 1.0]],
]);

if (gte(version, "6.2.0")) {
Expand Down

0 comments on commit 33e30e9

Please sign in to comment.