UNPKG

nope-js-node

Version:

NoPE Runtime for Nodejs. For Browser-Support please use nope-browser

104 lines (103 loc) 4.69 kB
"use strict"; /** * @author Martin Karkowski * @email m.karkowski@zema.de * @desc [description] */ Object.defineProperty(exports, "__esModule", { value: true }); const async_1 = require("./async"); const chai_1 = require("chai"); const mocha_1 = require("mocha"); const taskQueue_1 = require("./taskQueue"); (0, mocha_1.describe)("PriorityTaskQueue", function () { // Describe the required Test: (0, mocha_1.describe)("Async Functions", function () { (0, mocha_1.it)("no parallel execution - no priority", async function () { let called = []; async function delayed(ret) { await (0, async_1.sleep)(25); called.push(ret); return ret; } const queue = new taskQueue_1.ParallelPriorityTaskQueue(); queue.maxParallel = 1; queue.usePriority = false; const promises = [ queue.execute(delayed, ["first"], 5), queue.execute(delayed, ["second"], 10), ]; const start = Date.now(); await Promise.all(promises); const diff = Date.now() - start; (0, chai_1.assert)(diff > 40, "Functions should be called after each other"); (0, chai_1.assert)(called[0] == "first", "First should be the first entry"); (0, chai_1.assert)(called[1] == "second", "First should be the first entry"); }); (0, mocha_1.it)("parallel execution - no priority", async function () { let called = []; async function delayed(ret) { await (0, async_1.sleep)(25); called.push(ret); return ret; } const queue = new taskQueue_1.ParallelPriorityTaskQueue(); queue.maxParallel = 10; queue.usePriority = false; const promises = [ queue.execute(delayed, ["first"], 5), queue.execute(delayed, ["second"], 10), ]; const start = Date.now(); await Promise.all(promises); const diff = Date.now() - start; (0, chai_1.assert)(diff < 40, "Functions should be called parallel"); (0, chai_1.assert)(called[0] == "first", "First should be the first entry"); (0, chai_1.assert)(called[1] == "second", "First should be the first entry"); }); (0, mocha_1.it)("no parallel execution - with priority", async function () { let called = []; async function delayed(ret) { await (0, async_1.sleep)(25); called.push(ret); return ret; } const queue = new taskQueue_1.ParallelPriorityTaskQueue(); queue.maxParallel = 1; queue.usePriority = true; const promises = [ queue.execute(delayed, ["first"], 5), queue.execute(delayed, ["second"], 10), queue.execute(delayed, ["third"], 15), ]; const start = Date.now(); await Promise.all(promises); const diff = Date.now() - start; (0, chai_1.assert)(diff > 40, "Functions should be called after each other"); (0, chai_1.assert)(called[1] == "third", "second should be the third entry"); (0, chai_1.assert)(called[2] == "second", "third should be the second entry"); }); (0, mocha_1.it)("parallel execution - with priority", async function () { let called = []; async function delayed(ret) { await (0, async_1.sleep)(25); called.push(ret); return ret; } const queue = new taskQueue_1.ParallelPriorityTaskQueue(); queue.maxParallel = 10; queue.usePriority = true; const promises = [ queue.execute(delayed, ["first"], 5), queue.execute(delayed, ["second"], 10), queue.execute(delayed, ["third"], 15), ]; const start = Date.now(); await Promise.all(promises); const diff = Date.now() - start; (0, chai_1.assert)(diff < 40, "Functions should be called parallel"); (0, chai_1.assert)(called[0] == "first", "First should be the first entry"); (0, chai_1.assert)(called[1] == "second", "second should be the second entry"); (0, chai_1.assert)(called[2] == "third", "third should be the third entry"); }); }); });