From 454a28274b9b409981fa0ee17021202d3ec29dd1 Mon Sep 17 00:00:00 2001 From: Frank Weigel Date: Thu, 9 Jul 2020 15:33:58 +0200 Subject: [PATCH] [INTERNAL] TestRunner: throttle sending of XHR requests Change-Id: I9d1ffd976f894cf6fe0b7edbf398f2a62e7e71c4 --- .../test/sap/ui/qunit/TestRunner.js | 81 ++++++++++++++++--- 1 file changed, 68 insertions(+), 13 deletions(-) diff --git a/src/sap.ui.core/test/sap/ui/qunit/TestRunner.js b/src/sap.ui.core/test/sap/ui/qunit/TestRunner.js index 55f6e9ea59b5..b99544435ca7 100644 --- a/src/sap.ui.core/test/sap/ui/qunit/TestRunner.js +++ b/src/sap.ui.core/test/sap/ui/qunit/TestRunner.js @@ -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 */ @@ -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) { @@ -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("