diff --git a/package.json b/package.json index c43672b..9575bbd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "speed-testjs", - "version": "1.0.27", + "version": "1.0.28", "description": "measure internet bandwidth", "main": "index.js", "author": "Maulan Byron", diff --git a/public/examples/download/downloadApp.js b/public/examples/download/downloadApp.js index f45db90..b5dd89f 100644 --- a/public/examples/download/downloadApp.js +++ b/public/examples/download/downloadApp.js @@ -178,7 +178,7 @@ startTestButton.innerHTML = 'Testing in Progress ...'; //disable button startTestButton.disabled = true; - //set accessiblity aria-disabled state. + //set accessiblity aria-disabled state. //This will also effect the visual look by corresponding css startTestButton.setAttribute('aria-disabled', true); } @@ -217,7 +217,7 @@ startTestButton.innerHTML = 'Start Test'; option.series[0].data[0].value = 0; option.series[0].data[0].name = 'Test Complete'; - //set accessiblity aria-disabled state. + //set accessiblity aria-disabled state. //This will also effect the visual look by corresponding css startTestButton.setAttribute('aria-disabled', false); startTestButton.disabled = false; @@ -248,14 +248,14 @@ option.series[0].data[0].value = 0; //updat test status to complete option.series[0].data[0].name = 'Test Failed'; - //set accessiblity aria-disabled state. + //set accessiblity aria-disabled state. //This will also effect the visual look by corresponding css startTestButton.setAttribute('aria-disabled', false); //update button text to communicate current state of test as In Progress startTestButton.innerHTML = 'Start Test'; //enable start button startTestButton.disabled = false; - //hide current test value in chart + //hide current test value in chart option.series[0].detail.show = false; //update gauge myChart.setOption(option, true); @@ -271,14 +271,14 @@ option.series[0].data[0].value = 0; //updat test status to complete option.series[0].data[0].name = 'Test Failed'; - //set accessiblity aria-disabled state. + //set accessiblity aria-disabled state. //This will also effect the visual look by corresponding css startTestButton.setAttribute('aria-disabled', false); //update button text to communicate current state of test as In Progress startTestButton.innerHTML = 'Start Test'; //enable start button startTestButton.disabled = false; - //hide current test value in chart + //hide current test value in chart option.series[0].detail.show = false; //update gauge myChart.setOption(option, true); @@ -294,14 +294,14 @@ option.series[0].data[0].value = 0; //updat test status to complete option.series[0].data[0].name = 'Test Failed'; - //set accessiblity aria-disabled state. + //set accessiblity aria-disabled state. //This will also effect the visual look by corresponding css startTestButton.setAttribute('aria-disabled', false); //update button text to communicate current state of test as In Progress startTestButton.innerHTML = 'Start Test'; //enable start button startTestButton.disabled = false; - //hide current test value in chart + //hide current test value in chart option.series[0].detail.show = false; //update gauge myChart.setOption(option, true); diff --git a/public/examples/upload/uploadApp.js b/public/examples/upload/uploadApp.js index ba4fe95..07fe6d5 100644 --- a/public/examples/upload/uploadApp.js +++ b/public/examples/upload/uploadApp.js @@ -193,7 +193,7 @@ startTestButton.innerHTML = 'Testing in Progress ...'; //disable button startTestButton.disabled = true; - //set accessiblity aria-disabled state. + //set accessiblity aria-disabled state. //This will also effect the visual look by corresponding css startTestButton.setAttribute('aria-disabled', true); } @@ -257,14 +257,14 @@ option.series[0].data[0].value = 0; //updat test status to complete option.series[0].data[0].name = 'Test Failed'; - //set accessiblity aria-disabled state. + //set accessiblity aria-disabled state. //This will also effect the visual look by corresponding css startTestButton.setAttribute('aria-disabled', false); //update button text to communicate current state of test as In Progress startTestButton.innerHTML = 'Start Test'; //enable start button startTestButton.disabled = false; - //hide current test value in chart + //hide current test value in chart option.series[0].detail.show = false; //update gauge myChart.setOption(option, true); diff --git a/public/lib/downloadHttpConcurrentProgress.js b/public/lib/downloadHttpConcurrentProgress.js index 802771b..97d6d4a 100644 --- a/public/lib/downloadHttpConcurrentProgress.js +++ b/public/lib/downloadHttpConcurrentProgress.js @@ -77,11 +77,17 @@ * @return error object */ downloadHttpConcurrentProgress.prototype.onTestError = function (result) { - if (this._running) { - this.clientCallbackError(result); - clearInterval(this.interval); + if (this._running) { + if ((Date.now() - this._beginTime) > this.testLength) { + this.endTest(); + } + else{ this._running = false; - } + clearInterval(this.interval); + this.clientCallbackError(result); + this.abortAll(); + } + } }; /** * onAbort method @@ -98,13 +104,7 @@ downloadHttpConcurrentProgress.prototype.onTestTimeout = function () { if(this._running) { if ((Date.now() - this._beginTime) > this.testLength) { - clearInterval(this.interval); - if (this.downloadResults && this.downloadResults.length) { - this.clientCallbackComplete(this.downloadResults); - } else { - this.clientCallbackError('no measurements obtained'); - } - this._running = false; + this.endTest(); } } @@ -133,6 +133,10 @@ if (!this._running) { return; } + //check for end of test + if ((Date.now() - this._beginTime) > this.testLength) { + this.endTest(); + } this.totalBytes = this.totalBytes + result.loaded; this._storeResults(result); @@ -224,18 +228,24 @@ } //check for end of test - if ((Date.now() - this._beginTime) > (this.testLength)) { - this._running = false; - clearInterval(this.interval); - if (this.downloadResults && this.downloadResults.length) { - this.clientCallbackComplete(this.downloadResults); - } else { - this.clientCallbackError('no measurements obtained'); - } - this.abortAll(); + if ((Date.now() - this._beginTime) > this.testLength) { + this.endTest(); } }; + /** + * end test method + */ + downloadHttpConcurrentProgress.prototype.endTest = function(){ + this._running = false; + clearInterval(this.interval); + if (this.downloadResults && this.downloadResults.length) { + this.clientCallbackComplete(this.downloadResults); + } else { + this.clientCallbackError('no measurements obtained'); + } + this.abortAll(); + }; /** * reset test variables @@ -255,4 +265,4 @@ }; window.downloadHttpConcurrentProgress = downloadHttpConcurrentProgress; -})(); \ No newline at end of file +})(); diff --git a/public/lib/uploadHttpConcurrentProgress.js b/public/lib/uploadHttpConcurrentProgress.js index 151c592..880b08e 100644 --- a/public/lib/uploadHttpConcurrentProgress.js +++ b/public/lib/uploadHttpConcurrentProgress.js @@ -86,9 +86,15 @@ */ uploadHttpConcurrentProgress.prototype.onTestError = function (result) { if (this._running) { - this.clientCallbackError(result); - clearInterval(this.interval); + if ((Date.now() - this._beginTime) > this.testLength) { + this.endTest(); + } + else{ this._running = false; + clearInterval(this.interval); + this.clientCallbackError(result); + this.abortAll(); + } } }; /** @@ -106,13 +112,7 @@ uploadHttpConcurrentProgress.prototype.onTestTimeout = function () { if (this._running) { if ((Date.now() - this._beginTime) > this.testLength) { - clearInterval(this.interval); - if (this.uploadResults && this.uploadResults.length) { - this.clientCallbackComplete(this.uploadResults); - } else { - this.clientCallbackError('no measurements obtained'); - } - this._running = false; + this.endTest(); } } @@ -277,30 +277,33 @@ } }; - + /** + * end of test + */ + uploadHttpConcurrentProgress.prototype.endTest = function () { + this._running = false; + this.abortAll(); + clearInterval(this.interval); + if (this.uploadResults && this.uploadResults.length) { + var uploadResults = this.uploadResults; + var dataLength = uploadResults.length; + var data = slicing(uploadResults, Math.round(dataLength * 0.4), dataLength); + data = data.sort(numericComparator); + var result = meanCalculator(data); + this.clientCallbackComplete(result); + } else { + this.clientCallbackError('no measurements obtained'); + } + }; /** * Monitor testSeries */ uploadHttpConcurrentProgress.prototype._monitor = function () { this._calculateResults(); //check for end of test - if ((Date.now() - this._beginTime) > (this.testLength)) { - this.abortAll(); - this._running = false; - clearInterval(this.interval); - if (this.uploadResults && this.uploadResults.length) { - var uploadResults = this.uploadResults; - var dataLength = uploadResults.length; - var data = slicing(uploadResults, Math.round(dataLength * 0.4), dataLength); - data = data.sort(numericComparator); - var result = meanCalculator(data); - this.clientCallbackComplete(result); - } else { - this.clientCallbackError('no measurements obtained'); - } - + if ((Date.now() - this._beginTime) > this.testLength) { + this.endTest(); } - }; /** diff --git a/public/lib/xmlhttprequest.js b/public/lib/xmlhttprequest.js index 2cb0087..0f9455a 100644 --- a/public/lib/xmlhttprequest.js +++ b/public/lib/xmlhttprequest.js @@ -65,7 +65,6 @@ this._request.onload = this._handleLoad.bind(this); this._request.onabort = this._handleAbort.bind(this); this._request.timout = this._handleTimeout.bind(this); - this.requestTimeout= setTimeout(this._request.abort.bind(this._request), this.timeout); this._request.onerror = this._handleError.bind(this); this._request.onreadystatechange = this._handleOnReadyStateChange.bind(this); if(this.method==='GET') { @@ -84,7 +83,7 @@ this.id = id; this.transferSize = size; this._request.open(this.method, this.url, true); - this._request.timeout = this.timeout; + this.requestTimeout= setTimeout(this._internalAbort.bind(this), this.timeout); if(this.method==='POST') { this._request.send(payload); } @@ -93,6 +92,16 @@ } }; + + /** + * internal timed abort + */ + xmlHttpRequest.prototype._internalAbort = function() { + if((this._request)&&(this._request.readyState!==4)){ + this._request.abort(); + } + }; + /** * Mark the start time of the request */ @@ -104,6 +113,7 @@ * Handle eror event */ xmlHttpRequest.prototype._handleError = function() { + clearTimeout(this.requestTimeout); var err = { statusText: this._request.statusText, status: this._request.status @@ -114,6 +124,7 @@ * Handle the timeout event on the wrapped request */ xmlHttpRequest.prototype._handleTimeout = function(response) { + clearTimeout(this.requestTimeout); this.totalTime = this.endTime - this.startTime; var transferSizeMbs = (response.loaded * 8) / 1000000; var transferDurationSeconds = this.totalTime/1000; @@ -149,7 +160,10 @@ * Close the request explicitly */ xmlHttpRequest.prototype.close = function () { + clearTimeout(this.requestTimeout); + if((this._request)&&(this._request.readyState!==4)){ this._request.abort(); + } }; /** @@ -158,6 +172,7 @@ xmlHttpRequest.prototype._handleOnReadyStateChange = function () { if(this._request.readyState === 4 && this._request.status === 200) { + clearTimeout(this.requestTimeout); var result = {}; result.totalTime = Date.now() - this.startTime; result.id = this.id; @@ -176,20 +191,13 @@ } } - if(this._request.status > 399){ - var err = { - statusText: this._request.statusText, - status: this._request.status - }; - this.callbackError(err); - return; - } }; /** * Handle the load event on the wrapped request */ xmlHttpRequest.prototype._handleLoad = function (response) { + clearTimeout(this.requestTimeout); this.totalTime = Date.now() - this.startTime; var result = {}; result.time = this.totalTime;