Skip to content

Commit

Permalink
Test delayed scheduling.
Browse files Browse the repository at this point in the history
  • Loading branch information
jjrv committed Jul 13, 2017
1 parent cbcf2d3 commit a2dff3b
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,40 @@ function test3() {
queue.wrap(rejects)().then(null as any, (err: any) => ++testCount);
}

function test4() {
const unsorted: number[] = [];

function compare(a: number, b: number) { return(a - b); }

for(let num = 0; num < 100; ++num) {
unsorted[num] = ~~(Math.random() * 50);
}

const correct = unsorted.slice(0).sort(compare);

const queue = new TaskQueue(Promise, 1);
const result: number[] = [];
const start = new Date().getTime();

Promise.map(
unsorted,
(item: number) => queue.add(
() => {
const delta = new Date().getTime() - start - item * 10;
if(delta > 0 && delta < 20) result.push(item);
},
item * 10
)
).then(
() => result.join(' ') == correct.join(' ') && ++testCount
);
}

test1();
test2();
test3();
test4();

setTimeout(() => {
equals(testCount, 6);
equals(testCount, 7);
}, 1000);

0 comments on commit a2dff3b

Please sign in to comment.