Skip to content
Snippets Groups Projects
Commit 2b92ff4e authored by Paul Kaplan's avatar Paul Kaplan
Browse files

Be more careful about when connection attempt is incremented

parent 7eb203b8
No related branches found
No related tags found
No related merge requests found
......@@ -58,20 +58,16 @@ class CloudProvider {
}
onOpen () {
this.connectionAttempts = 1; // Reset because we successfully connected
this.connectionAttempts = 0; // Reset because we successfully connected
this.writeToServer('handshake');
log.info(`Successfully connected to clouddata server.`);
}
onClose () {
log.info(`Closed connection to websocket`);
this.connectionAttempts += 1;
const randomizedTimeout = this.randomizeDuration(this.exponentialTimeout());
this.setTimeout(this.reconnectNow.bind(this), randomizedTimeout);
}
reconnectNow () {
this.connectionAttempts = this.connectionAttempts + 1;
this.openConnection();
this.setTimeout(this.openConnection.bind(this), randomizedTimeout);
}
exponentialTimeout () {
......
......@@ -31,6 +31,11 @@ export default appTarget => {
if (process.env.NODE_ENV === 'production' && typeof window === 'object') {
// Warn before navigating away
window.onbeforeunload = () => true;
} else {
window.onerror = e => {
alert('Uncaught error occurred–check the console'); // eslint-disable-line
throw e;
};
}
ReactDOM.render(
......
......@@ -103,50 +103,45 @@ describe('CloudProvider', () => {
expect(vmIOData[1].varCreate.name).toEqual('name2');
});
test('connecting sets connnection attempts back to 1', () => {
test('connecting sets connnection attempts back to 0', () => {
expect(cloudProvider.connectionAttempts).toBe(0);
cloudProvider.connectionAttempts = 10;
cloudProvider.connection._open();
expect(cloudProvider.connectionAttempts).toBe(1);
expect(cloudProvider.connectionAttempts).toBe(0);
});
test('disconnect waits for a period equal to 2^k-1 before trying again', () => {
websocketConstructorCount = 1; // This is global, so set it back to 1 to start
websocketConstructorCount = 0; // This is global, so set it back to 0 to start
// Connection attempts should still be 0 because connection hasn't opened yet
expect(cloudProvider.connectionAttempts).toBe(0);
cloudProvider.connection._open();
cloudProvider.connection.close();
expect(timeout).toEqual(1 * 1000); // 2^1 - 1
expect(websocketConstructorCount).toBe(1);
expect(cloudProvider.connectionAttempts).toBe(1);
cloudProvider.connection.close();
expect(timeout).toEqual(1 * 1000); // 2^1 - 1
expect(timeout).toEqual(3 * 1000); // 2^2 - 1
expect(websocketConstructorCount).toBe(2);
expect(cloudProvider.connectionAttempts).toBe(2);
cloudProvider.connection.close();
expect(timeout).toEqual(3 * 1000); // 2^2 - 1
expect(timeout).toEqual(7 * 1000); // 2^3 - 1
expect(websocketConstructorCount).toBe(3);
expect(cloudProvider.connectionAttempts).toBe(3);
cloudProvider.connection.close();
expect(timeout).toEqual(7 * 1000); // 2^3 - 1
expect(timeout).toEqual(15 * 1000); // 2^4 - 1
expect(websocketConstructorCount).toBe(4);
expect(cloudProvider.connectionAttempts).toBe(4);
cloudProvider.connection.close();
expect(timeout).toEqual(15 * 1000); // 2^4 - 1
expect(timeout).toEqual(31 * 1000); // 2^5 - 1
expect(websocketConstructorCount).toBe(5);
expect(cloudProvider.connectionAttempts).toBe(5);
cloudProvider.connection.close();
expect(timeout).toEqual(31 * 1000); // 2^5 - 1
expect(timeout).toEqual(31 * 1000); // maxed out at 2^5 - 1
expect(websocketConstructorCount).toBe(6);
expect(cloudProvider.connectionAttempts).toBe(6);
cloudProvider.connection.close();
expect(timeout).toEqual(31 * 1000); // maxed out at 2^5 - 1
expect(websocketConstructorCount).toBe(7);
expect(cloudProvider.connectionAttempts).toBe(7);
});
test('exponentialTimeout caps connection attempt number', () => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment