[tor-commits] [snowflake/master] Use some class vars in broker to eliminate globals

arlo at torproject.org arlo at torproject.org
Wed May 8 15:41:17 UTC 2019


commit 7ce3c83a31dc8fd7667af6aa9b0f58defb0192c9
Author: Arlo Breault <arlolra at gmail.com>
Date:   Wed May 1 10:48:07 2019 -0400

    Use some class vars in broker to eliminate globals
---
 proxy/broker.coffee           | 25 ++++++++++++-------------
 proxy/spec/broker.spec.coffee | 10 +++++-----
 2 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/proxy/broker.coffee b/proxy/broker.coffee
index 6154a4b..981a761 100644
--- a/proxy/broker.coffee
+++ b/proxy/broker.coffee
@@ -5,15 +5,14 @@ Browser snowflakes must register with the broker in order
 to get assigned to clients.
 ###
 
-STATUS_OK = 200
-STATUS_GONE = 410
-STATUS_GATEWAY_TIMEOUT = 504
-
-MESSAGE_TIMEOUT = 'Timed out waiting for a client offer.'
-MESSAGE_UNEXPECTED = 'Unexpected status.'
-
 # Represents a broker running remotely.
 class Broker
+  @STATUS_OK = 200
+  @STATUS_GONE = 410
+  @STATUS_GATEWAY_TIMEOUT = 504
+
+  @MESSAGE_TIMEOUT = 'Timed out waiting for a client offer.'
+  @MESSAGE_UNEXPECTED = 'Unexpected status.'
 
   clients: 0
 
@@ -39,15 +38,15 @@ class Broker
       xhr.onreadystatechange = ->
         return if xhr.DONE != xhr.readyState
         switch xhr.status
-          when STATUS_OK
+          when Broker.STATUS_OK
             fulfill xhr.responseText  # Should contain offer.
-          when STATUS_GATEWAY_TIMEOUT
-            reject MESSAGE_TIMEOUT
+          when Broker.STATUS_GATEWAY_TIMEOUT
+            reject Broker.MESSAGE_TIMEOUT
           else
             log 'Broker ERROR: Unexpected ' + xhr.status +
                 ' - ' + xhr.statusText
             snowflake.ui.setStatus ' failure. Please refresh.'
-            reject MESSAGE_UNEXPECTED
+            reject Broker.MESSAGE_UNEXPECTED
       @_xhr = xhr  # Used by spec to fake async Broker interaction
       @_postRequest id, xhr, 'proxy', id
 
@@ -60,10 +59,10 @@ class Broker
     xhr.onreadystatechange = ->
       return if xhr.DONE != xhr.readyState
       switch xhr.status
-        when STATUS_OK
+        when Broker.STATUS_OK
           dbg 'Broker: Successfully replied with answer.'
           dbg xhr.responseText
-        when STATUS_GONE
+        when Broker.STATUS_GONE
           dbg 'Broker: No longer valid to reply with answer.'
         else
           dbg 'Broker ERROR: Unexpected ' + xhr.status +
diff --git a/proxy/spec/broker.spec.coffee b/proxy/spec/broker.spec.coffee
index aef115f..3532fe1 100644
--- a/proxy/spec/broker.spec.coffee
+++ b/proxy/spec/broker.spec.coffee
@@ -25,7 +25,7 @@ describe 'Broker', ->
       # fake successful request and response from broker.
       spyOn(b, '_postRequest').and.callFake ->
         b._xhr.readyState = b._xhr.DONE
-        b._xhr.status = STATUS_OK
+        b._xhr.status = Broker.STATUS_OK
         b._xhr.responseText = 'fake offer'
         b._xhr.onreadystatechange()
       poll = b.getClientOffer()
@@ -35,7 +35,7 @@ describe 'Broker', ->
         expect(desc).toEqual 'fake offer'
         done()
       .catch ->
-        fail 'should not reject on STATUS_OK'
+        fail 'should not reject on Broker.STATUS_OK'
         done()
 
     it 'rejects if the broker timed-out', (done) ->
@@ -43,7 +43,7 @@ describe 'Broker', ->
       # fake timed-out request from broker
       spyOn(b, '_postRequest').and.callFake ->
         b._xhr.readyState = b._xhr.DONE
-        b._xhr.status = STATUS_GATEWAY_TIMEOUT
+        b._xhr.status = Broker.STATUS_GATEWAY_TIMEOUT
         b._xhr.onreadystatechange()
       poll = b.getClientOffer()
       expect(poll).not.toBeNull()
@@ -52,7 +52,7 @@ describe 'Broker', ->
         fail 'should not fulfill on GATEWAY_TIMEOUT'
         done()
       , (err) ->
-        expect(err).toBe MESSAGE_TIMEOUT
+        expect(err).toBe Broker.MESSAGE_TIMEOUT
         done()
 
     it 'rejects on any other status', (done) ->
@@ -69,7 +69,7 @@ describe 'Broker', ->
         fail 'should not fulfill on non-OK status'
         done()
       , (err) ->
-        expect(err).toBe MESSAGE_UNEXPECTED
+        expect(err).toBe Broker.MESSAGE_UNEXPECTED
         expect(b._xhr.status).toBe 1337
         done()
 





More information about the tor-commits mailing list