1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/test/backend/unit/model/threading/TaskExecuter.spec.ts

67 lines
1.6 KiB
TypeScript
Raw Normal View History

2018-12-09 01:17:33 +08:00
import {expect} from 'chai';
import {TaskExecuter} from '../../../../../backend/model/threading/TaskExecuter';
describe('TaskExecuter', () => {
it('should execute', async () => {
const taskWorker = (input: number) => {
return new Promise<number>((resolve, reject) => {
setTimeout(() => {
resolve(input * 2);
}, 1);
});
};
const tq = new TaskExecuter<number, number>(10, taskWorker);
expect(await tq.execute(1)).to.be.equal(2);
expect(await tq.execute(10)).to.be.equal(20);
expect(await tq.execute(1)).to.be.equal(2);
expect(await tq.execute(111)).to.be.equal(222);
});
it('should fail', async () => {
const taskWorker = (input: number) => {
return new Promise<number>((resolve, reject) => {
setTimeout(() => {
reject((input * 2).toString());
}, 1);
});
};
const tq = new TaskExecuter<number, number>(10, taskWorker);
try {
await tq.execute(1);
expect(false).to.be.equal(true); // should not reach
} catch (e) {
expect(e).to.be.equal('2');
}
});
it('should handle race condition', async () => {
let counter = 0;
const taskWorker = (input: number) => {
return new Promise<void>((resolve, reject) => {
setTimeout(() => {
counter++;
resolve();
}, 1);
});
};
const tq = new TaskExecuter<number, void>(10, taskWorker);
const prs = [];
prs.push(tq.execute(1));
prs.push(tq.execute(1));
prs.push(tq.execute(1));
prs.push(tq.execute(2));
prs.push(tq.execute(2));
await Promise.all(prs);
expect(counter).to.be.equal(2);
});
});