|
1 | 1 | import * as tape from 'tape' |
2 | 2 | import { PrioritizedTaskExecutor } from '../src/prioritizedTaskExecutor' |
3 | 3 |
|
4 | | -const taskExecutor = new PrioritizedTaskExecutor(2) |
| 4 | +tape('prioritized task executor test', function (t: any) { |
| 5 | + t.test('should execute tasks in the right order', (st: any) => { |
| 6 | + const taskExecutor = new PrioritizedTaskExecutor(2) |
| 7 | + const tasks = [1, 2, 3, 4] |
| 8 | + const callbacks = [] as any |
| 9 | + const executionOrder = [] as any |
| 10 | + tasks.forEach(function (task) { |
| 11 | + taskExecutor.execute(task, function (cb: Function) { |
| 12 | + executionOrder.push(task) |
| 13 | + callbacks.push(cb) |
| 14 | + }) |
| 15 | + }) |
5 | 16 |
|
6 | | -tape('prioritized task executor test', function (t) { |
7 | | - const tasks = [1, 2, 3, 4] |
8 | | - const callbacks = [] as any |
9 | | - const executionOrder = [] as any |
10 | | - tasks.forEach(function (task) { |
11 | | - taskExecutor.execute(task, function (cb: Function) { |
12 | | - executionOrder.push(task) |
13 | | - callbacks.push(cb) |
| 17 | + callbacks.forEach(function (callback: Function) { |
| 18 | + callback() |
14 | 19 | }) |
15 | | - }) |
16 | 20 |
|
17 | | - callbacks.forEach(function (callback: Function) { |
18 | | - callback() |
| 21 | + const expectedExecutionOrder = [1, 2, 4, 3] |
| 22 | + st.deepEqual(executionOrder, expectedExecutionOrder) |
| 23 | + st.end() |
19 | 24 | }) |
20 | 25 |
|
21 | | - const expectedExecutionOrder = [1, 2, 4, 3] |
22 | | - t.deepEqual(executionOrder, expectedExecutionOrder) |
23 | | - t.end() |
| 26 | + t.test('should queue tasks in the right order', (st: any) => { |
| 27 | + const priorityList = [0, 1, 0, 2, 0, 1, 0, 2, 2, 1] |
| 28 | + const PTE = new PrioritizedTaskExecutor(0) // this ensures that no task actually gets executed, so this essentially just checks the sort algorithm |
| 29 | + priorityList.map((priority) => { |
| 30 | + PTE.execute(priority, () => {}) |
| 31 | + }) |
| 32 | + // have to cast the PTE as <any> to access the private queue |
| 33 | + st.deepEqual( |
| 34 | + (<any>PTE).queue.map((task: any) => task.priority), |
| 35 | + priorityList.sort((a: any, b: any) => b - a), |
| 36 | + ) |
| 37 | + st.end() |
| 38 | + }) |
24 | 39 | }) |
0 commit comments