This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,39 @@ | ||
import * as tape from 'tape' | ||
import { PrioritizedTaskExecutor } from '../src/prioritizedTaskExecutor' | ||
|
||
const taskExecutor = new PrioritizedTaskExecutor(2) | ||
tape('prioritized task executor test', function (t: any) { | ||
t.test('should execute tasks in the right order', async (st: any) => { | ||
const taskExecutor = new PrioritizedTaskExecutor(2) | ||
const tasks = [1, 2, 3, 4] | ||
const callbacks = [] as any | ||
const executionOrder = [] as any | ||
for (let task of tasks) { | ||
await taskExecutor.execute(task, function (cb: Function) { | ||
executionOrder.push(task) | ||
callbacks.push(cb) | ||
}) | ||
} | ||
|
||
tape('prioritized task executor test', function (t) { | ||
const tasks = [1, 2, 3, 4] | ||
const callbacks = [] as any | ||
const executionOrder = [] as any | ||
tasks.forEach(function (task) { | ||
taskExecutor.execute(task, function (cb: Function) { | ||
executionOrder.push(task) | ||
callbacks.push(cb) | ||
}) | ||
}) | ||
for (let callback of callbacks) { | ||
await callback() | ||
} | ||
|
||
callbacks.forEach(function (callback: Function) { | ||
callback() | ||
const expectedExecutionOrder = [1, 2, 4, 3] | ||
st.deepEqual(executionOrder, expectedExecutionOrder) | ||
st.end() | ||
}) | ||
|
||
const expectedExecutionOrder = [1, 2, 4, 3] | ||
t.deepEqual(executionOrder, expectedExecutionOrder) | ||
t.end() | ||
t.test('should queue tasks in the right order', async (st: any) => { | ||
const priorityList = [0, 1, 0, 2, 0, 1, 0, 2, 2, 1] | ||
const PTE = new PrioritizedTaskExecutor(0) // this ensures that no task actually gets executed, so this essentially just checks the sort algorithm | ||
for (let priority of priorityList) { | ||
await PTE.execute(priority, () => {}) | ||
} | ||
// have to cast the PTE as <any> to access the private queue | ||
st.deepEqual( | ||
(<any>PTE).queue.map((task: any) => task.priority), | ||
priorityList.sort((a: any, b: any) => b - a), | ||
) | ||
st.end() | ||
}) | ||
}) |