cache tests

main
Ethan Niser 2023-09-17 00:07:28 -05:00
parent ca4b2509c2
commit 5486079888
3 changed files with 249 additions and 3 deletions

2
.gitignore vendored

@ -173,4 +173,4 @@ local.sqlite
local.sqlite-shm
local.sqlite-wal
.beth/
beth-cache.sqlite

@ -19,7 +19,7 @@ class BethPersistCache {
constructor() {
this.callBackMap = new Map();
this.inMemoryDataCache = new Map();
this.jsonDataCache = new Database("./.beth/beth-cache.sqlite");
this.jsonDataCache = new Database("beth-cache.sqlite");
this.intervals = new Set();
this.pendingMap = new Map();
this.keys = new Set();
@ -82,7 +82,7 @@ class BethPersistCache {
promise.then((value) => {
this.pendingMap.delete(key);
console.log(`Seeding ${cache} Cache:`, key, value);
console.log(`Seeding ${cache} Cache:`, key);
if (cache === "memory") {
this.inMemoryDataCache.set(key, value);
} else if (cache === "json") {

@ -22,6 +22,16 @@ test("static json cache", async () => {
expect(html).toBe(`<p>number: 1</p><p>number: 1</p>`);
// This should result in no 'cache hit' log, because the render cache is never reset from the previous render
const html2 = await (
<>
<Component />
<Component />
</>
);
expect(html2).toBe(`<p>number: 1</p><p>number: 1</p>`);
// even in a new render we get the same results
const Test = () => <Component />;
@ -304,3 +314,239 @@ test("memory cache revalidate tag", async () => {
expect(html3).toBe(`<p>number: 3</p><p>number: 3</p>`);
});
test("request during interval revalidation", async () => {
let count = 0;
const getCount = async () =>
new Promise((resolve) => setTimeout(() => resolve(++count), 100));
const cachedGetCount = persistedCache(getCount, "getCount7", {
revalidate: 1,
});
const Component = async () => {
const data = await cachedGetCount();
return <p>number: {data}</p>;
};
const html = await renderToString(() => (
<>
<Component />
<Component />
</>
));
expect(html).toBe(`<p>number: 1</p><p>number: 1</p>`);
// cache request goes off during revalidation
// should result in 'pending cache hit' log + updated data
await new Promise((resolve) =>
setTimeout(async () => {
const html3 = await renderToString(() => (
<>
<Component />
<Component />
</>
));
expect(html3).toBe(`<p>number: 2</p><p>number: 2</p>`);
resolve(void 0);
}, 1010)
);
});
test("request during tag revalidation", async () => {
let count = 0;
const getCount = async () =>
new Promise((resolve) => setTimeout(() => resolve(++count), 100));
const cachedGetCount = persistedCache(getCount, "getCount8", {
tags: ["tag1"],
});
const Component = async () => {
const data = await cachedGetCount();
return <p>number: {data}</p>;
};
const html = await renderToString(() => (
<>
<Component />
<Component />
</>
));
expect(html).toBe(`<p>number: 1</p><p>number: 1</p>`);
setTimeout(() => {
count++;
revalidateTag("tag1");
}, 1000);
// cache request goes off during revalidation
// should result in 'pending cache hit' log + updated data
await new Promise((resolve) =>
setTimeout(async () => {
const html3 = await renderToString(() => (
<>
<Component />
<Component />
</>
));
expect(html3).toBe(`<p>number: 3</p><p>number: 3</p>`);
resolve(void 0);
}, 1010)
);
});
test("interval during tag revalidation", async () => {
let count = 0;
const getCount = async () =>
new Promise((resolve) => setTimeout(() => resolve(++count), 300));
const cachedGetCount = persistedCache(getCount, "getCount9", {
tags: ["tag1"],
revalidate: 1,
});
const Component = async () => {
const data = await cachedGetCount();
return <p>number: {data}</p>;
};
const html = await renderToString(() => (
<>
<Component />
<Component />
</>
));
expect(html).toBe(`<p>number: 1</p><p>number: 1</p>`);
setTimeout(() => {
count++;
revalidateTag("tag1");
}, 900);
// should see pending cache hit for interval revalidation
// cache request goes off during revalidation
// should result in 2nd 'pending cache hit' log + updated data
await new Promise((resolve) =>
setTimeout(async () => {
const html3 = await renderToString(() => (
<>
<Component />
<Component />
</>
));
expect(html3).toBe(`<p>number: 3</p><p>number: 3</p>`);
resolve(void 0);
}, 1100)
);
});
test("interval during tag revalidation", async () => {
let count = 0;
const getCount = async () =>
new Promise((resolve) => setTimeout(() => resolve(++count), 300));
const cachedGetCount = persistedCache(getCount, "getCount10", {
tags: ["tag1"],
revalidate: 1,
});
const Component = async () => {
const data = await cachedGetCount();
return <p>number: {data}</p>;
};
const html = await renderToString(() => (
<>
<Component />
<Component />
</>
));
expect(html).toBe(`<p>number: 1</p><p>number: 1</p>`);
setTimeout(() => {
count++;
revalidateTag("tag1");
}, 1100);
// should see pending cache hit for tag revalidation
// cache request goes off during revalidation
// should result in 2nd 'pending cache hit' log + updated data
await new Promise((resolve) =>
setTimeout(async () => {
const html3 = await renderToString(() => (
<>
<Component />
<Component />
</>
));
expect(html3).toBe(`<p>number: 3</p><p>number: 3</p>`);
resolve(void 0);
}, 1150)
);
});
test("complex object storage to memory", async () => {
const getData = async () => ({
a: 1,
b: 2,
c: {
d: 3,
e: [4, 5, 6],
},
});
const cachedGetData = persistedCache(getData, "getData1", {
persist: "memory",
});
const data = await cachedGetData();
expect(data).toStrictEqual({
a: 1,
b: 2,
c: {
d: 3,
e: [4, 5, 6],
},
});
});
test("complex object storage to json", async () => {
const getData = async () => ({
a: 1,
b: 2,
c: {
d: 3,
e: [4, 5, 6],
},
});
const cachedGetData = persistedCache(getData, "getData2", {
persist: "json",
});
const data = await cachedGetData();
expect(data).toStrictEqual({
a: 1,
b: 2,
c: {
d: 3,
e: [4, 5, 6],
},
});
});