Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store: make TTL optionally #437

Merged
merged 73 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
eeb387d
Update modules/store/records.json.
ynwd Sep 16, 2024
1955f88
Delete modules/store/records.json
ynwd Sep 16, 2024
8d35b86
Update modules/store/records.json.
ynwd Sep 16, 2024
983a45d
Delete modules/store/records.json
ynwd Sep 16, 2024
75918e5
Update modules/store/records.json
ynwd Sep 16, 2024
7ec7fe6
Delete modules/store/records.json
ynwd Sep 16, 2024
3f86923
Delete modules/store/coba.json
ynwd Sep 16, 2024
a3777aa
Delete modules/store/x.json
ynwd Sep 16, 2024
71560d8
Update modules/store/records.json
ynwd Sep 16, 2024
195476a
Delete modules/store/records.json
ynwd Sep 16, 2024
a3c12fb
Update modules/store/records.json
ynwd Sep 16, 2024
0a3713d
Delete modules/store/records.json
ynwd Sep 16, 2024
0fd0954
Update modules/store/records.json
ynwd Sep 16, 2024
903761e
Delete modules/store/records.json
ynwd Sep 16, 2024
eade44f
Create modules/store/records.json
ynwd Sep 16, 2024
5ca1ba6
Update modules/store/records.json
ynwd Sep 16, 2024
5c83a94
Update modules/store/records.json
ynwd Sep 16, 2024
c5f85c2
Update modules/store/records.json
ynwd Sep 16, 2024
7003789
Update modules/store/records.json
ynwd Sep 16, 2024
dc3c16d
Update modules/store/records.json
ynwd Sep 16, 2024
221037d
Update modules/store/records.json
ynwd Sep 16, 2024
bcfabee
Update modules/store/records.json
ynwd Sep 16, 2024
c1c1dd7
Update modules/store/records.json
ynwd Sep 16, 2024
b192819
Update modules/store/records.json
ynwd Sep 16, 2024
2182e8a
Update modules/store/records.json
ynwd Sep 16, 2024
cd5714a
Update modules/store/records.json
ynwd Sep 16, 2024
e874917
Update modules/store/records.json
ynwd Sep 16, 2024
50578b2
Update modules/store/records.json
ynwd Sep 16, 2024
e071e5c
Delete modules/store/records.json
ynwd Sep 16, 2024
d9bc977
Create modules/store/records.json
ynwd Sep 16, 2024
ac550ee
Delete modules/store/records.json
ynwd Sep 16, 2024
5588ab2
Create modules/store/records.json
ynwd Sep 16, 2024
8bb187d
Delete modules/store/records.json
ynwd Sep 16, 2024
5b0b5d7
Create modules/store/records.json
ynwd Sep 16, 2024
9e17a63
Delete modules/store/records.json
ynwd Sep 16, 2024
c9a7662
Create modules/store/records.json
ynwd Sep 16, 2024
ecf9d4d
Delete modules/store/records.json
ynwd Sep 16, 2024
69cf30c
Create modules/store/records.json
ynwd Sep 16, 2024
4912fcf
Delete modules/store/records.json
ynwd Sep 16, 2024
da81a34
Create modules/store/records.json
ynwd Sep 16, 2024
a054be5
Update modules/store/records.json
ynwd Sep 16, 2024
4b69d8d
Update modules/store/records.json
ynwd Sep 16, 2024
33274fa
Update modules/store/records.json
ynwd Sep 16, 2024
a8f56fa
Delete modules/store/records.json
ynwd Sep 16, 2024
af6bbcd
Create modules/store/records.json
ynwd Sep 16, 2024
94a32a9
Update modules/store/records.json
ynwd Sep 16, 2024
92acd26
Update modules/store/records.json
ynwd Sep 16, 2024
b6b6e0e
Delete modules/store/records.json
ynwd Sep 16, 2024
30f6230
Create modules/store/records.json
ynwd Sep 16, 2024
3f7c4d6
Delete modules/store/records.json
ynwd Sep 16, 2024
b08fe7d
Create modules/store/records.json
ynwd Sep 16, 2024
1666d86
Delete modules/store/records.json
ynwd Sep 16, 2024
f10779d
Create modules/store/records.json
ynwd Sep 16, 2024
805d08f
Delete modules/store/records.json
ynwd Sep 16, 2024
dfee0b9
Create modules/store/records.json
ynwd Sep 16, 2024
bda4895
Delete modules/store/records.json
ynwd Sep 16, 2024
9cecc01
Create modules/store/records.json
ynwd Sep 16, 2024
fb546ed
Delete modules/store/records.json
ynwd Sep 16, 2024
95c8ba6
Create modules/store/records.json
ynwd Sep 16, 2024
cf1d6f2
Delete modules/store/records.json
ynwd Sep 16, 2024
692f84f
Create modules/store/records.json
ynwd Sep 16, 2024
5f0fbc1
Delete modules/store/records.json
ynwd Sep 16, 2024
f231ede
Create modules/store/records.json
ynwd Sep 16, 2024
ee3e28c
Delete modules/store/records.json
ynwd Sep 16, 2024
5a9f2a5
Create modules/store/records.json
ynwd Sep 16, 2024
2a74d96
Delete modules/store/records.json
ynwd Sep 16, 2024
8f545fc
Create modules/store/records.json
ynwd Sep 16, 2024
ad506c3
Delete modules/store/records.json
ynwd Sep 16, 2024
d160174
Create modules/store/records.json
ynwd Sep 16, 2024
6393ba6
Delete modules/store/records.json
ynwd Sep 16, 2024
94a9f29
chore: update store to use options
ynwd Sep 16, 2024
891740b
Merge branch 'store' of github.com:fastrodev/fastro into store
ynwd Sep 16, 2024
df409ac
Merge branch 'main' into store
ynwd Sep 16, 2024
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
128 changes: 74 additions & 54 deletions core/map/map.test.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,103 @@
import { assert, assertEquals } from "../server/deps.ts";
import { assertEquals } from "../server/deps.ts";
import { Store } from "./map.ts";

