mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-12 07:04:52 +09:00
Migrate the usage of CommonJS specific code and imports to ESM. Roughly summarised this: - Changes all require() calls to import statement or expressions. - Changes usage of module.exports to export statements. - Replaces require.resolve with import.meta.resolve. - Replaces conditional imports with conditional subpath imports. - Replaces module === require.main with import.meta.main. - Adds the full filename with extention to all relative imports. - Changes all usage of __dirname and __filename to their import.meta equivalents. - Sets "type": "module" in package.json files. - Removes usage of "use strict". - Adds .ts an an alias of .js for the webpack build. Due to differences in how require and import resolves files the plugin entrypoints will have to include the filename and extension after this migration in order to work.
63 lines
1.9 KiB
JavaScript
63 lines
1.9 KiB
JavaScript
import assert from "node:assert/strict";
|
|
|
|
import { RateLimiter, wait } from "@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");
|
|
});
|
|
});
|
|
});
|