Skip to content

Commit 2b7c05f

Browse files
authored
Merge pull request #104 from rotosonic/EricTest
Update xmlhttprequest timeout and percentage complete
2 parents c00ca20 + 9d1ae69 commit 2b7c05f

File tree

6 files changed

+90
-69
lines changed

6 files changed

+90
-69
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "speed-testjs",
3-
"version": "1.0.27",
3+
"version": "1.0.28",
44
"description": "measure internet bandwidth",
55
"main": "index.js",
66
"author": "Maulan Byron",

public/examples/download/downloadApp.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
startTestButton.innerHTML = 'Testing in Progress ...';
179179
//disable button
180180
startTestButton.disabled = true;
181-
//set accessiblity aria-disabled state.
181+
//set accessiblity aria-disabled state.
182182
//This will also effect the visual look by corresponding css
183183
startTestButton.setAttribute('aria-disabled', true);
184184
}
@@ -217,7 +217,7 @@
217217
startTestButton.innerHTML = 'Start Test';
218218
option.series[0].data[0].value = 0;
219219
option.series[0].data[0].name = 'Test Complete';
220-
//set accessiblity aria-disabled state.
220+
//set accessiblity aria-disabled state.
221221
//This will also effect the visual look by corresponding css
222222
startTestButton.setAttribute('aria-disabled', false);
223223
startTestButton.disabled = false;
@@ -248,14 +248,14 @@
248248
option.series[0].data[0].value = 0;
249249
//updat test status to complete
250250
option.series[0].data[0].name = 'Test Failed';
251-
//set accessiblity aria-disabled state.
251+
//set accessiblity aria-disabled state.
252252
//This will also effect the visual look by corresponding css
253253
startTestButton.setAttribute('aria-disabled', false);
254254
//update button text to communicate current state of test as In Progress
255255
startTestButton.innerHTML = 'Start Test';
256256
//enable start button
257257
startTestButton.disabled = false;
258-
//hide current test value in chart
258+
//hide current test value in chart
259259
option.series[0].detail.show = false;
260260
//update gauge
261261
myChart.setOption(option, true);
@@ -271,14 +271,14 @@
271271
option.series[0].data[0].value = 0;
272272
//updat test status to complete
273273
option.series[0].data[0].name = 'Test Failed';
274-
//set accessiblity aria-disabled state.
274+
//set accessiblity aria-disabled state.
275275
//This will also effect the visual look by corresponding css
276276
startTestButton.setAttribute('aria-disabled', false);
277277
//update button text to communicate current state of test as In Progress
278278
startTestButton.innerHTML = 'Start Test';
279279
//enable start button
280280
startTestButton.disabled = false;
281-
//hide current test value in chart
281+
//hide current test value in chart
282282
option.series[0].detail.show = false;
283283
//update gauge
284284
myChart.setOption(option, true);
@@ -294,14 +294,14 @@
294294
option.series[0].data[0].value = 0;
295295
//updat test status to complete
296296
option.series[0].data[0].name = 'Test Failed';
297-
//set accessiblity aria-disabled state.
297+
//set accessiblity aria-disabled state.
298298
//This will also effect the visual look by corresponding css
299299
startTestButton.setAttribute('aria-disabled', false);
300300
//update button text to communicate current state of test as In Progress
301301
startTestButton.innerHTML = 'Start Test';
302302
//enable start button
303303
startTestButton.disabled = false;
304-
//hide current test value in chart
304+
//hide current test value in chart
305305
option.series[0].detail.show = false;
306306
//update gauge
307307
myChart.setOption(option, true);

public/examples/upload/uploadApp.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@
193193
startTestButton.innerHTML = 'Testing in Progress ...';
194194
//disable button
195195
startTestButton.disabled = true;
196-
//set accessiblity aria-disabled state.
196+
//set accessiblity aria-disabled state.
197197
//This will also effect the visual look by corresponding css
198198
startTestButton.setAttribute('aria-disabled', true);
199199
}
@@ -257,14 +257,14 @@
257257
option.series[0].data[0].value = 0;
258258
//updat test status to complete
259259
option.series[0].data[0].name = 'Test Failed';
260-
//set accessiblity aria-disabled state.
260+
//set accessiblity aria-disabled state.
261261
//This will also effect the visual look by corresponding css
262262
startTestButton.setAttribute('aria-disabled', false);
263263
//update button text to communicate current state of test as In Progress
264264
startTestButton.innerHTML = 'Start Test';
265265
//enable start button
266266
startTestButton.disabled = false;
267-
//hide current test value in chart
267+
//hide current test value in chart
268268
option.series[0].detail.show = false;
269269
//update gauge
270270
myChart.setOption(option, true);

