Files
immich/web/src/lib/utils/executor-queue.spec.ts
Jorge Avila 5f6d09d3da chore(web): migrate to vitest (#5754)
* Updated vite and ts config file with vtest options and a new alias to fix the dev command error

* Updated package script and update the packages
 -- this removes jest dependencies

* Added new setup file needed in vitest in order to be able to use the jest-dom matchers in tests

* Updated deprecated utilities when using faker

* Updated test files and mocks to use vitest instead of jest

* Enabled web test check in GitHub actions

* remove babel dependencies as they are no longer needed with vitest

* move the jest config files to a folder in case we need to go back to jest

* chore: remove old files

---------

Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
2024-01-01 12:36:49 -05:00

53 lines
1.6 KiB
TypeScript

import { ExecutorQueue } from '$lib/utils/executor-queue';
describe('Executor Queue test', function () {
it('should run all promises', async function () {
const eq = new ExecutorQueue({ concurrency: 1 });
const n1 = await eq.addTask(() => Promise.resolve(10));
expect(n1).toBe(10);
const n2 = await eq.addTask(() => Promise.resolve(11));
expect(n2).toBe(11);
const n3 = await eq.addTask(() => Promise.resolve(12));
expect(n3).toBe(12);
});
it('should respect concurrency parameter', function () {
vi.useFakeTimers();
const eq = new ExecutorQueue({ concurrency: 3 });
const finished = vi.fn();
const started = vi.fn();
const timeoutPromiseBuilder = (delay: number, id: string) =>
new Promise((resolve) => {
started();
setTimeout(() => {
finished();
resolve(id);
}, delay);
});
// The first 3 should be finished within 200ms (concurrency 3)
eq.addTask(() => timeoutPromiseBuilder(100, 'T1'));
eq.addTask(() => timeoutPromiseBuilder(200, 'T2'));
eq.addTask(() => timeoutPromiseBuilder(150, 'T3'));
// The last task will be executed after 200ms and will finish at 400ms
eq.addTask(() => timeoutPromiseBuilder(200, 'T4'));
expect(finished).not.toBeCalled();
expect(started).toHaveBeenCalledTimes(3);
vi.advanceTimersByTime(100);
expect(finished).toHaveBeenCalledTimes(1);
vi.advanceTimersByTime(250);
expect(finished).toHaveBeenCalledTimes(3);
// expect(started).toHaveBeenCalledTimes(4)
//TODO : fix The test ...
vi.runAllTimers();
vi.useRealTimers();
});
});