Browse Source

debug push takes function & beginTimer returns Date

pull/946/head
Lily He 7 years ago
parent
commit
af03e706b2
  1. 43
      helper/debug.js
  2. 37
      test/unit/helper/debug.js

43
helper/debug.js

@ -6,38 +6,47 @@ class Debug {
this.name = moduleName || 'unnamed module'; this.name = moduleName || 'unnamed module';
} }
push(req, debugMsg){ push(req, value){
if (!_.isEmpty(req.clean) && req.clean.enableDebug){ if (!req || _.isEmpty(req.clean) || !req.clean.enableDebug){
return;
}
req.debug = req.debug || []; req.debug = req.debug || [];
// remove the extra space character switch(typeof value) {
req.debug.push({[this.name]: debugMsg}); case 'function':
// req.debug.push(`[${this.name}] ${debugMsg}`); req.debug.push({[this.name]: value()});
break;
default:
req.debug.push({[this.name]: value});
} }
} }
// optional debugMsg passed to timer
beginTimer(req, debugMsg){ beginTimer(req, debugMsg){
if (!_.isEmpty(req.clean) && req.clean.enableDebug){ if (req && !_.isEmpty(req.clean) && req.clean.enableDebug){
// internal object debugTimers. Doesn't get displayed in geocodeJSON // debugMsg is optional
req.debugTimers = req.debugTimers || {}; this.push(req, () => {
req.debugTimers[this.name] = Date.now();
if (debugMsg){ if (debugMsg){
this.push(req, `Timer Began: ${debugMsg}`); return `Timer Began. ${debugMsg}`;
} else { } else {
this.push(req, `Timer Began`); return `Timer Began`;
} }
});
return Date.now();
} }
} }
stopTimer(req, debugMsg){ stopTimer(req, startTime, debugMsg){
if (!_.isEmpty(req.clean) && req.clean.enableDebug){ if (req && !_.isEmpty(req.clean) && req.clean.enableDebug){
let timeElapsed = Date.now() - req.debugTimers[this.name]; let timeElapsed = Date.now() - startTime;
this.push(req, () => {
if (debugMsg){ if (debugMsg){
this.push(req, `Timer Stopped: ${timeElapsed} ms: ${debugMsg}`); return `Timer Stopped. ${timeElapsed} ms. ${debugMsg}`;
} else { } else {
this.push(req, `Timer Stopped: ${timeElapsed} ms`); return `Timer Stopped. ${timeElapsed} ms`;
} }
});
} }
} }
} }
module.exports = Debug; module.exports = Debug;

37
test/unit/helper/debug.js

@ -50,35 +50,50 @@ module.exports.tests.debug = function(test, common) {
enableDebug: true enableDebug: true
} }
}; };
const expected_req = { const expected_req = [
debug: [
{ {
debugger: 'This should be pushed' debugger: 'This should be pushed'
}, },
{ {
debugger: 'Timer Began: Timer 1' debugger: 'Timer Began. Timer 1'
} }
] ];
};
debugLog.push(req, 'This should be pushed'); debugLog.push(req, 'This should be pushed');
debugLog.beginTimer(req, 'Timer 1'); debugLog.beginTimer(req, 'Timer 1');
t.deepEquals(req.debug, expected_req);
t.end(); t.end();
}); });
test('Timer should return positive number of milliseconds', (t) => { test('Push messages can take output of function', (t) => {
const debugLog = new Debug('debugger'); const debugLog = new Debug('debugger');
const req = { const req = {
clean: { clean: {
enableDebug: true enableDebug: true
} }
}; };
debugLog.beginTimer(req); const expected_req = [
setTimeout(() => { {
debugLog.stopTimer(req); debugger: 'This should be pushed'
t.deepEquals(parseInt(req.debug[1].debugger.slice(15, -3)) > 0, true); }
];
debugLog.push(req, () => ('This should be pushed'));
t.deepEquals(req.debug, expected_req);
t.end(); t.end();
}, 2); });
test('Timer should return number of milliseconds', (t) => {
const debugLog = new Debug('debugger');
const req = {
clean: {
enableDebug: true
}
};
const timer = debugLog.beginTimer(req);
debugLog.stopTimer(req, timer);
// Checks that there is a debug message
// that matches the pattern "Timer Stopped. [number] ms"
t.deepEquals(req.debug[1].debugger.match(/Timer Stopped\. \d+ ms/i).length, 1);
t.end();
}); });
}; };

Loading…
Cancel
Save