Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 98 additions & 1 deletion pkg/commands/expire.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { keygen, newHttpClient, randomID } from "../test-utils";

import { afterAll, expect, test } from "bun:test";
import { afterAll, describe, expect, test } from "bun:test";
import { ExpireCommand } from "./expire";
import { GetCommand } from "./get";
import { SetCommand } from "./set";
Expand All @@ -20,3 +20,100 @@ test("expires a key correctly", async () => {

expect(res2).toEqual(null);
});

describe("NX", () => {
test("should set expiry only when the key has no expiry", async () => {
const key = newKey();
const value = randomID();
await new SetCommand([key, value]).exec(client);
const res = await new ExpireCommand([key, 1, "NX"]).exec(client);
expect(res).toEqual(1);
await new Promise((res) => setTimeout(res, 2000));
const res2 = await new GetCommand([key]).exec(client);

expect(res2).toEqual(null);
});

test("should not set expiry when the key has expiry", async () => {
const key = newKey();
const value = randomID();
await new SetCommand([key, value, { ex: 1000 }]).exec(client);
const res = await new ExpireCommand([key, 1, "NX"]).exec(client);
expect(res).toEqual(0);
});
});

describe("XX", () => {
test(
"should set expiry only when the key has an existing expiry",
async () => {
const key = newKey();
const value = randomID();
await new SetCommand([key, value, { ex: 1 }]).exec(client);
const res = await new ExpireCommand([key, 5, "XX"]).exec(client);
expect(res).toEqual(1);
await new Promise((res) => setTimeout(res, 6000));
const res2 = await new GetCommand([key]).exec(client);
expect(res2).toEqual(null);
},
{ timeout: 10000 }
);

test("should not set expiry when the key does not have an existing expiry", async () => {
const key = newKey();
const value = randomID();
await new SetCommand([key, value]).exec(client);
const res = await new ExpireCommand([key, 5, "XX"]).exec(client);
expect(res).toEqual(0);
});
});

describe("GT", () => {
test(
"should set expiry only when the new expiry is greater than current one",
async () => {
const key = newKey();
const value = randomID();
await new SetCommand([key, value, { ex: 1 }]).exec(client);
const res = await new ExpireCommand([key, 5, "GT"]).exec(client);
expect(res).toEqual(1);
await new Promise((res) => setTimeout(res, 6000));
const res2 = await new GetCommand([key]).exec(client);
expect(res2).toEqual(null);
},
{ timeout: 10000 }
);

test("should not set expiry when the new expiry is not greater than current one", async () => {
const key = newKey();
const value = randomID();
await new SetCommand([key, value, { ex: 10 }]).exec(client);
const res = await new ExpireCommand([key, 5, "GT"]).exec(client);
expect(res).toEqual(0);
});
});

describe("LT", () => {
test(
"should set expiry only when the new expiry is less than current one",
async () => {
const key = newKey();
const value = randomID();
await new SetCommand([key, value, { ex: 5 }]).exec(client);
const res = await new ExpireCommand([key, 3, "LT"]).exec(client);
expect(res).toEqual(1);
await new Promise((res) => setTimeout(res, 4000));
const res2 = await new GetCommand([key]).exec(client);
expect(res2).toEqual(null);
},
{ timeout: 10000 }
);

test("should not set expiry when the new expiry is not less than current one", async () => {
const key = newKey();
const value = randomID();
await new SetCommand([key, value, { ex: 10 }]).exec(client);
const res = await new ExpireCommand([key, 20, "LT"]).exec(client);
expect(res).toEqual(0);
});
});
9 changes: 5 additions & 4 deletions pkg/commands/expire.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Command, CommandOptions } from "./command";

/**
* @see https://redis.io/commands/expire
*/
type ExpireOptions = "NX" | "nx" | "XX" | "xx" | "GT" | "gt" | "LT" | "lt";
export class ExpireCommand extends Command<"0" | "1", 0 | 1> {
constructor(cmd: [key: string, seconds: number], opts?: CommandOptions<"0" | "1", 0 | 1>) {
constructor(
cmd: [key: string, seconds: number, option?: ExpireOptions],
opts?: CommandOptions<"0" | "1", 0 | 1>
) {
super(["expire", ...cmd], opts);
}
}