First commit

This commit is contained in:
2025-12-25 11:16:59 +01:00
commit 0c5ca09a63
720 changed files with 329234 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
const { describe, it, beforeEach } = require('mocha');
const { expect } = require('chai');
const BlockedKeys = require('../../../lib/component/BlockedKeys/BlockedKeys');
describe('BlockedKeys', () => {
let blockedKeys;
beforeEach(() => {
blockedKeys = new BlockedKeys();
});
it('add blocked key', () => {
blockedKeys.add('key', 5);
blockedKeys.collectExpired();
expect(blockedKeys.msBeforeExpire('key') > 0).to.equal(true);
});
it('expire blocked key', (done) => {
blockedKeys.add('key', 1);
setTimeout(() => {
expect(blockedKeys.msBeforeExpire('key')).to.equal(0);
done();
}, 1001);
});
it('check not blocked key', () => {
blockedKeys.add('key', 1);
expect(blockedKeys.msBeforeExpire('key1')).to.equal(0);
});
it('do not collect expired on add', (done) => {
blockedKeys.add('key', 1);
blockedKeys.add('key1', 1);
setTimeout(() => {
blockedKeys.add('key2', 1);
expect(Object.keys(blockedKeys._keys).length).to.equal(3);
done();
}, 1001);
});
it('collect expired on add if there more than 999 blocked keys', (done) => {
for (let i = 0; i < 1000; i++) {
blockedKeys.add(`key${i}`, 1);
}
setTimeout(() => {
blockedKeys.add('key1', 1);
expect(Object.keys(blockedKeys._keys).length === 1 && blockedKeys._addedKeysAmount === 1)
.to.equal(true);
done();
}, 1001);
});
it('do not collect expired when key is not blocked', (done) => {
blockedKeys.add('key', 1);
setTimeout(() => {
blockedKeys.msBeforeExpire('key');
expect(Object.keys(blockedKeys._keys).length === 1 && blockedKeys._addedKeysAmount === 1)
.to.equal(true);
done();
}, 1001);
});
it('collect expired when key is blocked', (done) => {
blockedKeys.add('key', 1);
blockedKeys.add('blocked', 2);
setTimeout(() => {
blockedKeys.msBeforeExpire('blocked');
expect(Object.keys(blockedKeys._keys).length).to.equal(1);
done();
}, 1001);
});
it('duplicated keys do not brake collectExpired and msBeforeExpire', (done) => {
blockedKeys.add('key', 1);
blockedKeys.add('key', 2);
setTimeout(() => {
blockedKeys.add('key', 3);
expect(blockedKeys.msBeforeExpire('key') > 2000).to.equal(true);
done();
}, 1001);
});
});

View File

@@ -0,0 +1,79 @@
const { describe, it, beforeEach } = require('mocha');
const { expect } = require('chai');
const MemoryStorage = require('../../../lib/component/MemoryStorage/MemoryStorage');
describe('MemoryStorage', function MemoryStorageTest() {
const testKey = 'test';
const val = 34;
let storage;
this.timeout(5000);
beforeEach(() => {
storage = new MemoryStorage();
});
it('should set and get', (done) => {
storage.set(testKey, val, 5);
expect(storage.get(testKey).consumedPoints).to.equal(val);
done();
});
it('should delete record on expire', (done) => {
storage.set(testKey, val, 1);
setTimeout(() => {
expect(storage.get(testKey)).to.equal(null);
done();
}, 2000);
});
it('should incrby', (done) => {
storage.set(testKey, val, 5);
storage.incrby(testKey, 2);
expect(storage.get(testKey).consumedPoints).to.equal(val + 2);
done();
});
it('incrby should create record if it is not set', (done) => {
storage.incrby(testKey, val, 5);
expect(storage.get(testKey).consumedPoints).to.equal(val);
done();
});
it('incrby should create record if expiresAt is not set', (done) => {
storage.set(testKey, val)
expect(storage.get(testKey).expiresAt).to.equal(undefined);
storage.incrby(testKey, val, 5);
expect(storage.get(testKey).expiresAt !== null).to.equal(true);
done();
});
it('should delete record and return true, if it was there', () => {
storage.set(testKey, val, 10);
expect(storage.delete(testKey)).to.equal(true);
expect(storage.get(testKey)).to.equal(null);
});
it('return false, if there is no record to delete', () => {
expect(storage.delete(testKey)).to.equal(false);
});
it('should not fail in the absence of Timeout::unref', (done) => {
// Node (where we most likely be running tests) provides `Timeout.prototype.unref`, however
// MemoryStorage should run in environments where `Timeout.prototype.unref` is not provided
// (e.g. browsers). For this test we remove `unref` from `Timeout.prototype` only for the
// duration of this test, to verify that MemoryStorage.prototype.set won't throw.
const handle = setTimeout(() => {}, 0);
const isHandleObject = typeof handle === 'object' && !!handle.constructor;
let timeoutUnref;
if (isHandleObject) {
timeoutUnref = handle.constructor.prototype.unref;
delete handle.constructor.prototype.unref;
}
expect(() => new MemoryStorage().set('key', 0, 0.001)).to.not.throw();
setTimeout(done, 250);
if (isHandleObject) {
handle.constructor.prototype.unref = timeoutUnref;
}
});
});

View File

@@ -0,0 +1,21 @@
const { describe, it, beforeEach } = require('mocha');
const { expect } = require('chai');
const Record = require('../../../lib/component/MemoryStorage/Record');
describe('MemoryStorage Record', () => {
let record;
beforeEach(() => {
record = new Record();
});
it('value set with cast to int and get', () => {
record.value = '123';
expect(record.value).to.equal(123);
});
it('expiresAt set unix time and get Date', () => {
const now = Date.now();
record.expiresAt = now;
expect(record.expiresAt.getTime()).to.equal(now);
});
});

View File

@@ -0,0 +1,11 @@
const { describe, it } = require('mocha');
const { expect } = require('chai');
const RateLimiterQueueError = require('../../lib/component/RateLimiterQueueError');
describe('RateLimiterQueueError', () => {
it('supports extra argument in constructor', (done) => {
const err = new RateLimiterQueueError('test', 'extra');
expect(err.extra).to.equal('extra');
done();
});
});