Skip to content

Commit

Permalink
fixed testing (valkey-io#1522)
Browse files Browse the repository at this point in the history
* fixed testing

* Update test comments for SET

Signed-off-by: Andrew Carbonetto <andrew.carbonetto@improving.com>

---------

Signed-off-by: Andrew Carbonetto <andrew.carbonetto@improving.com>
Co-authored-by: Andrew Carbonetto <andrew.carbonetto@improving.com>
  • Loading branch information
2 people authored and cyip10 committed Jun 24, 2024
1 parent 245ba70 commit 52c02ec
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 50 deletions.
2 changes: 1 addition & 1 deletion node/tests/RedisClusterClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Context = {
client: RedisClusterClient;
};

const TIMEOUT = 10000;
const TIMEOUT = 50000;

describe("RedisClusterClient", () => {
let testsFailed = 0;
Expand Down
114 changes: 65 additions & 49 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2499,7 +2499,7 @@ export function runBaseTests<Context>(config: {
const setWithUnixSec = await client.set(key, value, {
expiry: {
type: "unixSeconds",
count: 1,
count: Math.floor(Date.now() / 1000) + 1,
},
});
expect(setWithUnixSec).toEqual("OK");
Expand All @@ -2513,20 +2513,21 @@ export function runBaseTests<Context>(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);
Expand All @@ -2535,19 +2536,23 @@ export function runBaseTests<Context>(config: {
async function setWithOnlyIfExistOptions(client: BaseClient) {
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",
});
expect(setExistingKeyRes).toEqual("OK");
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);
}
Expand Down Expand Up @@ -2601,70 +2606,81 @@ export function runBaseTests<Context>(config: {
const key = uuidv4();
const value = uuidv4();

// set with multiple options:
// * only apply SET if the key already exists
// * expires after 1 second
// * returns the old value
const setResWithAllOptions = await client.set(key, value, {
expiry: {
type: "unixSeconds",
count: 1,
count: Math.floor(Date.now() / 1000) + 1,
},
conditionalSet: "onlyIfExists",
returnOldValue: true,
});
// key does not exist, so old value should be null
expect(setResWithAllOptions).toEqual(null);
// key should have been set
const getResWithAllOptions = await client.get(key);
expect(getResWithAllOptions).toEqual(value);

// wait for the key to expire
setTimeout(() => {}, 1000);
// key should have expired
const gettResWithAllOptions = await client.get(key);
expect(gettResWithAllOptions).toEqual(null);
// key does not exist, so SET should not have applied
expect(await client.get(key)).toEqual(null);
}

async function testSetWithAllCombination(client: BaseClient) {
const key = uuidv4();
const value = uuidv4();
const count = 1;
const count = 2;
const expiryCombination = [
"keepExisting",
{ type: "seconds", count },
{ type: "milliseconds", count },
{ type: "unixSeconds", count },
{ type: "unixMilliseconds", count },
undefined,
];
const conditionalSetCombination = [
"onlyIfExists",
"onlyIfDoesNotExist",
undefined,
{ type: "milliseconds", count },
"keepExisting",
];
const returnOldValueCombination = [true, false, undefined];
let exist = false;

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();
}
const setRes = await client.set(key, value, {
expiry: expiryVal as
| "keepExisting"
| {
type:
| "seconds"
| "milliseconds"
| "unixSeconds"
| "unixMilliseconds";
count: number;
},
conditionalSet: "onlyIfDoesNotExist",
});

if (exist == false) {
expect(setRes).toEqual("OK");
exist = true;
} else {
expect(setRes).toEqual(null);
}

const getRes = await client.get(key);
expect(getRes).toEqual(value);
}

for (const expiryVal of expiryCombination) {
const setRes = await client.set(key, value, {
expiry: expiryVal as
| "keepExisting"
| {
type:
| "seconds"
| "milliseconds"
| "unixSeconds"
| "unixMilliseconds";
count: number;
},

conditionalSet: "onlyIfExists",
returnOldValue: true,
});

expect(setRes).toBeDefined();
}
}

Expand Down

0 comments on commit 52c02ec

Please sign in to comment.