[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Speed up remaining unit tests



commit c6e1fa22a2615bed4282c26c7fb618e7702720a1
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Wed Feb 8 10:08:31 2017 -0800

    Speed up remaining unit tests
    
    Attempted to get all our individual tests under 100ms. Wasn't always
    successful, but close. Biggest benefit was omitting unnecessary iterations from
    the authentication test where we exercise all use cases. This dropped it from
    1.1s to 0.4s.
    
    On my wimpy little netbook this improves the overall unit test runtime from
    7.5s to 6.4s (15% faster).
---
 test/unit/connection/authentication.py            | 59 ++++++++++++-----------
 test/unit/descriptor/networkstatus/document_v3.py | 13 ++---
 test/unit/exit_policy/policy.py                   |  2 +-
 test/unit/manual.py                               | 14 +++++-
 4 files changed, 48 insertions(+), 40 deletions(-)

diff --git a/test/unit/connection/authentication.py b/test/unit/connection/authentication.py
index 70de6d7..b7ccc0d 100644
--- a/test/unit/connection/authentication.py
+++ b/test/unit/connection/authentication.py
@@ -93,16 +93,22 @@ class TestAuthenticate(unittest.TestCase):
       stem.connection.AuthMethod.UNKNOWN,
     ], include_empty = True)
 
-    for protocolinfo_auth_methods in auth_method_combinations:
-      # protocolinfo input for the authenticate() call we'll be making
-      protocolinfo_arg = mocking.get_protocolinfo_response(
-        auth_methods = protocolinfo_auth_methods,
-        cookie_path = '/tmp/blah',
-      )
+    protocolinfo = mocking.get_protocolinfo_response(cookie_path = '/tmp/blah')
 
+    for auth_methods in auth_method_combinations:
       for auth_none_exc in all_auth_none_exc:
         for auth_password_exc in all_auth_password_exc:
           for auth_cookie_exc in all_auth_cookie_exc:
+            # Skip iteration if it's to test exceptions for authentication
+            # we're not using.
+
+            if auth_none_exc and stem.connection.AuthMethod.NONE not in auth_methods:
+              continue
+            elif auth_password_exc and stem.connection.AuthMethod.PASSWORD not in auth_methods:
+              continue
+            elif auth_cookie_exc and stem.connection.AuthMethod.COOKIE not in auth_methods and stem.connection.AuthMethod.SAFECOOKIE not in auth_methods:
+              continue
+
             # Determine if the authenticate() call will succeed and mock each
             # of the authenticate_* function to raise its given exception.
             #
@@ -116,33 +122,28 @@ class TestAuthenticate(unittest.TestCase):
             # matters so the above inaccuracies seem fine.
 
             expect_success = False
-            auth_mocks = {
-              stem.connection.AuthMethod.NONE:
-                (authenticate_none_mock, auth_none_exc),
-              stem.connection.AuthMethod.PASSWORD:
-                (authenticate_password_mock, auth_password_exc),
-              stem.connection.AuthMethod.COOKIE:
-                (authenticate_cookie_mock, auth_cookie_exc),
-              stem.connection.AuthMethod.SAFECOOKIE:
-                (authenticate_safecookie_mock, auth_cookie_exc),
-            }
-
-            for auth_method in auth_mocks:
-              auth_mock, raised_exc = auth_mocks[auth_method]
-
-              if not raised_exc:
-                # Mocking this authentication method so it will succeed. If
-                # it's among the protocolinfo methods then expect success.
-
-                auth_mock.side_effect = None
-                expect_success |= auth_method in protocolinfo_auth_methods
-              else:
+            protocolinfo.auth_methods = auth_methods
+
+            for auth_method in auth_methods:
+              if auth_method == stem.connection.AuthMethod.NONE:
+                auth_mock, raised_exc = authenticate_none_mock, auth_none_exc
+              elif auth_method == stem.connection.AuthMethod.PASSWORD:
+                auth_mock, raised_exc = authenticate_password_mock, auth_password_exc
+              elif auth_method == stem.connection.AuthMethod.COOKIE:
+                auth_mock, raised_exc = authenticate_cookie_mock, auth_cookie_exc
+              elif auth_method == stem.connection.AuthMethod.SAFECOOKIE:
+                auth_mock, raised_exc = authenticate_safecookie_mock, auth_cookie_exc
+
+              if raised_exc:
                 auth_mock.side_effect = raised_exc
