Skip to content

Commit

Permalink
Last log line should be parsed as int (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
niekcandaele committed Jul 1, 2020
1 parent 12dbf4d commit 7dd47a4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
2 changes: 1 addition & 1 deletion api/hooks/sdtdLogs/LoggingObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class LoggingObject extends EventEmitter {
if (!this.slowmode) {
sails.log.info(
`SdtdLogs - Server ${
this.serverId
this.serverId
} has failed ${counter} times. Changing interval time. Server was last successful on ${prettyLastSuccess.toLocaleDateString()} ${prettyLastSuccess.toLocaleTimeString()}`
);
this.slowmode = true;
Expand Down
3 changes: 2 additions & 1 deletion api/hooks/sdtdLogs/logProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const handleLogLine = require("./handleLogLine");

module.exports = async function (job) {
const resultLogs = [];
let lastLogLine = job.data.lastLogLine;
let lastLogLine = parseInt(job.data.lastLogLine, 10);

// If latest log line is not found, get it from the server
if (!lastLogLine) {
const webUIUpdate = await sails.helpers.sdtdApi.getWebUIUpdates(SdtdServer.getAPIConfig(job.data.server));
Expand Down
43 changes: 43 additions & 0 deletions test/integration/hooks/sdtdLogs/logProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,49 @@ const expect = require("chai").expect;
const logProcessor = require("../../../../api/hooks/sdtdLogs/logProcessor");

describe('logProcessor', function () {

describe('It resets lastLogLine if job.data.lastLogLine is falsey', function () {
beforeEach(function () {
sails.helpers.sdtdApi.getWebUIUpdates = sandbox.stub().returns({ newlogs: 2 });
sails.helpers.sdtdApi.getLog = sandbox.stub().returns({
entries: [
{
date: '2020-07-01',
time: '23:58:38',
uptime: '127.041',
msg: "Executing command 'say test' by WebCommandResult_for_say",
trace: '',
type: 'Log'
},
{
date: '2020-07-01',
time: '23:58:38',
uptime: '127.065',
msg: "Chat (from '-non-player-', entity id '-1', to 'Global'): 'Server': test",
trace: '',
type: 'Log'
}
],
lastLine: 5
});

})

it('resets lastLogLine if job.data.lastLogLine is 0', async function () {
const result = await logProcessor({ data: { lastLogLine: 0, server: sails.testServer } });
expect(sails.helpers.sdtdApi.getWebUIUpdates).to.have.been.callCount(1);
expect(result.serverId).to.eq(sails.testServer.id);
expect(result.lastLogLine).to.eq(5);
});

it('resets lastLogLine if job.data.lastLogLine is "0"', async function () {
const result = await logProcessor({ data: { lastLogLine: "0", server: sails.testServer } });
expect(sails.helpers.sdtdApi.getWebUIUpdates).to.have.been.callCount(1);
expect(result.serverId).to.eq(sails.testServer.id);
expect(result.lastLogLine).to.eq(5);
});
})

it('Confirm able to fetch log messages', async function () {
sandbox.stub(sails.helpers.sdtdApi, "getWebUIUpdates").callsFake(async function () {
return {
Expand Down
22 changes: 13 additions & 9 deletions test/lifecycle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ process.env.CSMM_DONATOR_TIER = 'patron';
delete process.env.REDISSTRING;
delete process.env.PORT;

beforeEach(function() {
beforeEach(function () {
MockDate.set('2020-05-01T01:20:05+0000');
});
before(() => {
Expand All @@ -31,13 +31,13 @@ beforeEach(() => {
// Before running any tests...
before(function (done) {

async function onComplete (err) {
async function onComplete(err) {
if (err) {
throw err;
}

let testUser = await User.create({
steamId: faker.random.number({min: 0, max: 9999999999999}),
steamId: faker.random.number({ min: 0, max: 9999999999999 }),
username: faker.internet.userName()
}).fetch();

Expand Down Expand Up @@ -84,6 +84,10 @@ before(function (done) {
csrf: false
},


port: process.env.CSMM_PORT || 8500,


datastores: {
default: {
adapter: 'sails-disk',
Expand All @@ -110,17 +114,17 @@ after(function (done) {
sails.lower(done);
});

beforeEach(function(done) {
beforeEach(function (done) {
destroyFuncs = [];
for (modelName in sails.models) {
destroyFuncs.push(function(callback) {
destroyFuncs.push(function (callback) {
sails.models[modelName].destroy({})
.exec(function(err) {
callback(null, err)
});
.exec(function (err) {
callback(null, err)
});
})
}
async.parallel(destroyFuncs, function(err, results) {
async.parallel(destroyFuncs, function (err, results) {
done(err);
})
});

0 comments on commit 7dd47a4

Please sign in to comment.