diff --git a/package.json b/package.json index e66111d..72850e6 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "compile": "npx tsc", "build": "npm run fix && npm run compile", "prepublish": "npm run build", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "node ./tests/test.js" }, "devDependencies": { "@types/express": "^4.17.17", diff --git a/tests/test.js b/tests/test.js new file mode 100644 index 0000000..31576e9 --- /dev/null +++ b/tests/test.js @@ -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); + } + } +})();