+              else:
+                auth_mock.side_effect = None
+                expect_success = True
 
             if expect_success:
-              stem.connection.authenticate(None, 'blah', None, protocolinfo_arg)
+              stem.connection.authenticate(None, 'blah', None, protocolinfo)
             else:
-              self.assertRaises(stem.connection.AuthenticationFailure, stem.connection.authenticate, None, 'blah', None, protocolinfo_arg)
+              self.assertRaises(stem.connection.AuthenticationFailure, stem.connection.authenticate, None, 'blah', None, protocolinfo)
 
     # revert logging back to normal
     stem_logger.setLevel(log.logging_level(log.TRACE))
diff --git a/test/unit/descriptor/networkstatus/document_v3.py b/test/unit/descriptor/networkstatus/document_v3.py
index a3da8a4..e6e5d76 100644
--- a/test/unit/descriptor/networkstatus/document_v3.py
+++ b/test/unit/descriptor/networkstatus/document_v3.py
@@ -662,15 +662,12 @@ DnN5aFtYKiTc19qIC7Nmo+afPdDEf0MlJvEOP5EWl3w=
       '2012-12-12 01:a1:01',
     )
 
-    for field in ('published', 'valid-after', 'fresh-until', 'valid-until'):
-      attr = field.replace('-', '_')
-
-      for test_value in test_values:
-        content = get_network_status_document_v3({'vote-status': 'vote', field: test_value}, content = True)
-        self.assertRaises(ValueError, NetworkStatusDocumentV3, content, True)
+    for test_value in test_values:
+      content = get_network_status_document_v3({'vote-status': 'vote', 'published': test_value}, content = True)
+      self.assertRaises(ValueError, NetworkStatusDocumentV3, content, True)
 
-        document = NetworkStatusDocumentV3(content, False)
-        self.assertEqual(None, getattr(document, attr))
+      document = NetworkStatusDocumentV3(content, False)
+      self.assertEqual(None, document.published)
 
   def test_voting_delay(self):
     """
diff --git a/test/unit/exit_policy/policy.py b/test/unit/exit_policy/policy.py
index b1187ac..29e3837 100644
--- a/test/unit/exit_policy/policy.py
+++ b/test/unit/exit_policy/policy.py
@@ -64,7 +64,7 @@ class TestExitPolicy(unittest.TestCase):
 
     policy = ExitPolicy('accept *:80', 'accept *:443', 'reject *:*')
 
-    for index in range(1, 500):
+    for index in range(1, 100):
       ip_addr = '%i.%i.%i.%i' % (index / 2, index / 2, index / 2, index / 2)
       expected_result = index in (80, 443)
 
diff --git a/test/unit/manual.py b/test/unit/manual.py
index 65957c8..8338af7 100644
--- a/test/unit/manual.py
+++ b/test/unit/manual.py
@@ -103,6 +103,16 @@ EXPECTED_CONFIG_OPTIONS['MaxAdvertisedBandwidth'] = stem.manual.ConfigOption(
   summary = 'Limit for the bandwidth we advertise as being available for relaying',
   description = 'If set, we will not advertise more than this amount of bandwidth for our BandwidthRate. Server operators who want to reduce the number of clients who ask to build circuits through them (since this is proportional to advertised bandwidth rate) can thus reduce the CPU demands on their server without impacting network performance.')
 
+CACHED_MANUAL = None
+
+def _cached_manual():
+  global CACHED_MANUAL
+
+  if CACHED_MANUAL is None:
+    CACHED_MANUAL = stem.manual.Manual.from_cache()
+
+  return CACHED_MANUAL
+
 
 class TestManual(unittest.TestCase):
   def test_has_all_summaries(self):
@@ -112,7 +122,7 @@ class TestManual(unittest.TestCase):
     to write a little summary. They're located in 'stem/settings.cfg'.
     """
 
-    manual = stem.manual.Manual.from_cache()
+    manual = _cached_manual()
     present = set(manual.config_options.keys())
     expected = set([key[15:] for key in stem.manual._config(lowercase = False) if key.startswith('manual.summary.')])
 
@@ -212,7 +222,7 @@ class TestManual(unittest.TestCase):
       self.assertEqual(manual, loaded_manual)
 
   def test_cached_manual(self):
-    manual = stem.manual.Manual.from_cache()
+    manual = _cached_manual()
 
     self.assertEqual('tor - The second-generation onion router', manual.name)
     self.assertEqual('tor [OPTION value]...', manual.synopsis)



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits