specs for timer

This commit is contained in:
AlecM33
2022-02-26 03:43:15 -05:00
parent 1cacaf511f
commit 6d5b74d9d9
3 changed files with 63 additions and 7 deletions

View File

@@ -66,6 +66,7 @@ class ServerTimer {
}
stopTimer () {
this.logger.debug('STOPPING TIMER');
if (this.ticking) {
clearTimeout(this.ticking);
}

View File

@@ -7,10 +7,10 @@ const GameManager = require('../../../../server/modules/GameManager.js');
const GameStateCurator = require('../../../../server/modules/GameStateCurator');
const logger = require('../../../../server/modules/Logger.js')(false);
describe('GameManager', function () {
describe('GameManager', () => {
let gameManager, namespace;
beforeAll(function () {
beforeAll(() => {
spyOn(logger, 'debug');
spyOn(logger, 'error');
gameManager = new GameManager(logger, globals.ENVIRONMENT.PRODUCTION).getInstance();
@@ -19,10 +19,10 @@ describe('GameManager', function () {
gameManager.namespace = namespace;
});
beforeEach(function () {
beforeEach(() => {
});
describe('#transferModerator', function () {
describe('#transferModerator', () => {
it('Should transfer successfully from a dedicated moderator to a killed player', () => {
const personToTransferTo = new Person('1', '123', 'Joe', USER_TYPES.KILLED_PLAYER);
personToTransferTo.out = true;
@@ -100,7 +100,7 @@ describe('GameManager', function () {
});
});
describe('#killPlayer', function () {
describe('#killPlayer', () => {
it('Should mark a player as out and broadcast it, and should not transfer moderators if the moderator is a dedicated mod.', () => {
spyOn(namespace.in(), 'emit');
spyOn(gameManager, 'transferModeratorPowers');
@@ -142,7 +142,7 @@ describe('GameManager', function () {
});
});
describe('#handleRequestForGameState', function () {
describe('#handleRequestForGameState', () => {
it('should send the game state to a matching person with an active connection to the room', () => {
const player = new Person('1', '123', 'Joe', USER_TYPES.PLAYER);
const socket = { id: 'socket1' };
@@ -211,7 +211,7 @@ describe('GameManager', function () {
});
});
describe('#joinGame', function () {
describe('#joinGame', () => {
it('should mark the game as full when all players have been assigned', () => {
const person = new Person('1', '123', 'Placeholder', USER_TYPES.KILLED_PLAYER);
const moderator = new Person('3', '789', 'Jack', USER_TYPES.MODERATOR);

View File

@@ -0,0 +1,55 @@
// TODO: clean up these deep relative paths? jsconfig.json is not working...
const LOG_LEVEL = require("../../../../server/config/globals.js").LOG_LEVEL;
const ServerTimer = require('../../../../server/modules/ServerTimer.js');
const logger = require('../../../../server/modules/Logger.js')(LOG_LEVEL.DEBUG);
describe('ServerTimer', () => {
let serverTimer;
it ('should run a timer for the specified time', async () => {
serverTimer = new ServerTimer(0, 0.001, 10, logger);
spyOn(global, 'clearTimeout');
await serverTimer.runTimer(false).then(() => {
expect(serverTimer.currentTimeInMillis).toBeLessThanOrEqual(0);
expect(clearTimeout).toHaveBeenCalledWith(serverTimer.ticking);
}).catch((e) => {
fail(e);
});
});
it ('should stop the timer', () => {
serverTimer = new ServerTimer(1, 0, 10, logger);
spyOn(global, 'clearTimeout');
serverTimer.runTimer(false).then(() => {
fail();
}).catch((e) => {
fail(e);
});
serverTimer.stopTimer();
expect(clearTimeout).toHaveBeenCalledWith(serverTimer.ticking);
});
it ('should stop and resume the timer', async () => {
serverTimer = new ServerTimer(0, 1, 10, logger);
spyOn(global, 'clearTimeout');
serverTimer.runTimer(false).then(() => {
fail();
}).catch((e) => {
fail(e);
});
await new Promise(function (resolve) {
setTimeout(function () {
serverTimer.stopTimer();
let timeRemainingWhenStopped = serverTimer.currentTimeInMillis;
serverTimer.resumeTimer();
expect(clearTimeout).toHaveBeenCalledTimes(1);
expect(serverTimer.totalTime).toEqual(timeRemainingWhenStopped);
serverTimer.stopTimer();
resolve();
}, 50);
});
});
});