Files
JS-RateLimiter/tests/test.js
T
Michael Fabian 'Xaymar' Dirks 28c96ba9b9 Add some basic tests
Not much else can be tested here. This checks that things are being run, and that their return values are being returned. Not sure how we would verify that the rate limit is being enforced yet.
2023-03-19 01:51:44 +01:00

57 lines
1.2 KiB
JavaScript

// May require `npm link`.
const { RateLimiter } = require("../generated/ratelimiter");
async function asyncRunTest(fn, ...args) {
try {
await fn(...args);
console.log(`${fn.name}(${args})`)
} catch (ex) {
console.log(`${fn.name}(${args})`)
console.error(ex);
process.exitCode++;
}
}
function runTest(fn, ...args) {
try {
fn(...args);
console.log(`${fn.name}(${args})`);
} catch (ex) {
console.log(`${fn.name}(${args})`);
console.error(ex);
process.exitCode++;
}
}
function test_Construct(limit) {
let rl = new RateLimiter(limit);
}
async function test_TaskLimit(tasks, limit) {
let rl = new RateLimiter(limit);
let p = new Array();
for (let idx = 0; idx < tasks; idx++) {
p.push(rl.queue(() => {
return true;
}));
}
let r = await Promise.all(p);
for (res of r) {
if (res !== true) {
throw new Error("Result is unexpected.");
}
}
}
(async function() {
process.exitCode = 0;
for (let limit = 1; limit <= 64; limit *= 2) {
runTest(test_Construct, limit);
}
for (let limit = 1; limit <= 10; limit++) {
for (let tasks = 1; tasks <= 64; tasks *= 2) {
await asyncRunTest(test_TaskLimit, tasks, limit);
}
}
})();