[TIMOB-25676] iOS: HTTPClient unit-test sometimes fails because of incorrect states
| GitHub Issue | n/a |
|---|---|
| Type | Bug |
| Priority | High |
| Status | Closed |
| Resolution | Cannot Reproduce |
| Resolution Date | 2018-02-15T13:44:57.000+0000 |
| Affected Version/s | n/a |
| Fix Version/s | n/a |
| Components | iOS |
| Labels | n/a |
| Reporter | Hans Knöchel |
| Assignee | Hans Knöchel |
| Created | 2018-01-16T08:41:15.000+0000 |
| Updated | 2018-02-15T13:45:10.000+0000 |
Description
The following test-case sometimes fails because the
onreadystate event fires before the ondatastream one. There are also cases where the orreadystate fires one state twice because the states are lost as a result of a race condition when sending out the events.
The proposed fix (working 20/20 times so far) is to ensure we are firing events to the client serial (blocking) on the main-thread. This ensures all state events get queued correctly and fixes the above issues.
Before:
2018-01-16 09:40:35.286 Titanium[4965:119750] [INFO] 1
2018-01-16 09:40:35.288 Titanium[4965:119750] [INFO] 3
2018-01-16 09:40:35.288 Titanium[4965:119750] [INFO] 3
2018-01-16 09:40:35.289 Titanium[4965:119750] [INFO] 4
2018-01-16 09:40:35.289 Titanium[4965:119750] [INFO] DONE
After:
2018-01-16 09:40:35.286 Titanium[4965:119750] [INFO] 1
2018-01-16 09:40:35.288 Titanium[4965:119750] [INFO] 2
2018-01-16 09:40:35.288 Titanium[4965:119750] [INFO] 3
2018-01-16 09:40:35.289 Titanium[4965:119750] [INFO] 4
2018-01-16 09:40:35.289 Titanium[4965:119750] [INFO] DONE
PR (master): https://github.com/appcelerator/titanium_mobile/pull/9737 PR (7_0_X): https://github.com/appcelerator/titanium_mobile/pull/9738
function textXHR() { var xhr = Ti.Network.createHTTPClient(), attempts = 3, dataStreamFinished = false; xhr.setTimeout(3e4); xhr.onreadystatechange = function () { Ti.API.info(this.readyState); if (this.readyState === this.DONE) { if (dataStreamFinished === true) { Ti.API.info('DONE'); } else { alert('ERROR: onreadystatechange done fired before 100% progress'); } } }; xhr.ondatastream = function (e) { if(e.progress == null) { alert('Error: Progress null!') }; if (e.progress >= 1.0) { dataStreamFinished = true; } }; xhr.onerror = function (e) { if (attempts-- > 0) { Ti.API.warn('failed, attempting to retry request...'); xhr.send(); } else { Ti.API.debug(JSON.stringify(e, null , 2)); Ti.API.error(e.error || this.responseText); } }; xhr.open('GET', 'http://www.appcelerator.com/assets/The_iPad_App_Wave.pdf'); xhr.send(); } var win = Ti.UI.createWindow({ backgroundColor: '#fff' }); var btn = Ti.UI.createButton({ title: 'Trigger' }); btn.addEventListener('click', textXHR); win.add(btn); win.open();Not an issue anymore