From 5d0ff2fa0c722b25e9e87de5e455bd2b4bc4993e Mon Sep 17 00:00:00 2001 From: avifenesh Date: Tue, 4 Jun 2024 08:03:06 +0000 Subject: [PATCH] fixed testing --- node/tests/RedisClusterClient.test.ts | 2 +- node/tests/SharedTests.ts | 107 ++++++++++++++++---------- 2 files changed, 67 insertions(+), 42 deletions(-) diff --git a/node/tests/RedisClusterClient.test.ts b/node/tests/RedisClusterClient.test.ts index 2d39839945..4ab7c49e1c 100644 --- a/node/tests/RedisClusterClient.test.ts +++ b/node/tests/RedisClusterClient.test.ts @@ -32,7 +32,7 @@ type Context = { client: RedisClusterClient; }; -const TIMEOUT = 10000; +const TIMEOUT = 50000; describe("RedisClusterClient", () => { let testsFailed = 0; diff --git a/node/tests/SharedTests.ts b/node/tests/SharedTests.ts index ec80c31a6d..8361bf8427 100644 --- a/node/tests/SharedTests.ts +++ b/node/tests/SharedTests.ts @@ -2473,7 +2473,7 @@ export function runBaseTests(config: { // Set command tests - async function setWithExpiryOptions(client: BaseClient) { + async function setWithExpiryOptions(client: BaseClient): Promise { const key = uuidv4(); const value = uuidv4(); const setResWithExpirySetMilli = await client.set(key, value, { @@ -2499,7 +2499,7 @@ export function runBaseTests(config: { const setWithUnixSec = await client.set(key, value, { expiry: { type: "unixSeconds", - count: 1, + count: Math.floor(Date.now() / 1000) + 1, }, }); expect(setWithUnixSec).toEqual("OK"); @@ -2513,28 +2513,37 @@ export function runBaseTests(config: { const getResWithExpiryKeep = await client.get(key); expect(getResWithExpiryKeep).toEqual(value); // wait for the key to expire base on the previous set - setTimeout(() => {}, 1000); + let sleep = new Promise((resolve) => setTimeout(resolve, 1000)); + await sleep; const getResExpire = await client.get(key); // key should have expired expect(getResExpire).toEqual(null); - const setResWithExpiryWithUmilli = await client.set(key, value, { expiry: { type: "unixMilliseconds", - count: 2, + count: Date.now() + 1000, }, }); expect(setResWithExpiryWithUmilli).toEqual("OK"); // wait for the key to expire - setTimeout(() => {}, 3); + sleep = new Promise((resolve) => setTimeout(resolve, 1001)); + await sleep; const getResWithExpiryWithUmilli = await client.get(key); // key should have expired expect(getResWithExpiryWithUmilli).toEqual(null); + + return true; } - async function setWithOnlyIfExistOptions(client: BaseClient) { + async function setWithOnlyIfExistOptions( + client: BaseClient, + ): Promise { const key = uuidv4(); const value = uuidv4(); + const setKey = await client.set(key, value); + expect(setKey).toEqual("OK"); + const getRes = await client.get(key); + expect(getRes).toEqual(value); const setExistingKeyRes = await client.set(key, value, { conditionalSet: "onlyIfExists", }); @@ -2542,17 +2551,21 @@ export function runBaseTests(config: { const getExistingKeyRes = await client.get(key); expect(getExistingKeyRes).toEqual(value); - const notExistingKeyRes = await client.set(key, value + "1", { + const notExistingKeyRes = await client.set(key + 1, value, { conditionalSet: "onlyIfExists", }); // key does not exist, so it should not be set expect(notExistingKeyRes).toEqual(null); - const getNotExistingKey = await client.get(key); + const getNotExistingKey = await client.get(key + 1); // key should not have been set expect(getNotExistingKey).toEqual(null); + + return true; } - async function setWithOnlyIfNotExistOptions(client: BaseClient) { + async function setWithOnlyIfNotExistOptions( + client: BaseClient, + ): Promise { const key = uuidv4(); const value = uuidv4(); const notExistingKeyRes = await client.set(key, value, { @@ -2572,9 +2585,11 @@ export function runBaseTests(config: { const getExistingKey = await client.get(key); // key should not have been set expect(getExistingKey).toEqual(value); + + return true; } - async function setWithGetOldOptions(client: BaseClient) { + async function setWithGetOldOptions(client: BaseClient): Promise { const key = uuidv4(); const value = uuidv4(); @@ -2595,18 +2610,20 @@ export function runBaseTests(config: { // key should have been set const getResGetExistOld = await client.get(key); expect(getResGetExistOld).toEqual(value); + + return true; } - async function setWithAllOptions(client: BaseClient) { + async function setWithAllOptions(client: BaseClient): Promise { const key = uuidv4(); const value = uuidv4(); const setResWithAllOptions = await client.set(key, value, { expiry: { type: "unixSeconds", - count: 1, + count: Math.floor(Date.now() / 1000) + 1, }, - conditionalSet: "onlyIfExists", + conditionalSet: "onlyIfDoesNotExist", returnOldValue: true, }); // key does not exist, so old value should be null @@ -2616,13 +2633,18 @@ export function runBaseTests(config: { expect(getResWithAllOptions).toEqual(value); // wait for the key to expire - setTimeout(() => {}, 1000); + const sleep = new Promise((resolve) => setTimeout(resolve, 1001)); + await sleep; // key should have expired const gettResWithAllOptions = await client.get(key); expect(gettResWithAllOptions).toEqual(null); + + return true; } - async function testSetWithAllCombination(client: BaseClient) { + async function testSetWithAllCombination( + client: BaseClient, + ): Promise { const key = uuidv4(); const value = uuidv4(); const count = 1; @@ -2644,40 +2666,43 @@ export function runBaseTests(config: { for (const expiryVal of expiryCombination) { for (const conditionalSetVal of conditionalSetCombination) { for (const returnOldValueVal of returnOldValueCombination) { - const setRes = await client.set(key, value, { - expiry: expiryVal as - | "keepExisting" - | { - type: - | "seconds" - | "milliseconds" - | "unixSeconds" - | "unixMilliseconds"; - count: number; - } - | undefined, - conditionalSet: conditionalSetVal as - | "onlyIfExists" - | "onlyIfDoesNotExist" - | undefined, - returnOldValue: returnOldValueVal, - }); - expect(setRes).not.toBeNull(); + expect(async () => { + await client.set(key, value, { + expiry: expiryVal as + | "keepExisting" + | { + type: + | "seconds" + | "milliseconds" + | "unixSeconds" + | "unixMilliseconds"; + count: number; + } + | undefined, + conditionalSet: conditionalSetVal as + | "onlyIfExists" + | "onlyIfDoesNotExist" + | undefined, + returnOldValue: returnOldValueVal, + }); + }).not.toThrowError(); } } } + + return true; } it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])( "Set commands with options test_%p", async (protocol) => { await runTest(async (client: BaseClient) => { - await setWithExpiryOptions(client); - await setWithOnlyIfExistOptions(client); - await setWithOnlyIfNotExistOptions(client); - await setWithGetOldOptions(client); - await setWithAllOptions(client); - await testSetWithAllCombination(client); + expect(await setWithExpiryOptions(client)).toBeTruthy(); + expect(await setWithOnlyIfExistOptions(client)).toBeTruthy(); + expect(await setWithOnlyIfNotExistOptions(client)).toBeTruthy(); + expect(await setWithGetOldOptions(client)).toBeTruthy(); + expect(await setWithAllOptions(client)).toBeTruthy(); + expect(await testSetWithAllCombination(client)).toBeTruthy(); }, protocol); }, config.timeout,