mock-request: improve logging

attempt to make the logging a bit more comprehensible
pull/3956/head
Richard van der Hoff 2017-05-18 13:37:23 +01:00
parent a39a7c8334
commit f60773ae1f
1 changed files with 24 additions and 7 deletions

View File

@ -13,7 +13,7 @@ function HttpBackend() {
// the request function dependency that the SDK needs. // the request function dependency that the SDK needs.
this.requestFn = function(opts, callback) { this.requestFn = function(opts, callback) {
const req = new Request(opts, callback); const req = new Request(opts, callback);
console.log("HTTP backend received request: %s", req); console.log("HTTP backend received request: " + req);
self.requests.push(req); self.requests.push(req);
const abort = function() { const abort = function() {
@ -51,20 +51,23 @@ HttpBackend.prototype = {
waitTime = 5; waitTime = 5;
} }
console.log( console.log(
"HTTP backend flushing... (path=%s numToFlush=%s waitTime=%s)", "HTTP backend flushing... (path=" + path
path, numToFlush, waitTime, + " numToFlush=" + numToFlush
+ " waitTime=" + waitTime
+ ")",
); );
const tryFlush = function() { const tryFlush = function() {
// if there's more real requests and more expected requests, flush 'em. // if there's more real requests and more expected requests, flush 'em.
console.log( console.log(
" trying to flush queue => reqs=[%s] expected=[%s]", " trying to flush queue => reqs=[" + self.requests
self.requests, self.expectedRequests.map((er) => er.path), + "] expected=[" + self.expectedRequests
+ "]",
); );
if (self._takeFromQueue(path)) { if (self._takeFromQueue(path)) {
// try again on the next tick. // try again on the next tick.
flushed += 1; flushed += 1;
if (numToFlush && flushed === numToFlush) { if (numToFlush && flushed === numToFlush) {
console.log(" Flushed assigned amount: %s", numToFlush); console.log(" Flushed assigned amount:", numToFlush);
defer.resolve(flushed); defer.resolve(flushed);
} else { } else {
console.log(" flushed. Trying for more."); console.log(" flushed. Trying for more.");
@ -73,10 +76,20 @@ HttpBackend.prototype = {
} else if (flushed === 0 && !triedWaiting) { } else if (flushed === 0 && !triedWaiting) {
// we may not have made the request yet, wait a generous amount of // we may not have made the request yet, wait a generous amount of
// time before giving up. // time before giving up.
console.log(
" nothing to flush yet; waiting " + waitTime +
"ms for requests.")
setTimeout(tryFlush, waitTime); setTimeout(tryFlush, waitTime);
triedWaiting = true; triedWaiting = true;
} else { } else {
console.log(" no more flushes."); if (flushed === 0) {
console.log(" nothing to flush; giving up");
} else {
console.log(
" no more flushes after flushing", flushed,
"requests",
);
}
defer.resolve(flushed); defer.resolve(flushed);
} }
}; };
@ -197,6 +210,10 @@ function ExpectedRequest(method, path, data) {
} }
ExpectedRequest.prototype = { ExpectedRequest.prototype = {
toString: function() {
return this.method + " " + this.path
},
/** /**
* Execute a check when this request has been satisfied. * Execute a check when this request has been satisfied.
* @param {Function} fn The function to execute. * @param {Function} fn The function to execute.