public/lib/downloadHttpConcurrentProgress.js

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,17 @@
7777
* @return error object
7878
*/
7979
downloadHttpConcurrentProgress.prototype.onTestError = function (result) {
80-
if (this._running) {
81-
this.clientCallbackError(result);
82-
clearInterval(this.interval);
80+
if (this._running) {
81+
if ((Date.now() - this._beginTime) > this.testLength) {
82+
this.endTest();
83+
}
84+
else{
8385
this._running = false;
84-
}
86+
clearInterval(this.interval);
87+
this.clientCallbackError(result);
88+
this.abortAll();
89+
}
90+
}
8591
};
8692
/**
8793
* onAbort method
@@ -98,13 +104,7 @@
98104
downloadHttpConcurrentProgress.prototype.onTestTimeout = function () {
99105
if(this._running) {
100106
if ((Date.now() - this._beginTime) > this.testLength) {
101-
clearInterval(this.interval);
102-
if (this.downloadResults && this.downloadResults.length) {
103-
this.clientCallbackComplete(this.downloadResults);
104-
} else {
105-
this.clientCallbackError('no measurements obtained');
106-
}
107-
this._running = false;
107+
this.endTest();
108108
}
109109

110110
}
@@ -133,6 +133,10 @@
133133
if (!this._running) {
134134
return;
135135
}
136+
//check for end of test
137+
if ((Date.now() - this._beginTime) > this.testLength) {
138+
this.endTest();
139+
}
136140
this.totalBytes = this.totalBytes + result.loaded;
137141
this._storeResults(result);
138142

@@ -224,18 +228,24 @@
224228

225229
}
226230
//check for end of test
227-
if ((Date.now() - this._beginTime) > (this.testLength)) {
228-
this._running = false;
229-
clearInterval(this.interval);
230-
if (this.downloadResults && this.downloadResults.length) {
231-
this.clientCallbackComplete(this.downloadResults);
232-
} else {
233-
this.clientCallbackError('no measurements obtained');
234-
}
235-
this.abortAll();
231+
if ((Date.now() - this._beginTime) > this.testLength) {
232+
this.endTest();
236233
}
237234

238235
};
236+
/**
237+
* end test method
238+
*/
239+
downloadHttpConcurrentProgress.prototype.endTest = function(){
240+
this._running = false;
241+
clearInterval(this.interval);
242+
if (this.downloadResults && this.downloadResults.length) {
243+
this.clientCallbackComplete(this.downloadResults);
244+
} else {
245+
this.clientCallbackError('no measurements obtained');
246+
}
247+
this.abortAll();
248+
};
239249

