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

Keep track of and log the real connection attempt number and update test

parent ca48afad
Branches
Tags
No related merge requests found
...@@ -65,17 +65,19 @@ class CloudProvider { ...@@ -65,17 +65,19 @@ class CloudProvider {
onClose () { onClose () {
log.info(`Closed connection to websocket`); log.info(`Closed connection to websocket`);
const exponentialTimeout = (Math.pow(2, this.connectionAttempts) - 1) * 1000; const randomizedTimeout = this.randomizeDuration(this.exponentialTimeout());
const randomizedTimeout = this.randomizeDuration(exponentialTimeout);
this.setTimeout(this.reconnectNow.bind(this), randomizedTimeout); this.setTimeout(this.reconnectNow.bind(this), randomizedTimeout);
} }
reconnectNow () { reconnectNow () {
// Max connection attempts at 5, so timeout will max out in range [0, 31s] this.connectionAttempts = this.connectionAttempts + 1;
this.connectionAttempts = Math.min(this.connectionAttempts + 1, 5);
this.openConnection(); this.openConnection();
} }
exponentialTimeout () {
return (Math.pow(2, Math.min(this.connectionAttempts, 5)) - 1) * 1000;
}
randomizeDuration (t) { randomizeDuration (t) {
return Math.random() * t; return Math.random() * t;
} }
...@@ -168,6 +170,7 @@ class CloudProvider { ...@@ -168,6 +170,7 @@ class CloudProvider {
if (this.connection && if (this.connection &&
this.connection.readyState !== WebSocket.CLOSING && this.connection.readyState !== WebSocket.CLOSING &&
this.connection.readyState !== WebSocket.CLOSED) { this.connection.readyState !== WebSocket.CLOSED) {
log.info('Request close cloud connection without reconnecting');
this.connection.onclose = () => {}; // Remove close listener to prevent reconnect this.connection.onclose = () => {}; // Remove close listener to prevent reconnect
this.connection.close(); this.connection.close();
} }
......
...@@ -141,12 +141,17 @@ describe('CloudProvider', () => { ...@@ -141,12 +141,17 @@ describe('CloudProvider', () => {
cloudProvider.connection.close(); cloudProvider.connection.close();
expect(timeout).toEqual(31 * 1000); // 2^5 - 1 expect(timeout).toEqual(31 * 1000); // 2^5 - 1
expect(websocketConstructorCount).toBe(6); expect(websocketConstructorCount).toBe(6);
expect(cloudProvider.connectionAttempts).toBe(5); // Maxed at 5 expect(cloudProvider.connectionAttempts).toBe(6);
cloudProvider.connection.close(); cloudProvider.connection.close();
expect(timeout).toEqual(31 * 1000); // maxed out at 2^5 - 1 expect(timeout).toEqual(31 * 1000); // maxed out at 2^5 - 1
expect(websocketConstructorCount).toBe(7); expect(websocketConstructorCount).toBe(7);
expect(cloudProvider.connectionAttempts).toBe(5); // Maxed at 5 expect(cloudProvider.connectionAttempts).toBe(7);
});
test('exponentialTimeout caps connection attempt number', () => {
cloudProvider.connectionAttempts = 1000;
expect(cloudProvider.exponentialTimeout()).toEqual(31 * 1000);
}); });
test('requestCloseConnection does not try to reconnect', () => { test('requestCloseConnection does not try to reconnect', () => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment