cachedAsyncBuffer tests

This commit is contained in:
Kenny Daniel 2024-10-16 01:35:13 -07:00
parent 5d21b09b7a
commit a5c34e2950
No known key found for this signature in database
GPG Key ID: 90AB653A8CAD7E45
6 changed files with 56 additions and 13 deletions

2
demo/bundle.min.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -27,25 +27,25 @@
"test": "vitest run"
},
"devDependencies": {
"@rollup/plugin-commonjs": "28.0.0",
"@rollup/plugin-commonjs": "28.0.1",
"@rollup/plugin-node-resolve": "15.3.0",
"@rollup/plugin-replace": "6.0.1",
"@rollup/plugin-terser": "0.4.4",
"@rollup/plugin-typescript": "12.1.0",
"@rollup/plugin-typescript": "12.1.1",
"@types/node": "22.7.5",
"@types/react": "18.3.11",
"@types/react-dom": "18.3.0",
"@vitest/coverage-v8": "2.1.2",
"@types/react-dom": "18.3.1",
"@vitest/coverage-v8": "2.1.3",
"eslint": "9.12.0",
"eslint-plugin-jsdoc": "50.3.1",
"hightable": "0.5.2",
"eslint-plugin-jsdoc": "50.4.1",
"hightable": "0.5.3",
"http-server": "14.1.1",
"hyparquet-compressors": "0.1.4",
"react": "18.3.1",
"react-dom": "18.3.1",
"rollup": "4.24.0",
"typescript": "5.6.2",
"typescript-eslint": "8.8.1",
"vitest": "2.1.2"
"typescript": "5.6.3",
"typescript-eslint": "8.9.0",
"vitest": "2.1.3"
}
}

43
test/asyncbuffer.test.js Normal file

@ -0,0 +1,43 @@
import { describe, expect, it, vi } from 'vitest'
import { cachedAsyncBuffer } from '../src/asyncBuffer.js'
describe('cachedAsyncBuffer', () => {
it('caches slices of a file to avoid multiple reads', async () => {
const slice = vi.fn(async (start, end) => {
// Simulate an async slice operation
if (end === undefined) end = 1000
if (start < 0) start = Math.max(0, 1000 + start)
const buffer = new ArrayBuffer(end - start)
return buffer
})
const cachedFile = cachedAsyncBuffer({
byteLength: 1000,
slice,
})
// Test cache miss
const slice1 = await cachedFile.slice(0, 100)
expect(slice).toHaveBeenCalledTimes(1)
expect(slice1.byteLength).toBe(100)
// Test cache hit for the same range
const slice2 = await cachedFile.slice(0, 100)
expect(slice).toHaveBeenCalledTimes(1) // No additional call
expect(slice2).toBe(slice1) // Exact same object from cache
// Test cache with undefined end, should use byteLength as end
const slice3 = await cachedFile.slice(900)
expect(slice).toHaveBeenCalledTimes(2)
expect(slice3.byteLength).toBe(100)
// Test cache hit for suffix-range
const slice4 = await cachedFile.slice(-100)
expect(slice).toHaveBeenCalledTimes(2) // Still no additional call
expect(slice4).toBe(slice3) // Cached result reused
// Verify that asking for the same end implicitly gets from cache
const slice5 = await cachedFile.slice(900, 1000)
expect(slice).toHaveBeenCalledTimes(2) // Still no additional call
expect(slice5).toBe(slice3) // Cached result reused
})
})