Skip to content

Commit

Permalink
[INTERNAL] TestRunner: throttle sending of XHR requests
Browse files Browse the repository at this point in the history
Change-Id: I9d1ffd976f894cf6fe0b7edbf398f2a62e7e71c4
  • Loading branch information
codeworrior authored and mmilko01 committed Jul 20, 2020
1 parent fba693b commit 454a282
Showing 1 changed file with 68 additions and 13 deletions.
81 changes: 68 additions & 13 deletions src/sap.ui.core/test/sap/ui/qunit/TestRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,52 @@
this.aPages.push(sTestPage);
};

function XHRQueue(iMaxParallelRequests, iWaitTime) {
this.iLimit = iMaxParallelRequests === undefined ? Infinity : iMaxParallelRequests;
this.iWaitTime = iWaitTime === undefined ? 0 : iWaitTime;
this.aPendingTasks = [];
this.oRunningTasks = new Set();
this.iLastTaskExecution = -Infinity;
}

XHRQueue.prototype.ajax = function(sUrl, options) {
var oTask = {
url: sUrl,
options: options
};
oTask.promise = new Promise(function(resolve, reject) {
oTask.resolve = resolve;
oTask.reject = reject;
});
this.aPendingTasks.push(oTask);
this._processNext();
return oTask.promise;
};

XHRQueue.prototype._processNext = function() {
var iNow = Date.now();
var iDelay = iNow - this.iLastTaskExecution;
if ( iDelay < this.iWaitTime ) {
setTimeout(function() {
this._processNext();
}.bind(this), iDelay);
return;
}
if ( this.aPendingTasks.length > 0 && this.oRunningTasks.size < this.iLimit ) {
var oTask = this.aPendingTasks.shift();
this.oRunningTasks.add(oTask);
this.iLastTaskExecution = iNow;
Promise.resolve(jQuery.ajax(oTask.url, oTask.options))
.then(oTask.resolve, oTask.reject)
.finally(function() {
this.oRunningTasks.delete(oTask);
this._processNext();
}.bind(this));
}
};

var oXHRQueue = new XHRQueue(50, 2);

/*
* Template for test results
*/
Expand Down Expand Up @@ -62,6 +108,21 @@
window.sap.ui.qunit.TestRunner = {

checkTestPage: function(sTestPage, bSequential) {
var t0 = Date.now();
var oPromise =
this._checkTestPage(sTestPage, bSequential)
.then(function(aTestPages) {
var t1 = Date.now();
window.console.log("[DEBUG] checkTestPage(\"" + sTestPage + "\") found " + aTestPages.length + " pages in " + (t1 - t0) + "msec.");
window.console.log("[DEBUG] checkTestPage(\"" + sTestPage + "\") currently running IFrames: " + window.frames.length);
return aTestPages;
});
oPromise.done = oPromise.then; // compat for Deferred
oPromise.fail = oPromise.catch; // compat for Deferred
return oPromise;
},

_checkTestPage: function(sTestPage, bSequential) {

var oPromise = new Promise(function(resolve, reject) {

Expand All @@ -70,25 +131,22 @@
reject("QUnit: invalid test page specified");
}

/*
if (window.console && typeof window.console.log === "function") {
window.console.log("QUnit: checking test page: " + sTestPage);
}
}*/

// check for an existing test page and check for test suite or page
jQuery.get(sTestPage).done(function(sData) {
oXHRQueue.ajax(sTestPage).then(function(sData) {
if (/(?:window\.suite\s*=|function\s*suite\s*\(\s*\)\s*{)/.test(sData)
|| (/data-sap-ui-testsuite/.test(sData) && !/sap\/ui\/test\/starter\/runTest/.test(sData)) ) {
window.console.log("[DEBUG] checkTestPage checking testsuite page: " + sTestPage);
// window.console.log("[DEBUG] _checkTestPage checking testsuite page: " + sTestPage);
var $frame = jQuery("<iframe>");
var that = this;

var onSuiteReady = function(oIFrame) {
that.findTestPages(oIFrame, bSequential).then(function(aTestPages) {
var aTestPagesFiltered = aTestPages.filter(function(e, i, a) { return a.indexOf(e) === i; });
if (oIFrame.src.indexOf("test-resources/qunit/testsuite.qunit.html") > -1) {
window.console.log("[DEBUG] findTestPages oIFrame source " + oIFrame.src + " found pages " + aTestPagesFiltered.length);
window.console.log("[DEBUG] findTestPages oIFrame source " + oIFrame.src + " currently running IFrames " + window.frames.length);
}
$frame.remove();
// avoid duplicates in test pages
resolve(aTestPagesFiltered);
Expand Down Expand Up @@ -117,7 +175,7 @@
} else {
resolve([sTestPage]);
}
}.bind(this)).fail(function(xhr,status,msg) {
}.bind(this)).catch(function(xhr,status,msg) {
var text = (xhr ? xhr.status + " " : "") + (msg || status || 'unspecified error');
if (window.console && typeof window.console.error === "function") {
window.console.error("QUnit: failed to load page '" + sTestPage + "': " + text);
Expand All @@ -129,9 +187,6 @@

}.bind(this));

oPromise.done = oPromise.then; // compat for Deferred
oPromise.fail = oPromise.catch; // compat for Deferred

return oPromise;

},
Expand All @@ -156,7 +211,7 @@

var aTestPages = [];
aTestPagePromises.push(aPages.reduce(function(oPromise, sTestPage) {
return oPromise.then(this.checkTestPage.bind(this, sTestPage, bSequential)).then(function(aFoundTestPages) {
return oPromise.then(this._checkTestPage.bind(this, sTestPage, bSequential)).then(function(aFoundTestPages) {
aTestPages = aTestPages.concat(aFoundTestPages);
});
}.bind(this), Promise.resolve([])).then(function() {
Expand All @@ -167,7 +222,7 @@

for (var i = 0, l = aPages.length; i < l; i++) {
var sTestPage = aPages[i];
aTestPagePromises.push(this.checkTestPage(sTestPage, bSequential));
aTestPagePromises.push(this._checkTestPage(sTestPage, bSequential));
}

}
Expand Down

0 comments on commit 454a282

Please sign in to comment.