Skip to content

Commit 04d70ed

Browse files
committed
upgraded tests
1 parent 319a87b commit 04d70ed

4 files changed

Lines changed: 33 additions & 5 deletions

File tree

packages/core/src/cache.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,19 @@ for (const type of ["memory", "jsonl", "fs"]) {
7272
const cachedValue = await cache.get("newKey")
7373
assert.strictEqual(cachedValue, 99)
7474
})
75+
76+
test("values() retrieves all stored values", async () => {
77+
const cache = createCache<string, number>("testCache", {
78+
type: type as any,
79+
})
80+
await cache.set("key1", 10)
81+
await cache.set("key2", 20)
82+
await cache.set("key3", 30)
83+
84+
const values = await cache.values()
85+
assert(values.includes(10))
86+
assert(values.includes(20))
87+
assert(values.includes(30))
88+
})
7589
})
7690
}

packages/core/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ export const TEMPLATE_ARG_DATA_SLICE_SAMPLE = 2000
335335

336336
export const CHAT_REQUEST_PER_MODEL_CONCURRENT_LIMIT = 8
337337
export const PROMISE_QUEUE_CONCURRENCY_DEFAULT = 16
338+
export const FILE_READ_CONCURRENCY_DEFAULT = 16
338339

339340
export const GITHUB_REST_API_CONCURRENCY_LIMIT = 8
340341
export const GITHUB_REST_PAGE_DEFAULT = 10

packages/core/src/fscache.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ import debug, { Debugger } from "debug"
77
import { errorMessage } from "./error"
88
import { tryReadJSON } from "./fs"
99
import { rm, readdir } from "fs/promises"
10-
import { CACHE_FORMAT_VERSION, CACHE_SHA_LENGTH } from "./constants"
10+
import {
11+
CACHE_FORMAT_VERSION,
12+
CACHE_SHA_LENGTH,
13+
FILE_READ_CONCURRENCY_DEFAULT,
14+
} from "./constants"
1115
import { hash } from "./crypto"
16+
import pLimit from "p-limit"
1217

1318
/**
1419
* A cache class stores each entry as a separate file in a directory.
@@ -54,10 +59,15 @@ export class FsCache<K, V> implements WorkspaceFileCache<any, any> {
5459
}
5560
async values(): Promise<any[]> {
5661
try {
62+
const dir = this.folder()
5763
const files = await readdir(this.folder())
58-
return files
59-
.filter((f) => /\.json$/.test(f))
60-
.map((f) => basename(f).replace(/\.json$/, ""))
64+
const limit = pLimit(FILE_READ_CONCURRENCY_DEFAULT)
65+
return await Promise.all(
66+
files
67+
.filter((f) => /\.json$/.test(f))
68+
.map((f) => limit(() => tryReadJSON(join(dir, f))))
69+
.filter((f) => f !== undefined)
70+
)
6171
} catch (e) {
6272
this.dbg(
6373
`error while reading directory ${this.folder()}: ${errorMessage(e)}`

packages/sample/genaisrc/cache.genai.mts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ for (const cache of [
1010
await workspace.cache<number, number>("test-cache"),
1111
await host.cache<number, number>("test-cache"),
1212
]) {
13+
console.log(`cache test ${cache.name}`)
1314
await cache.set(key, value)
1415
const result = await cache.get(key)
1516
if (result !== value) throw new Error(`unexpected value: ${result}`)
1617

1718
const values = await cache.values()
1819
if (!values.includes(value)) {
19-
throw new Error(`unexpected values: ${values}`)
20+
throw new Error(
21+
`unexpected values: ${values.join("\n")} in ${cache.name}`
22+
)
2023
}
2124
}
2225

0 commit comments

Comments
 (0)