ludo-web-multiplayer/backend/tests/models/timeoutManager.test.js
Wenszel e5a69fa4a9 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.
2023-12-20 10:31:09 +01:00

51 lines
1.5 KiB
JavaScript

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);
});
});