diff --git a/src/lib/cloud-provider.js b/src/lib/cloud-provider.js index d1598dc8e2532379403b41e556c65b45264f7ad1..22fdb1cdb03cae2cf9e98748fb36b9171f466fc9 100644 --- a/src/lib/cloud-provider.js +++ b/src/lib/cloud-provider.js @@ -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 () { diff --git a/src/playground/render-gui.jsx b/src/playground/render-gui.jsx index fac1137e927ef1ffa78a2f766dc1d8f1acb2850c..84f451f6d0413ce546fa973e9a6c50f1b6e857d9 100644 --- a/src/playground/render-gui.jsx +++ b/src/playground/render-gui.jsx @@ -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( diff --git a/test/unit/util/cloud-provider.test.js b/test/unit/util/cloud-provider.test.js index 3ef4179d22a9e2588196b346f5be8b060a51d69e..b803b76d597a51bd934bfe0aed91bfa23d64cb2b 100644 --- a/test/unit/util/cloud-provider.test.js +++ b/test/unit/util/cloud-provider.test.js @@ -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', () => {