mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-09-27 03:48:12 +08:00
* Disabled @blueprintjs/classes-constants. #2288. * thuang-eslint-bp-off (#2344) * Disabled @blueprintjs/classes-constants on webpack dev and shared. #2288. * Added TS recommended, suppress lint errors codemod. * Added per-error/warning ignore for tests. * Added per-error/warning ignore for configuration. * Added per-error/warning ignore for src. Removed suppress package. * Minor linting. Co-authored-by: Timmy Huang <tihuan@users.noreply.github.com>
100 lines
3.1 KiB
TypeScript
100 lines
3.1 KiB
TypeScript
import PromiseLimit from "../../src/util/promiseLimit";
|
|
import { range } from "../../src/util/range";
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
|
|
const delay = (t: any) => new Promise((resolve) => setTimeout(resolve, t));
|
|
|
|
describe("PromiseLimit", () => {
|
|
test("simple evaluation, concurrency 1", async () => {
|
|
const plimit = new PromiseLimit(1);
|
|
const result = await Promise.all([
|
|
plimit.add(() => Promise.resolve(1)),
|
|
plimit.add(() => Promise.resolve(2)),
|
|
plimit.add(() => Promise.resolve(3)),
|
|
plimit.add(() => Promise.resolve(4)),
|
|
]);
|
|
expect(result).toEqual([1, 2, 3, 4]);
|
|
});
|
|
|
|
test("simple evaluation, concurrency > 1", async () => {
|
|
const plimit = new PromiseLimit(100);
|
|
const result = await Promise.all([
|
|
plimit.add(() => Promise.resolve(1)),
|
|
plimit.add(() => Promise.resolve(2)),
|
|
plimit.add(() => Promise.resolve(3)),
|
|
plimit.add(() => Promise.resolve(4)),
|
|
]);
|
|
expect(result).toEqual([1, 2, 3, 4]);
|
|
});
|
|
|
|
test("eval in order of insertion", async () => {
|
|
const plimit = new PromiseLimit(100);
|
|
|
|
const result = await Promise.all([
|
|
plimit.add(() => Promise.resolve(1)),
|
|
plimit.add(() => Promise.resolve(2)),
|
|
plimit.add(() => Promise.resolve(3)),
|
|
plimit.add(() => Promise.resolve(4)),
|
|
]);
|
|
|
|
expect(result).toEqual([1, 2, 3, 4]);
|
|
});
|
|
|
|
test("obeys concurrency limit", async () => {
|
|
const plimit = new PromiseLimit(2);
|
|
let running = 0;
|
|
let maxRunning = 0;
|
|
|
|
const callback = async () => {
|
|
running += 1;
|
|
maxRunning = running > maxRunning ? running : maxRunning;
|
|
await delay(100);
|
|
running -= 1;
|
|
};
|
|
|
|
// @ts-expect-error ts-migrate(2554) FIXME: Expected 3 arguments, but got 1.
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
|
|
await Promise.all(range(10).map((i: any) => plimit.add(() => callback(i))));
|
|
|
|
expect(maxRunning).toEqual(2);
|
|
});
|
|
|
|
test("rejection", async () => {
|
|
const plimit = new PromiseLimit(2);
|
|
const result = await Promise.all([
|
|
plimit.add(() => Promise.resolve("OK")),
|
|
// eslint-disable-next-line prefer-promise-reject-errors -- unit test
|
|
plimit.add(() => Promise.reject("not OK")).catch((e) => e),
|
|
plimit.add(() => Promise.resolve("OK")),
|
|
plimit
|
|
.add(() => {
|
|
throw new Error("not OK");
|
|
})
|
|
.catch((e) => e.message),
|
|
]);
|
|
expect(result).toEqual(["OK", "not OK", "OK", "not OK"]);
|
|
});
|
|
|
|
test("priority queue", async () => {
|
|
const plimit = new PromiseLimit(1);
|
|
|
|
let finishOrder = 0;
|
|
const callback = () => async () => {
|
|
await delay(100);
|
|
const result = finishOrder;
|
|
finishOrder += 1;
|
|
return result;
|
|
};
|
|
|
|
const result = await Promise.all([
|
|
plimit.add(callback()),
|
|
plimit.priorityAdd(4, callback()),
|
|
plimit.priorityAdd(0, callback()),
|
|
plimit.priorityAdd(1, callback()),
|
|
plimit.priorityAdd(-1, callback()),
|
|
]);
|
|
|
|
expect(result).toEqual([0, 4, 2, 3, 1]);
|
|
});
|
|
});
|