240250
/**
241251
* reset test variables
@@ -255,4 +265,4 @@
255265
};
256266

257267
window.downloadHttpConcurrentProgress = downloadHttpConcurrentProgress;
258-
})();
268+
})();

public/lib/uploadHttpConcurrentProgress.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,15 @@
8686
*/
8787
uploadHttpConcurrentProgress.prototype.onTestError = function (result) {
8888
if (this._running) {
89-
this.clientCallbackError(result);
90-
clearInterval(this.interval);
89+
if ((Date.now() - this._beginTime) > this.testLength) {
90+
this.endTest();
91+
}
92+
else{
9193
this._running = false;
94+
clearInterval(this.interval);
95+
this.clientCallbackError(result);
96+
this.abortAll();
97+
}
9298
}
9399
};
94100
/**
@@ -106,13 +112,7 @@
106112
uploadHttpConcurrentProgress.prototype.onTestTimeout = function () {
107113
if (this._running) {
108114
if ((Date.now() - this._beginTime) > this.testLength) {
109-
clearInterval(this.interval);
110-
if (this.uploadResults && this.uploadResults.length) {
111-
this.clientCallbackComplete(this.uploadResults);
112-
} else {
113-
this.clientCallbackError('no measurements obtained');
114-
}
115-
this._running = false;
115+
this.endTest();
116116
}
117117

118118
}
@@ -277,30 +277,33 @@
277277
}
278278
};
279279

280-
280+
/**
281+
* end of test
282+
*/
283+
uploadHttpConcurrentProgress.prototype.endTest = function () {
284+
this._running = false;
285+
this.abortAll();
286+
clearInterval(this.interval);
287+
if (this.uploadResults && this.uploadResults.length) {
288+
var uploadResults = this.uploadResults;
289+
var dataLength = uploadResults.length;
290+
var data = slicing(uploadResults, Math.round(dataLength * 0.4), dataLength);
291+
data = data.sort(numericComparator);
292+
var result = meanCalculator(data);
293+
this.clientCallbackComplete(result);
294+
} else {
295+
this.clientCallbackError('no measurements obtained');
296+
}
297+
};
281298
/**
282299
* Monitor testSeries
283300
*/
284301
uploadHttpConcurrentProgress.prototype._monitor = function () {
285302
this._calculateResults();
286303
//check for end of test
287-
if ((Date.now() - this._beginTime) > (this.testLength)) {
288-
this.abortAll();
289-
this._running = false;
290-
clearInterval(this.interval);
291-
if (this.uploadResults && this.uploadResults.length) {
292-
var uploadResults = this.uploadResults;
293-
var dataLength = uploadResults.length;
294-
var data = slicing(uploadResults, Math.round(dataLength * 0.4), dataLength);
295-
data = data.sort(numericComparator);
296-
var result = meanCalculator(data);
297-
this.clientCallbackComplete(result);
298-
} else {
299-
this.clientCallbackError('no measurements obtained');
300-
}
301-
304+
if ((Date.now() - this._beginTime) > this.testLength) {
305+
this.endTest();
302306
}
303-
304307
};
305308

306309
/**

public/lib/xmlhttprequest.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
this._request.onload = this._handleLoad.bind(this);
6666
this._request.onabort = this._handleAbort.bind(this);
6767
this._request.timout = this._handleTimeout.bind(this);
68-
this.requestTimeout= setTimeout(this._request.abort.bind(this._request), this.timeout);
6968
this._request.onerror = this._handleError.bind(this);
7069
this._request.onreadystatechange = this._handleOnReadyStateChange.bind(this);
7170
if(this.method==='GET') {
@@ -84,7 +83,7 @@
8483
this.id = id;
8584
this.transferSize = size;
8685
this._request.open(this.method, this.url, true);
87-
this._request.timeout = this.timeout;
86+
this.requestTimeout= setTimeout(this._internalAbort.bind(this), this.timeout);
8887
if(this.method==='POST') {
8988
this._request.send(payload);
9089
}
@@ -93,6 +92,16 @@
9392
}
9493

9594
};
95+
96+
/**
97+
* internal timed abort
98+
*/
99+
xmlHttpRequest.prototype._internalAbort = function() {
100+
if((this._request)&&(this._request.readyState!==4)){
101+
this._request.abort();
102+
}
103+
};
104+
96105
/**
97106
* Mark the start time of the request
98107
*/
@@ -104,6 +113,7 @@
104113
* Handle eror event
105114
*/
106115
xmlHttpRequest.prototype._handleError = function() {
116+
clearTimeout(this.requestTimeout);
107117
var err = {
108118
statusText: this._request.statusText,
109119
status: this._request.status
@@ -114,6 +124,7 @@
114124
* Handle the timeout event on the wrapped request
115125
*/
116126
xmlHttpRequest.prototype._handleTimeout = function(response) {
127+
clearTimeout(this.requestTimeout);
117128
this.totalTime = this.endTime - this.startTime;
118129
var transferSizeMbs = (response.loaded * 8) / 1000000;
119130
var transferDurationSeconds = this.totalTime/1000;
@@ -149,7 +160,10 @@
149160
* Close the request explicitly
150161
*/
151162
xmlHttpRequest.prototype.close = function () {
163+
clearTimeout(this.requestTimeout);
164+
if((this._request)&&(this._request.readyState!==4)){
152165
this._request.abort();
166+
}
153167
};
154168

155169
/**
@@ -158,6 +172,7 @@
158172
xmlHttpRequest.prototype._handleOnReadyStateChange = function () {
159173

160174
if(this._request.readyState === 4 && this._request.status === 200) {
175+
clearTimeout(this.requestTimeout);
161176
var result = {};
162177
result.totalTime = Date.now() - this.startTime;
163178
result.id = this.id;
@@ -176,20 +191,13 @@
176191
}
177192

178193
}
179-
if(this._request.status > 399){
180-
var err = {
181-
statusText: this._request.statusText,
182-
status: this._request.status
183-
};
184-
this.callbackError(err);
185-
return;
186-
}
187194
};
188195

189196
/**
190197
* Handle the load event on the wrapped request
191198
*/
192199
xmlHttpRequest.prototype._handleLoad = function (response) {
200+
clearTimeout(this.requestTimeout);
193201
this.totalTime = Date.now() - this.startTime;
194202
var result = {};
195203
result.time = this.totalTime;

0 commit comments

Comments
 (0)