Skip to content

Commit

Permalink
Fix #24 make susi-rali ESM compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
boly38 committed Jan 23, 2022
1 parent 8037f89 commit 9605982
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 59 deletions.
2 changes: 1 addition & 1 deletion lib/SusiRali.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ class SusiRali {

}

module.exports = SusiRali;
export default SusiRali;
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 7 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,23 @@
"version": "0.1.5",
"description": "super simple rate limiter nodejs",
"main": "./lib/SusiRali.js",
"type": "module",
"scripts": {
"preinstall": "npm-force-resolutions",
"manual": "mocha --timeout 120000 tests/manual/*.test.js",
"test": "mocha --timeout 120000 tests/*.test.js",
"testS": "mocha --timeout 120000 tests/susi-rali.test.js",
"testP": "mocha --timeout 120000 tests/susi-rali-parallel.test.js",
"cover": "nyc --reporter text --reporter cobertura --reporter html --reporter=lcov --lines 66 mocha --timeout 120000 --exit --unhandled-rejections=strict tests/*.test.js",
"ci-test": "echo linux ci-test&& nyc --reporter text --reporter cobertura --reporter html --reporter=lcov --lines 66 mocha --timeout 120000 --exit --unhandled-rejections=strict tests/*.test.js"
},
"private": false,
"author": "Boly38 <[email protected]>",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/creharmony/susi-rali.git"
},
"bugs": {
"url": "https://github.com/creharmony/susi-rali/issues"
},
"keywords": [
"node",
"rate",
"limit",
"javascript",
"ratelimiter"
],
"copyright": {
"title": "susi-rali",
"years": [
2021
],
"author": "Brice Vandeputte"
},
"repository": {"type": "git", "url": "https://github.com/creharmony/susi-rali.git"},
"bugs": { "url": "https://github.com/creharmony/susi-rali/issues"},
"keywords": ["node", "rate", "limit", "javascript", "ratelimiter"],
"copyright": { "title": "susi-rali", "years": [2021, 2022], "author": "Brice Vandeputte"},
"dependencies": {},
"devDependencies": {
"chai": "^4.3.4",
Expand Down
4 changes: 1 addition & 3 deletions tests/TestHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,4 @@ class TestHelper {
}
}

makeHelper = function() { return new TestHelper(); }

module.exports = makeHelper;
export default TestHelper;
46 changes: 26 additions & 20 deletions tests/susi-rali-parallel.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const assert = require('assert').strict;
const expect = require('chai').expect
const SusiRali = require('../lib/SusiRali.js');
const TestHelper = require('./TestHelper')();
import SusiRali from '../lib/SusiRali.js';
import TestHelperJs from './TestHelper.js';
import { strict as assert } from 'assert';
import chai from 'chai';
const should = chai.should;
const expect = chai.expect;
should();

const testHelper = new TestHelperJs();

const debugEnabled=true;
const warnEnabled=true;