Deno.test("ExpiringMap: should set and get a value", async () => {
const expiringMap = new Store<string, string>(1000); // 1 second TTL
expiringMap.set("key1", "value1");
assertEquals(await expiringMap.get("key1"), "value1");
Deno.test("Store: set and get value without expiry", async () => {
const store = new Store<string, number>();
store.set("key1", 100);
const value = await store.get("key1");
assertEquals(value, 100);
});

Deno.test("ExpiringMap: should return undefined for expired keys", async () => {
const expiringMap = new Store<string, string>(100);
expiringMap.set("key1", "value1");
await new Promise((resolve) => setTimeout(resolve, 200));
assertEquals(await expiringMap.get("key1"), undefined);
Deno.test("Store: set and get value with expiry", async () => {
const store = new Store<string, number>();
store.set("key2", 200, 1000); // Set with 1 second expiry
const value = await store.get("key2");
assertEquals(value, 200);

// Wait for 1.1 seconds to let it expire
await new Promise((resolve) => setTimeout(resolve, 1100));
const expiredValue = await store.get("key2");
assertEquals(expiredValue, undefined);
});

Deno.test("ExpiringMap: should check if a key exists", async () => {
const expiringMap = new Store<string, string>(100);
expiringMap.set("key1", "value1");
assert(expiringMap.has("key1"));
await new Promise((resolve) => setTimeout(resolve, 200));
assert(!expiringMap.has("key1"));
Deno.test("Store: has method returns true for existing key", async () => {
const store = new Store<string, number>();
store.set("key3", 300);
assertEquals(await store.has("key3"), true);
});

Deno.test("ExpiringMap: should delete a key", async () => {
const expiringMap = new Store<string, string>();
expiringMap.set("key1", "value1");
assert(expiringMap.delete("key1"));
assertEquals(await expiringMap.get("key1"), undefined);
Deno.test("Store: has method returns false for expired key", async () => {
const store = new Store<string, number>();
store.set("key4", 400, 500); // Set with 0.5 seconds expiry
await new Promise((resolve) => setTimeout(resolve, 600)); // Wait for it to expire
assertEquals(await store.has("key4"), false);
});

Deno.test("ExpiringMap: should clear all entries", () => {
const expiringMap = new Store<string, string>();
expiringMap.set("key1", "value1");
expiringMap.set("key2", "value2");
expiringMap.clear();
assertEquals(expiringMap.size(), 0);
Deno.test("Store: delete method removes key", async () => {
const store = new Store<string, number>();
store.set("key5", 500);
assertEquals(store.delete("key5"), true);
assertEquals(await store.has("key5"), false);
});

Deno.test("ExpiringMap: should get the size of the map", () => {
const expiringMap = new Store<string, string>();
expiringMap.set("key1", "value1");
expiringMap.set("key2", "value2");
assertEquals(expiringMap.size(), 2);
Deno.test("Store: clear method removes all keys", () => {
const store = new Store<string, number>();
store.set("key6", 600);
store.set("key7", 700);
store.clear();
assertEquals(store.size(), 0);
});

Deno.test("ExpiringMap: should iterate over valid entries using forEach", () => {
const expiringMap = new Store<string, string>(1000);
expiringMap.set("key1", "value1");
expiringMap.set("key2", "value2");
Deno.test("Store: size method returns correct count", () => {
const store = new Store<string, number>();
store.set("key8", 800);
store.set("key9", 900);
assertEquals(store.size(), 2);
});

const entries: [string, string][] = [];
expiringMap.forEach((value, key) => {
entries.push([key, value]);
const d = new Date();
const time = d.getTime();
Deno.test("Store: save it to github", async () => {
const token = Deno.env.get("GITHUB_TOKEN");
if (!token) return;
const expiringMap = new Store<string, number>({
owner: "fastrodev",
repo: "fastro",
path: "modules/store/records.json",
branch: "store",
token,
});

assertEquals(entries.length, 2);
assertEquals(entries, [["key1", "value1"], ["key2", "value2"]]);
expiringMap.set("key1", time);
const r = await expiringMap.commit();
assertEquals(r?.data.content?.name, "records.json");
});

Deno.test("ExpiringMap: should set a key with custom TTL", async () => {
const expiringMap = new Store<string, string>(2000);
expiringMap.set("key1", "value1", 1000);
assertEquals(await expiringMap.get("key1"), "value1");
await new Promise((resolve) => setTimeout(resolve, 1100));
assertEquals(await expiringMap.get("key1"), undefined);
Deno.test("Store: get value from github", async () => {
const token = Deno.env.get("GITHUB_TOKEN");
if (!token) return;
const expiringMap = new Store<string, number>({
owner: "fastrodev",
repo: "fastro",
path: "modules/store/records.json",
branch: "store",
token,
});
// get kv from github
const g = await expiringMap.get("key1");
assertEquals(g, time);
});

Deno.test("ExpiringMap: save it to github", async () => {
Deno.test("Store: destroy map", async () => {
const token = Deno.env.get("GITHUB_TOKEN");
if (!token) return;
const expiringMap = new Store<string, number>(2000, {
const expiringMap = new Store<string, number>({
owner: "fastrodev",
repo: "fastro",
path: "modules/store/records.json",
branch: "store",
token,
});
const d = new Date();
expiringMap.set("key1", d.getTime(), 1000);
const c = await expiringMap.commit();
assertEquals(c?.data.content?.name, "records.json");
await expiringMap.destroy();
const g = await expiringMap.get("key1");
assertEquals(g, undefined);
});
Loading