mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-12 07:04:52 +09:00
Move all functions and classes currently available through lib/name and libName to be under a single flat namespace in lib. This make reorganising the lib namespace much simpler as the location of symbols can move without breaking the consumers of lib. The multi-entrypoint design was already problematic for the web plugin system which couldn't effectively use it due to how webpack's shared module system is awkward to set up with multiple entrypoints per package.
64 lines
1.9 KiB
JavaScript
64 lines
1.9 KiB
JavaScript
"use strict";
|
|
const assert = require("assert").strict;
|
|
|
|
const { RateLimiter, wait } = require("@clusterio/lib");
|
|
|
|
|
|
describe("lib/RateLimiter", function() {
|
|
describe("class RateLimiter", function() {
|
|
let rateLimiter;
|
|
beforeEach(function() {
|
|
rateLimiter = new RateLimiter();
|
|
});
|
|
afterEach(function() {
|
|
rateLimiter.cancel();
|
|
});
|
|
|
|
it("should rate limit activations", function() {
|
|
assert(rateLimiter.activate(), "fist invocation was rate limited");
|
|
assert(!rateLimiter.activate(), "second invocation was not rate limited");
|
|
});
|
|
|
|
it("should invoke action immediately when not rate limited", function() {
|
|
let invoked = false;
|
|
rateLimiter.action = () => { invoked = true; };
|
|
rateLimiter.activate();
|
|
assert(invoked, "action was not invoked");
|
|
});
|
|
|
|
it("should invoke action at a later point in time if rate limited", async function() {
|
|
let invokedCount = 0;
|
|
rateLimiter.maxRate = 100;
|
|
rateLimiter.action = () => { invokedCount += 1; };
|
|
rateLimiter.activate();
|
|
rateLimiter.activate();
|
|
assert.equal(invokedCount, 1, "expected one invocation");
|
|
await wait(20);
|
|
assert.equal(invokedCount, 2, "expected two invocations");
|
|
});
|
|
|
|
it("should invoke action before timeout if maxRate changed", async function() {
|
|
let invokedCount = 0;
|
|
rateLimiter.action = () => { invokedCount += 1; };
|
|
rateLimiter.activate();
|
|
rateLimiter.activate();
|
|
assert.equal(invokedCount, 1, "expected one invocation");
|
|
await wait(20);
|
|
rateLimiter.maxRate = 100;
|
|
rateLimiter.activate();
|
|
assert.equal(invokedCount, 2, "expected two invocations");
|
|
});
|
|
|
|
it("should cancel action if canceled before timeout", async function() {
|
|
let invokedCount = 0;
|
|
rateLimiter.action = () => { invokedCount += 1; };
|
|
rateLimiter.activate();
|
|
rateLimiter.activate();
|
|
rateLimiter.cancel();
|
|
assert.equal(invokedCount, 1, "expected one invocation");
|
|
await wait(20);
|
|
assert.equal(invokedCount, 1, "expected one invocation");
|
|
});
|
|
});
|
|
});
|