add basic unit test
This commit is contained in:
parent
f74684396b
commit
a5c1abbbed
6 changed files with 4544 additions and 1 deletions
11
jest.config.js
Normal file
11
jest.config.js
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
const { createDefaultPreset } = require("ts-jest");
|
||||||
|
|
||||||
|
const tsJestTransformCfg = createDefaultPreset().transform;
|
||||||
|
|
||||||
|
/** @type {import("jest").Config} **/
|
||||||
|
module.exports = {
|
||||||
|
testEnvironment: "node",
|
||||||
|
transform: {
|
||||||
|
...tsJestTransformCfg,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
@ -3,3 +3,6 @@ node = "24"
|
||||||
|
|
||||||
[tasks.build]
|
[tasks.build]
|
||||||
run = "npx rollup -c"
|
run = "npx rollup -c"
|
||||||
|
|
||||||
|
[tasks.test]
|
||||||
|
run = "npx jest"
|
||||||
|
|
|
||||||
4502
package-lock.json
generated
4502
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -19,7 +19,10 @@
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rollup/plugin-terser": "^0.4.4",
|
"@rollup/plugin-terser": "^0.4.4",
|
||||||
"@rollup/plugin-typescript": "^12.3.0",
|
"@rollup/plugin-typescript": "^12.3.0",
|
||||||
|
"@types/jest": "^30.0.0",
|
||||||
|
"jest": "^30.2.0",
|
||||||
"rollup": "^4.57.1",
|
"rollup": "^4.57.1",
|
||||||
|
"ts-jest": "^29.4.6",
|
||||||
"tslib": "^2.8.1",
|
"tslib": "^2.8.1",
|
||||||
"typescript": "^5.9.3"
|
"typescript": "^5.9.3"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
24
src/index.test.ts
Normal file
24
src/index.test.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
import { AsyncTaskQueue } from "./index";
|
||||||
|
|
||||||
|
test("example evaluates correctly", async () => {
|
||||||
|
const q = new AsyncTaskQueue<number>(
|
||||||
|
async (nums: number[]) => {/* no-op */},
|
||||||
|
)
|
||||||
|
// Handler will receive up to 4 items per batch.
|
||||||
|
.batchSize(4)
|
||||||
|
// Handler will be called at most once per 100 milliseconds.
|
||||||
|
.throttleMs(100);
|
||||||
|
|
||||||
|
// Add items to the queue.
|
||||||
|
for (let n = 0; n < 1000; n++) {
|
||||||
|
q.push(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 4000));
|
||||||
|
|
||||||
|
expect(q.size()).toBeLessThan(900);
|
||||||
|
expect(q.size()).toBeGreaterThan(800);
|
||||||
|
|
||||||
|
q.drain();
|
||||||
|
expect(q.size()).toEqual(0);
|
||||||
|
});
|
||||||
|
|
@ -102,7 +102,7 @@ export class AsyncTaskQueue<T> {
|
||||||
: Promise.resolve();
|
: Promise.resolve();
|
||||||
|
|
||||||
const items = this._queue.splice(0, this._batchSize);
|
const items = this._queue.splice(0, this._batchSize);
|
||||||
await this._handler(items).catch(this._error_handler);
|
await this._handler(items).catch(this._errorHandler);
|
||||||
|
|
||||||
await throttleTimeout;
|
await throttleTimeout;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue