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.
This commit is contained in:
Michael Fabian 'Xaymar' Dirks
2023-03-19 01:51:44 +01:00
parent 4db9ab23eb
commit 28c96ba9b9
2 changed files with 57 additions and 1 deletions
+1 -1
View File
@@ -16,7 +16,7 @@
"compile": "npx tsc", "compile": "npx tsc",
"build": "npm run fix && npm run compile", "build": "npm run fix && npm run compile",
"prepublish": "npm run build", "prepublish": "npm run build",
"test": "echo \"Error: no test specified\" && exit 1" "test": "node ./tests/test.js"
}, },
"devDependencies": { "devDependencies": {
"@types/express": "^4.17.17", "@types/express": "^4.17.17",
+56
View File
@@ -0,0 +1,56 @@
// 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);
}
}
})();