From 10b611ad8fa4cc7d9e96586fa7019b06f4c5d2b9 Mon Sep 17 00:00:00 2001
From: Paul Kaplan <pkaplan@media.mit.edu>
Date: Mon, 19 Nov 2018 10:17:45 -0500
Subject: [PATCH] Keep track of and log the real connection attempt number and
 update test

---
 src/lib/cloud-provider.js             | 11 +++++++----
 test/unit/util/cloud-provider.test.js |  9 +++++++--
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/src/lib/cloud-provider.js b/src/lib/cloud-provider.js
index 1e6fb2ade..421dd0116 100644
--- a/src/lib/cloud-provider.js
+++ b/src/lib/cloud-provider.js
@@ -65,17 +65,19 @@ class CloudProvider {
 
     onClose () {
         log.info(`Closed connection to websocket`);
-        const exponentialTimeout = (Math.pow(2, this.connectionAttempts) - 1) * 1000;
-        const randomizedTimeout = this.randomizeDuration(exponentialTimeout);
+        const randomizedTimeout = this.randomizeDuration(this.exponentialTimeout());
         this.setTimeout(this.reconnectNow.bind(this), randomizedTimeout);
     }
 
     reconnectNow () {
-        // Max connection attempts at 5, so timeout will max out in range [0, 31s]
-        this.connectionAttempts = Math.min(this.connectionAttempts + 1, 5);
+        this.connectionAttempts = this.connectionAttempts + 1;
         this.openConnection();
     }
 
+    exponentialTimeout () {
+        return (Math.pow(2, Math.min(this.connectionAttempts, 5)) - 1) * 1000;
+    }
+
     randomizeDuration (t) {
         return Math.random() * t;
     }
@@ -168,6 +170,7 @@ class CloudProvider {
         if (this.connection &&
             this.connection.readyState !== WebSocket.CLOSING &&
             this.connection.readyState !== WebSocket.CLOSED) {
+            log.info('Request close cloud connection without reconnecting');
             this.connection.onclose = () => {}; // Remove close listener to prevent reconnect
             this.connection.close();
         }
diff --git a/test/unit/util/cloud-provider.test.js b/test/unit/util/cloud-provider.test.js
index d5f8a7112..3ef4179d2 100644
--- a/test/unit/util/cloud-provider.test.js
+++ b/test/unit/util/cloud-provider.test.js
@@ -141,12 +141,17 @@ describe('CloudProvider', () => {
         cloudProvider.connection.close();
         expect(timeout).toEqual(31 * 1000); // 2^5 - 1
         expect(websocketConstructorCount).toBe(6);
-        expect(cloudProvider.connectionAttempts).toBe(5); // Maxed at 5
+        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(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', () => {
-- 
GitLab