Skip to content

Commit

Permalink
switch to promise / comment logs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-gavrilescu committed Oct 1, 2024
1 parent d888982 commit 20a5ddf
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 168 deletions.
240 changes: 122 additions & 118 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class PreCallTest {
this.rtt = null;
this.fractionLostBytes = -1;
this.resultsHandler = null;
this.startResolve = null;
this.startReject = null;
}

/*
Expand All @@ -45,60 +47,67 @@ class PreCallTest {

/** Start the precall tests
* @param {JSON} iceServers an RTCIceServer object/array
* @param {Function} callback function to call for results
* @public
*/
start(iceServers, callback) {
if (this.browserInfo.browserName === Constants.browserName.msie) {
('precalltest: disable for IE');
if (this.callback) {
this.callback(null, 'Not started: disabled for IE');
}
return;
}
start(iceServers) {
this.iceServers = iceServers;
this.callback = callback;
if (this.active) {
console.warn('Not started: already in progress');
if (this.callback) {
this.callback(null, 'Not started: already in progress');

return new Promise((resolve, reject) => {
this.startResolve = resolve;
this.startReject = reject;

if (this.browserInfo.browserName === Constants.browserName.msie) {
// console.warn('precalltest: disable for IE');
// this.callback(null, 'Not started: disabled for IE');
this.startReject(new Error('Not started: disabled for IE'));

return;
}
return;
}
if (this.callsInProgress > 0) {
console.warn('Not started: call in progress');
if (this.callback) {
this.callback(null, 'Not started: call in progress');

if (this.active) {
// console.warn('Not started: already in progress');
// this.callback(null, 'Not started: already in progress');
this.startReject(new Error('Not started: already in progress'));

return;
}
return;
}
if (!iceServers) {
console.warn('Not started: no ICE servers given');
if (this.callback) {
this.callback(null, 'Not started: no ICE servers given');

if (this.callsInProgress > 0) {
// console.warn('Not started: call in progress');
// this.callback(null, 'Not started: call in progress');
this.startReject(new Error('Not started: call in progress'));

return;
}
return;
}

('PreCallTest start');
this.turnTestCounter = 0;
this.resultsHandler = new ResultsHandler();

let endpointInfo = {
type: 'browser',
os: this.browserInfo.os,
osVersion: this.browserInfo.osVersion,
buildName: this.browserInfo.browserName,
buildVersion: this.browserInfo.browserVersion,
userAgent: this.browserInfo.userAgent,
};
this.resultsHandler.add('endpointInfo', endpointInfo);
if (!iceServers) {
// console.warn('Not started: no ICE servers given');
// this.callback(null, 'Not started: no ICE servers given');
this.startReject(new Error('Not started: no ICE servers given'));

return;
}

// console.log('PreCallTest start');
this.turnTestCounter = 0;
this.resultsHandler = new ResultsHandler();

this.onlineCheck.start();
let endpointInfo = {
type: 'browser',
os: this.browserInfo.os,
osVersion: this.browserInfo.osVersion,
buildName: this.browserInfo.browserName,
buildVersion: this.browserInfo.browserVersion,
userAgent: this.browserInfo.userAgent,
};
this.resultsHandler.add('endpointInfo', endpointInfo);

this.active = true;
this.onlineCheck.start();

this._start();
this.active = true;

this._start();
});
}

/** Internal start function
Expand All @@ -109,89 +118,85 @@ class PreCallTest {
return;
}
this.turnConnection = new TurnConnection(this.browserInfo);
this.turnConnection.connect(this.iceServers)
.then(() => {
('TURN connected.');
if (!this.active) {
this.stop();
return;
}

if (this.resultsHandler) {
this.resultsHandler.setStatusSuccess();
}
this.startTurnTests()
.then(() => {
('All TURN tests completed');
this.stop();
}, (e) => {
(e);
this.stop();
});
}, (e) => {
let continueFlag = e.continueFlag;
('TURN connection failed:', e);
if (this.resultsHandler) {
this.resultsHandler.failure(e);
}
if (!continueFlag) {
this.turnConnection.disconnect();
this.active = false;
let message = '';
try {
message = e.stack;
} catch (err) {
message = e.toString();
}
if (!message || message === '') {
message = e.toString();
this.turnConnection.connect(this.iceServers).then(
() => {
// console.log('TURN connected.');
if (!this.active) {
this.stop();
return;
}

if (this.resultsHandler) {
this.resultsHandler.setStatusFailed();
this.resultsHandler.setStatusSuccess();
}
this.startTurnTests().then(() => {
// console.log('All TURN tests completed');
this.stop();
}, (e) => {
// console.error(e);
this.stop();
});
}, (e) => {
let continueFlag = e.continueFlag;
// console.log('TURN connection failed:', e);
if (this.resultsHandler) {
this.resultsHandler.failure(e);
}
if (!continueFlag) {
this.turnConnection.disconnect();
this.active = false;
let message = '';
try {
message = e.stack;
} catch (err) {
message = e.toString();
}
if (!message || message === '') {
message = e.toString();
}

if (this.resultsHandler) {
this.resultsHandler.setStatusFailed();
}

// console.warn('Error:', message);
// this.callback(null, message);
this.startReject(new Error(message));

console.warn('Error:', message);
if (this.callback) {
this.callback(null, message);
return;
}
return;
}
if (this.resultsHandler) {
this.resultsHandler.setStatusFailed();
if (this.resultsHandler.getFailureNumber() >= FAILURE_RETRIES) {
if (this.resultsHandler) {
this.resultsHandler.setStatusFailed();
if (this.resultsHandler.getFailureNumber() >= FAILURE_RETRIES) {
this.stop();
return;
}
} else {
this.stop();
return;
}
} else {
this.stop();
return;
}
// restart if it hasn't failed too often already
this.turnConnection.disconnect();
setTimeout(() => {
('PreCallTest REstart');
this._start();
}, 0);
});
// restart if it hasn't failed too often already
this.turnConnection.disconnect();
setTimeout(() => {
// console.warn('PreCallTest REstart');
this._start();
}, 0);
});
}

/** Stop the precall tests
* @private */
stop() {
if (this.browserInfo.browserName === Constants.browserName.msie) {
return;
}
if (!this.active) {
return;
}

this.active = false;
if (this.activeTurnTest) {
('Stopping active test');
// console.warn('Stopping active test');
this.activeTurnTest.forceStop();
}
('PreCallTest stop');
// console.log('PreCallTest stop');

let onlineCheckResults = this.onlineCheck.stop();
if (this.resultsHandler) {
Expand All @@ -204,7 +209,7 @@ class PreCallTest {
}

// stop everything
('ICE obtained');
// console.log('ICE obtained');
this.turnConnection.disconnect();

// send results
Expand All @@ -215,7 +220,7 @@ class PreCallTest {
}

// stop everything
('ICE failure');
// console.log('ICE failure');
this.turnConnection.disconnect();

// send results
Expand Down Expand Up @@ -272,17 +277,16 @@ class PreCallTest {
*/
sendResults() {
if (!this.resultsHandler) {
if (this.callback) {
this.callback(null, 'No results present');
}
return;
// this.callback(null, 'No results present');
this.startReject(new Error('No results present'));
return;
}

const results = this.resultsHandler.getResults();
const resultsMin = this.getPublicPrecalltestResults(results);
('**** Results ', results, resultsMin);
if (this.callback) {
this.callback(resultsMin, null);
}
// console.log('**** Results ', results, resultsMin);
//this.callback(resultsMin, null);
this.startResolve(resultsMin);
}

/** Call starts
Expand All @@ -309,7 +313,7 @@ class PreCallTest {
*/
crashDisconnect() {
try {
('something crashed');
// console.error('something crashed');
this.turnConnection.disconnect();
} catch (err) {
(err);
Expand Down Expand Up @@ -356,14 +360,14 @@ class PreCallTest {
}
return test.start() // returns the Promise
.then(() => {
('Test succeeded', testName);
// console.info('Test succeeded', testName);
this.handleTestResults(testName, test.getResults());

this.turnTestCounter += 1;
this.activeTurnTest = null;
return this.startTurnTests();
}, (e) => {
('Test failed', testName, e);
// console.error('Test failed', testName, e);
this.handleTestResults(testName, test.getResults(), e);

this.turnTestCounter += 1;
Expand Down
6 changes: 3 additions & 3 deletions src/tests/lossTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class LossTest extends TurnTest {
this.handleError(new Error(errMsg));
return;
}
('LossTest start', this.duration);
// console.log('LossTest start', this.duration);
let now = Timestamps.getCurrent();
this.results.startTimestamp = now;
this.startSend();
Expand Down Expand Up @@ -103,7 +103,7 @@ class LossTest extends TurnTest {
try {
msgJson = JSON.parse(this.lastMessage);
} catch (e) {
console.error('Error parsing msg:', msg, e);
// console.error('Error parsing msg:', msg, e);
return;
}
if (msgJson) {
Expand All @@ -115,7 +115,7 @@ class LossTest extends TurnTest {
this.results.bytesSent = -1;
this.results.fractionLostBytes = -1;
}
('Loss Results are ', this.results);
// console.log('Loss Results are ', this.results);
}

stop() {
Expand Down
8 changes: 4 additions & 4 deletions src/tests/throughputTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ class ThroughputTest extends TurnTest {
is stuck in a loop when putting data to the buffer.
Also, when it is ever supported, there is no removeEventListener which has be taken care of */
let errMsg = 'Not running throughput test for react-native';
(errMsg);
// console.error(errMsg);
this.handleError(new Error(errMsg));
return;
}
('start', this.duration);
// console.log('start', this.duration);
this.startSend();
}

Expand Down Expand Up @@ -89,7 +89,7 @@ class ThroughputTest extends TurnTest {
let msgJson = JSON.parse(this.lastMessage);
rtt = now - msgJson.timestamp;
} catch (e) {
console.error('Error parsing msg:', msg, e);
// console.error('Error parsing msg:', msg, e);
}

// add and reset
Expand Down Expand Up @@ -173,7 +173,7 @@ class ThroughputTest extends TurnTest {
this.sendChannel = this.connection.sendChannel;
this.usePolling = true;
if (typeof this.sendChannel.bufferedAmountLowThreshold === 'number') {
('Using the bufferedamountlow event for flow control');
// console.log('Using the bufferedamountlow event for flow control');
this.usePolling = false;

this.sendChannel.bufferedAmountLowThreshold = this.bufferFullThreshold/10;
Expand Down
Loading

0 comments on commit 20a5ddf

Please sign in to comment.