Skip to content

Commit

Permalink
feat(test): add Scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
deot committed Dec 18, 2023
1 parent 2bdd1fc commit eaeb92c
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 1 deletion.
104 changes: 104 additions & 0 deletions packages/test/__tests__/scheduler.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { Scheduler } from '@deot/dev-test';

describe('scheduler.ts', () => {
it('next', async () => {
const scheduler = new Scheduler();
const next = vi.fn(scheduler.next);
const fail = vi.fn(scheduler.nextWithError);
setTimeout(next, 10);
await scheduler;

expect(next).toBeCalledTimes(1);
expect(fail).toBeCalledTimes(0);

setTimeout(next, 10);
await scheduler;
expect(next).toBeCalledTimes(2);
expect(fail).toBeCalledTimes(0);

try {
setTimeout(fail, 10);
await scheduler;
} catch {
expect(next).toBeCalledTimes(2);
expect(fail).toBeCalledTimes(1);
}
});

it('resolve', async () => {
expect.assertions(1);
const scheduler = new Scheduler();
const resolve = vi.fn(scheduler.finish);
setTimeout(resolve, 10);
await scheduler;

await scheduler.next();
await scheduler.nextWithError();
await scheduler;
await scheduler;
await scheduler;
await scheduler;

expect(resolve).toBeCalledTimes(1);
});

it('reject', async () => {
expect.assertions(4);
const scheduler = new Scheduler();
const reject = vi.fn(scheduler.finishWithError);
setTimeout(reject, 10);
try {
await scheduler;
} catch {
expect(reject).toBeCalledTimes(1);
}

try {
await scheduler;
} catch {
expect(reject).toBeCalledTimes(1);
}

try {
await scheduler.next();
} catch {
expect(reject).toBeCalledTimes(1);
}

try {
await scheduler.nextWithError();
} catch {
expect(reject).toBeCalledTimes(1);
}
});

it('catch', async () => {
expect.assertions(1);
const scheduler = new Scheduler();
const reject = vi.fn(scheduler.finishWithError);
setTimeout(reject, 10);

scheduler.catch(() => {
expect(reject).toBeCalledTimes(1);
});

try {
await scheduler;
} catch {
// any
}
});

it('finally', async () => {
expect.assertions(1);
const scheduler = new Scheduler();
const resolve = vi.fn(scheduler.finish);
setTimeout(resolve, 10);

scheduler.finally(() => {
expect(resolve).toBeCalledTimes(1);
});

await scheduler;
});
});
3 changes: 2 additions & 1 deletion packages/test/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import { Command } from './command';
import { Launch } from './launch';
import { Operater } from './operater';
import { Scheduler } from './scheduler';

import * as E2E from './e2e';
import * as Utils from './utils';
import * as Server from './server';

export { Command, E2E, Utils, Launch, Operater, Server };
export { Command, E2E, Utils, Launch, Operater, Server, Scheduler };
60 changes: 60 additions & 0 deletions packages/test/src/scheduler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
type Func<T> = (_?: T) => void;

export class Scheduler<T = any> {
_success?: Func<any>;
_fail?: Func<any>;
_task!: Promise<any>;
_finish = false;
constructor() {
this.next();
}

next = (v?: T) => {
if (!this._finish) {
this._success?.(v);
this._task = new Promise<T>((resolve, reject) => {
this._success = (value?: any) => {
this._fail = undefined;
resolve(value);
};
this._fail = (value?: any) => {
this._success = undefined;
reject(value);
};
});
}
return this;
};

nextWithError = (v?: any) => {
if (!this._finish) {
this._fail?.(v);
this.next();
}
return this;
};

finish = (v?: T) => {
this._success?.(v);
this._finish = true;
return this;
};

finishWithError = (v?: T) => {
this._fail?.(v);
this._finish = true;
return this;
};

then(resolve: Func<T>, reject: Func<T>) {
return this._task.then(resolve, reject);
}

catch(callback?: Func<T>) {
return this._task.catch(callback);
}

finally(callback?: Func<void>) {
return this._task.finally(callback);
}
}

0 comments on commit eaeb92c

Please sign in to comment.