fixed timeout issue

implemented timeoutManager, a more efficient solution for managing timeouts in RAM memory. this resolves the problem of lingering timeouts in the server, providing faster removal of old timeouts and improving overall performance.
This commit is contained in:
Wenszel
2023-12-20 10:17:49 +01:00
parent f07c1c8577
commit e5a69fa4a9
8 changed files with 381 additions and 28 deletions
@@ -0,0 +1,50 @@
const chai = require('chai');
const sinon = require('sinon');
const timeoutManager = require('../../models/timeoutManager');
const { expect } = chai;
describe('timeoutManager', () => {
beforeEach(() => {
timeoutManager.timeouts.clear();
sinon.useFakeTimers({ shouldClearNativeTimers: true });
});
afterEach(() => {
sinon.restore();
});
it('should add a timeout to the map', () => {
timeoutManager.add('room1', 1);
expect(timeoutManager.timeouts.size).to.equal(1);
});
it('should get a timeout from the map', () => {
timeoutManager.add('room1', 1);
const timeoutId = timeoutManager.get('room1');
expect(timeoutId).to.equal(1);
});
it('should clear a timeout from the map', () => {
timeoutManager.add('room1', 1);
timeoutManager.clear('room1');
expect(timeoutManager.timeouts.size).to.equal(0);
});
it('should set a new timeout', () => {
const timeoutFunction = sinon.spy();
timeoutManager.set(timeoutFunction, 100, 'room1');
expect(timeoutManager.timeouts.size).to.equal(1);
sinon.clock.tick(101);
sinon.assert.calledOnce(timeoutFunction);
});
it('should not call the timeout function if cleared', () => {
const timeoutFunction = sinon.spy();
timeoutManager.set(timeoutFunction, 100, 'room1');
timeoutManager.clear('room1');
sinon.clock.tick(101);
sinon.assert.notCalled(timeoutFunction);
expect(timeoutManager.timeouts.size).to.equal(0);
});
});