nope-js-node
Version:
NoPE Runtime for Nodejs. For Browser-Support please use nope-browser
117 lines (116 loc) • 4.47 kB
JavaScript
;
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @desc [description]
*/
Object.defineProperty(exports, "__esModule", { value: true });
const mocha_1 = require("mocha");
const async_1 = require("./async");
const limit_1 = require("./limit");
(0, mocha_1.describe)("limit", function () {
// Describe the required Test:
(0, mocha_1.describe)("limitedCalls", function () {
(0, mocha_1.it)("single-call - sync", async () => {
const f = (0, limit_1.limitedCalls)(async_1.sleep, {
maxParallel: 0,
});
const start = Date.now();
const promises = [f(100), f(100)];
await Promise.all(promises);
const end = Date.now();
if (end - start < 200) {
throw Error("Failed to call sync");
}
});
(0, mocha_1.it)("single-call - with locking", async () => {
const sleepExtended = (delay, opts) => {
return new Promise((resolve) => {
opts.pauseTask();
setTimeout(() => {
opts.continueTask();
resolve(null);
}, delay);
});
};
const f = (0, limit_1.limitedCalls)(sleepExtended, {
maxParallel: 0,
assignControlFunction(args, opts) {
args.push(opts);
return args;
},
});
const start = Date.now();
const promises = [f(100), f(100)];
await Promise.all(promises);
const end = Date.now();
if (end - start > 150) {
throw Error("Failed to call async");
}
});
(0, mocha_1.it)("single-call - parallel", async () => {
const f = (0, limit_1.limitedCalls)(async_1.sleep, {
maxParallel: 2,
});
const start = Date.now();
const promises = [f(100), f(100)];
await Promise.all(promises);
const end = Date.now();
if (end - start > 200) {
throw Error("Failed to call parallel");
}
});
(0, mocha_1.it)("single-call - between (sync)", async () => {
const f = (0, limit_1.limitedCalls)(async (...args) => { }, {
maxParallel: 0,
callbackBetween: () => (0, async_1.sleep)(50),
});
const start = Date.now();
const promises = [f(100), f(100)];
await Promise.all(promises);
const end = Date.now();
if (end - start < 50) {
throw Error("Failed to call callbackBetween");
}
});
(0, mocha_1.it)("single-call - between (parallel)", async () => {
const f = (0, limit_1.limitedCalls)(async (...args) => { }, {
maxParallel: 10,
callbackBetween: () => (0, async_1.sleep)(50),
});
const start = Date.now();
const promises = [f(100), f(100)];
await Promise.all(promises);
const end = Date.now();
if (end - start > 50) {
throw Error("Failed to call callbackBetween");
}
});
(0, mocha_1.it)("single-call - delay", async () => {
const f = (0, limit_1.limitedCalls)(async (...args) => { }, {
maxParallel: 0,
minDelay: 50,
});
const start = Date.now();
const promises = [f(100), f(100)];
await Promise.all(promises);
const end = Date.now();
if (end - start < 50) {
throw Error("Failed to call callbackBetween");
}
});
(0, mocha_1.it)("single-call - delay (parallel)", async () => {
const f = (0, limit_1.limitedCalls)(async (...args) => { }, {
maxParallel: 10,
minDelay: 50,
});
const start = Date.now();
const promises = [f(100), f(100)];
await Promise.all(promises);
const end = Date.now();
if (end - start < 50) {
throw Error("Failed to call callbackBetween");
}
});
});
});