Expand All @@ -11,21 +17,21 @@ describe("Test SusiRali parallel", function() {

async function testParallelPromise(batchCount, counterWindows) {
const start = new Date();
await TestHelper.countEachWindow(start, counterWindows, batchCount, susi);
await testHelper.countEachWindow(start, counterWindows, batchCount, susi);

console.log(`PARALLEL LAUNCH`)

var promises = [];
for (var k=0;k<batchCount;k++) {
const businessArg = `parallel${k}`;
promises.push(
susi.limitedCall(()=>TestHelper.businessCode(businessArg, 200)).then((result)=>console.log(">" + result))
susi.limitedCall(()=>testHelper.businessCode(businessArg, 200)).then((result)=>console.log(">" + result))
);
}
await Promise.allSettled(promises).catch(TestHelper._expectNoError);
await Promise.allSettled(promises).catch(testHelper._expectNoError);

const duration = new Date() - start;
console.log(`parallel duration: ${duration}ms ${TestHelper.nbDone} / ${TestHelper.nbDoneAll}`);
console.log(`parallel duration: ${duration}ms ${testHelper.nbDone} / ${testHelper.nbDoneAll}`);

return duration;
}
Expand All @@ -36,36 +42,36 @@ describe("Test SusiRali parallel", function() {
const maxProcessingPerWindow=9;
const nbEntries = 100;

TestHelper.reset();
testHelper.reset();

const susi = new SusiRali({windowsMs, maxQueryPerWindow, maxProcessingPerWindow, debugEnabled, warnEnabled});
var duration = await testParallelPromise(nbEntries, counterWindows);

expect(TestHelper.maxQ).to.be.lte(maxQueryPerWindow);
expect(TestHelper.maxP).to.be.lte(maxProcessingPerWindow);
await TestHelper.sleep(counterWindows*4);// end of counter must be reached to start next
expect(testHelper.maxQ).to.be.lte(maxQueryPerWindow);
expect(testHelper.maxP).to.be.lte(maxProcessingPerWindow);
await testHelper.sleep(counterWindows*4);// end of counter must be reached to start next
});


it("should rate limit parallel long queries 10/1000", async function() {

async function testParallelPromise(batchCount, counterWindows) {
const start = new Date();
await TestHelper.countEachWindow(start, counterWindows, batchCount, susi);
await testHelper.countEachWindow(start, counterWindows, batchCount, susi);

console.log(`PARALLEL LAUNCH`)

var promises = [];
for (var k=0;k<batchCount;k++) {
const businessArg = `parallel${k}`;
promises.push(
susi.limitedCall(()=>TestHelper.businessCode(businessArg, 200)).then((result)=>console.log(">" + result))
susi.limitedCall(()=>testHelper.businessCode(businessArg, 200)).then((result)=>console.log(">" + result))
);
}
await Promise.allSettled(promises).catch(TestHelper._expectNoError);
await Promise.allSettled(promises).catch(testHelper._expectNoError);

const duration = new Date() - start;
console.log(`parallel duration: ${duration}ms ${TestHelper.nbDone} / ${TestHelper.nbDoneAll}`);
console.log(`parallel duration: ${duration}ms ${testHelper.nbDone} / ${testHelper.nbDoneAll}`);

return duration;
}
Expand All @@ -76,14 +82,14 @@ describe("Test SusiRali parallel", function() {
const maxProcessingPerWindow=10;
const nbEntries = 20;

TestHelper.reset();
testHelper.reset();

const susi = new SusiRali({windowsMs, maxQueryPerWindow, maxProcessingPerWindow, debugEnabled, warnEnabled});
var duration = await testParallelPromise(nbEntries, counterWindows);

expect(TestHelper.maxQ).to.be.lte(maxQueryPerWindow);
expect(TestHelper.maxP).to.be.lte(maxProcessingPerWindow);
await TestHelper.sleep(counterWindows*4);// end of counter must be reached to start next
expect(testHelper.maxQ).to.be.lte(maxQueryPerWindow);
expect(testHelper.maxP).to.be.lte(maxProcessingPerWindow);
await testHelper.sleep(counterWindows*4);// end of counter must be reached to start next
});

});
31 changes: 18 additions & 13 deletions tests/susi-rali.test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
const assert = require('assert').strict;
const expect = require('chai').expect
const SusiRali = require('../lib/SusiRali.js');
const TestHelper = require('./TestHelper')();
import SusiRali from '../lib/SusiRali.js';
import TestHelperJs from './TestHelper.js';
import { strict as assert } from 'assert';
import chai from 'chai';
const should = chai.should;
const expect = chai.expect;
should();

const testHelper = new TestHelperJs();

describe("Test SusiRali pause and restart", function() {

it("should rate limit, sleep and rate limit again", async function() {

async function testPromise(batchCount, sleepTimeMs, counterWindows) {
const start = new Date();
await TestHelper.countEachWindow(start, counterWindows, batchCount*2, susi);
await testHelper.countEachWindow(start, counterWindows, batchCount*2, susi);

console.log(`FIRST LAUNCH`)
for (var i=0;i<batchCount;i++) {
await susi.limitedCall(() => TestHelper.businessCode("First"+i,0))
await susi.limitedCall(() => testHelper.businessCode("First"+i,0))
.then((result)=>console.log(">" + result));
}
var duration = new Date() - start;
console.log(`DD duration: ${duration}ms ${TestHelper.nbDone} / ${TestHelper.nbDoneAll}`);
await TestHelper.sleep(sleepTimeMs);
console.log(`DD duration: ${duration}ms ${testHelper.nbDone} / ${testHelper.nbDoneAll}`);
await testHelper.sleep(sleepTimeMs);

console.log(`SECOND LAUNCH`)
for (var j=0;j<batchCount;j++) {
await susi.limitedCall(() => TestHelper.businessCode("Second"+j,0))
await susi.limitedCall(() => testHelper.businessCode("Second"+j,0))
.then((result)=>console.log(">" + result));
}
var duration = new Date() - start;
Expand All @@ -38,15 +43,15 @@ describe("Test SusiRali pause and restart", function() {
const nbEntries = 50;
const sleepTimeMs = 2000;

TestHelper.reset();
testHelper.reset();

const susi = new SusiRali({windowsMs, maxQueryPerWindow, debugEnabled});
var duration = await testPromise(nbEntries, sleepTimeMs, counterWindows);

expect(TestHelper.maxQ).to.be.lt(maxQueryPerWindow);
expect(TestHelper.maxP).to.be.lt(maxQueryPerWindow);
expect(testHelper.maxQ).to.be.lt(maxQueryPerWindow);
expect(testHelper.maxP).to.be.lt(maxQueryPerWindow);

await TestHelper.sleep(counterWindows*4);// end of counter must be reached to start next
await testHelper.sleep(counterWindows*4);// end of counter must be reached to start next
});


Expand Down

0 comments on commit 9605982

Please sign in to comment.