-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8832b19
commit 07c3942
Showing
6 changed files
with
161 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { keygen, newHttpClient, randomID } from "../test-utils"; | ||
|
||
import { afterAll, describe, expect, test } from "bun:test"; | ||
import { XAddCommand } from "./xadd"; | ||
import { XLenCommand } from "./xlen"; | ||
import { XTrimCommand } from "./xtrim"; | ||
|
||
const client = newHttpClient(); | ||
|
||
const { newKey, cleanup } = keygen(); | ||
afterAll(cleanup); | ||
|
||
describe("XLEN", () => { | ||
test( | ||
"should approximately trim stream to 300 items", | ||
async () => { | ||
const key = newKey(); | ||
|
||
const promises = []; | ||
for (let i = 1; i <= 10000; i++) { | ||
promises.push( | ||
new XAddCommand([key, "*", { [randomID()]: randomID() }]).exec(client) | ||
); | ||
} | ||
await Promise.all(promises); | ||
|
||
await new XTrimCommand([ | ||
key, | ||
{ strategy: "MAXLEN", threshold: 300, exactness: "~" }, | ||
]).exec(client); | ||
|
||
const len = await new XLenCommand([key]).exec(client); | ||
|
||
expect(len).toBeGreaterThanOrEqual(290); | ||
expect(len).toBeLessThanOrEqual(310); | ||
}, | ||
{ timeout: 1000 * 60 } | ||
); | ||
|
||
test("should trim with zero threshold and remove everything", async () => { | ||
const key = newKey(); | ||
|
||
const promises = []; | ||
for (let i = 1; i <= 50; i++) { | ||
promises.push( | ||
new XAddCommand([key, "*", { [randomID()]: randomID() }]).exec(client) | ||
); | ||
} | ||
await Promise.all(promises); | ||
|
||
await new XTrimCommand([ | ||
key, | ||
{ strategy: "MAXLEN", threshold: 0, exactness: "=" }, | ||
]).exec(client); | ||
|
||
const len = await new XLenCommand([key]).exec(client); | ||
expect(len).toBeLessThanOrEqual(1); | ||
}); | ||
|
||
test( | ||
"should trim with MINID and a limit and only remove 10 items that satisfies MINID", | ||
async () => { | ||
const key = newKey(); | ||
const baseTimestamp = Date.now(); | ||
|
||
for (let i = 0; i < 100; i++) { | ||
const id = `${baseTimestamp}-${i}`; | ||
await new XAddCommand([key, id, { data: `value${i}` }]).exec(client); | ||
} | ||
|
||
const midRangeId = `${baseTimestamp}-50`; | ||
|
||
await new XTrimCommand([ | ||
key, | ||
{ strategy: "MINID", threshold: midRangeId, limit: 10 }, | ||
]).exec(client); | ||
|
||
const len = await new XLenCommand([key]).exec(client); | ||
expect(len).toBeLessThanOrEqual(100); | ||
}, | ||
{ timeout: 20000 } | ||
); | ||
|
||
test( | ||
"should trim with MINID and a without limit and delete half of the elements", | ||
async () => { | ||
const key = newKey(); | ||
const baseTimestamp = Date.now(); | ||
|
||
for (let i = 0; i < 100; i++) { | ||
const id = `${baseTimestamp}-${i}`; | ||
await new XAddCommand([key, id, { data: `value${i}` }]).exec(client); | ||
} | ||
|
||
const midRangeId = `${baseTimestamp}-50`; | ||
|
||
await new XTrimCommand([ | ||
key, | ||
{ strategy: "MINID", threshold: midRangeId }, | ||
]).exec(client); | ||
|
||
const len = await new XLenCommand([key]).exec(client); | ||
expect(len).toBeLessThanOrEqual(50); | ||
}, | ||
{ timeout: 20000 } | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Command, CommandOptions } from "./command"; | ||
|
||
/** | ||
* @see https://redis.io/commands/xtrim | ||
*/ | ||
|
||
type XTrimOptions = { | ||
strategy: "MAXLEN" | "MINID"; | ||
exactness?: "~" | "="; | ||
threshold: number | string; | ||
limit?: number; | ||
}; | ||
|
||
export class XTrimCommand extends Command<number, number> { | ||
constructor( | ||
[key, options]: [key: string, options: XTrimOptions], | ||
opts?: CommandOptions<number, number> | ||
) { | ||
const { limit, strategy, threshold, exactness = "~" } = options; | ||
|
||
super( | ||
[ | ||
"XTRIM", | ||
key, | ||
strategy, | ||
exactness, | ||
threshold, | ||
...(limit ? ["LIMIT", limit] : []), | ||
], | ||
opts | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters