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

[tor-commits] [nyx/master] Fix unit tests



commit c6c16ebc42908f56d064711492da6787c946c493
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Tue Oct 24 10:17:43 2017 -0700

    Fix unit tests
    
    Lots of breakages have slipped in while attempting to improve our performance.
    Most were timing related due to our switch to static sleeps.
---
 nyx/__init__.py                    |  1 +
 nyx/panel/__init__.py              |  5 ++---
 nyx/panel/connection.py            | 10 +++++++---
 nyx/panel/header.py                |  4 ++--
 nyx/tracker.py                     | 11 ++++++-----
 run_tests.py                       |  2 +-
 test/panel/connection.py           |  4 ++--
 test/panel/header.py               |  2 +-
 test/tracker/connection_tracker.py |  8 ++++----
 test/tracker/port_usage_tracker.py |  2 +-
 test/tracker/resource_tracker.py   |  2 +-
 11 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/nyx/__init__.py b/nyx/__init__.py
index d9a9b52..5947294 100644
--- a/nyx/__init__.py
+++ b/nyx/__init__.py
@@ -268,6 +268,7 @@ def our_address(default = None):
 
   return CACHED_ADDRESS if CACHED_ADDRESS is not None else default
 
+
 def show_message(message = None, *attr, **kwargs):
   """
   Shows a message in our header.
diff --git a/nyx/panel/__init__.py b/nyx/panel/__init__.py
index ed32887..b75964c 100644
--- a/nyx/panel/__init__.py
+++ b/nyx/panel/__init__.py
@@ -31,10 +31,9 @@ import inspect
 import threading
 import time
 
+import nyx
 import nyx.curses
 
-from nyx import PAUSE_TIME, nyx_interface
-
 __all__ = [
   'config',
   'connection',
@@ -207,7 +206,7 @@ class DaemonPanel(Panel, threading.Thread):
         sleep_until = last_ran + self._update_rate + 0.1
 
         while not self._halt and time.time() < sleep_until:
-          time.sleep(PAUSE_TIME)
+          time.sleep(nyx.PAUSE_TIME)
 
         continue  # done waiting, try again
 
diff --git a/nyx/panel/connection.py b/nyx/panel/connection.py
index 3fe283b..3002d67 100644
--- a/nyx/panel/connection.py
+++ b/nyx/panel/connection.py
@@ -11,12 +11,13 @@ import itertools
 import re
 import time
 
+import nyx
 import nyx.curses
 import nyx.panel
 import nyx.popups
 import nyx.tracker
 
-from nyx import PAUSE_TIME, nyx_interface, tor_controller, our_address
+from nyx import nyx_interface, tor_controller
 from nyx.curses import WHITE, NORMAL, BOLD, HIGHLIGHT
 from nyx.menu import MenuItem, Submenu, RadioMenuItem, RadioGroup
 
@@ -505,7 +506,7 @@ class ConnectionPanel(nyx.panel.DaemonPanel):
         elif self._halt:
           return
         else:
-          time.sleep(PAUSE_TIME)
+          time.sleep(nyx.PAUSE_TIME)
 
     controller = tor_controller()
     LAST_RETRIEVED_CIRCUITS = controller.get_circuits([])
@@ -607,7 +608,10 @@ def _draw_line(subwindow, x, y, line, is_selected, width, current_time):
 
 
 def _draw_address_column(subwindow, x, y, line, attr):
-  src = '%s:%s' % (our_address(line.connection.local_address), line.connection.local_port if line.line_type == LineType.CONNECTION else '')
+  if line.line_type == LineType.CONNECTION:
+    src = '%s:%s' % (nyx.our_address(line.connection.local_address), line.connection.local_port)
+  else:
+    src = nyx.our_address(line.connection.local_address)
 
   if line.line_type == LineType.CIRCUIT_HEADER and line.circuit.status != 'BUILT':
     dst = 'Building...'
diff --git a/nyx/panel/header.py b/nyx/panel/header.py
index 3751d56..0820a21 100644
--- a/nyx/panel/header.py
+++ b/nyx/panel/header.py
@@ -23,7 +23,7 @@ import nyx.popups
 import nyx.tracker
 
 from stem.util import conf, log
-from nyx import nyx_interface, tor_controller, our_address
+from nyx import nyx_interface, tor_controller
 
 from nyx.curses import RED, GREEN, YELLOW, CYAN, WHITE, BOLD, HIGHLIGHT
 
@@ -268,7 +268,7 @@ class Sampling(object):
       'version': str(controller.get_version('Unknown')).split()[0],
       'version_status': controller.get_info('status/version/current', 'Unknown'),
 
-      'address': or_listeners[0][0] if (or_listeners and or_listeners[0][0] != '0.0.0.0') else our_address('Unknown'),
+      'address': or_listeners[0][0] if (or_listeners and or_listeners[0][0] != '0.0.0.0') else nyx.our_address('Unknown'),
       'or_port': or_listeners[0][1] if or_listeners else '',
       'dir_port': controller.get_conf('DirPort', '0'),
       'control_port': str(control_listeners[0][1]) if control_listeners else None,
diff --git a/nyx/tracker.py b/nyx/tracker.py
index 6d6ab64..fb41f9e 100644
--- a/nyx/tracker.py
+++ b/nyx/tracker.py
@@ -60,7 +60,7 @@ import stem.control
 import stem.descriptor.router_status_entry
 import stem.util.log
 
-from nyx import PAUSE_TIME, tor_controller, our_address
+from nyx import tor_controller
 from stem.util import conf, connection, enum, proc, str_tools, system
 
 CONFIG = conf.config_dict('nyx', {
@@ -387,10 +387,10 @@ class Daemon(threading.Thread):
   def run(self):
     while not self._halt:
       if self._is_paused or time.time() - self._last_ran < self._rate:
-        sleep_until = self._last_ran + self._rate + 0.1
+        sleep_until = self._last_ran + self._rate
 
         while not self._halt and time.time() < sleep_until:
-          time.sleep(PAUSE_TIME)
+          time.sleep(nyx.PAUSE_TIME)
 
         continue  # done waiting, try again
 
@@ -903,7 +903,7 @@ class ConsensusTracker(object):
 
     controller = tor_controller()
 
-    if our_address() == address:
+    if nyx.our_address() == address:
       fingerprint = controller.get_info('fingerprint', None)
       ports = controller.get_ports(stem.control.Listener.OR, None)
 
@@ -924,9 +924,10 @@ class ConsensusTracker(object):
     controller = tor_controller()
 
     if fingerprint == controller.get_info('fingerprint', None):
+      my_address = nyx.our_address()
       my_or_ports = controller.get_ports(stem.control.Listener.OR, [])
 
-      if our_address() and len(my_or_ports) == 1:
+      if my_address and len(my_or_ports) == 1:
         return (my_address, my_or_ports[0])
 
     return nyx.cache().relay_address(fingerprint, default)
diff --git a/run_tests.py b/run_tests.py
index d923178..a9dd98a 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -30,7 +30,7 @@ SRC_PATHS = [os.path.join(NYX_BASE, path) for path in (
 
 @nyx.uses_settings
 def main():
-  nyx.TESTING = True
+  nyx.PAUSE_TIME = 0.000001  # make pauses negligibly low since our tests trigger things rapidly
   test_config = stem.util.conf.get_config('test')
   test_config.load(os.path.join(NYX_BASE, 'test', 'settings.cfg'))
 
diff --git a/test/panel/connection.py b/test/panel/connection.py
index f01d99a..c95f31a 100644
--- a/test/panel/connection.py
+++ b/test/panel/connection.py
@@ -195,8 +195,8 @@ class TestConnectionPanel(unittest.TestCase):
 
   @require_curses
   @patch('nyx.panel.connection.tor_controller')
+  @patch('nyx.our_address', Mock(return_value = '82.121.9.9'))
   def test_draw_line(self, tor_controller_mock):
-    tor_controller_mock().get_info.return_value = '82.121.9.9'
     tor_controller_mock().is_geoip_unavailable.return_value = False
 
     test_data = ((
@@ -222,8 +222,8 @@ class TestConnectionPanel(unittest.TestCase):
 
   @require_curses
   @patch('nyx.panel.connection.tor_controller')
+  @patch('nyx.our_address', Mock(return_value = '82.121.9.9'))
   def test_draw_address_column(self, tor_controller_mock):
-    tor_controller_mock().get_info.return_value = '82.121.9.9'
     tor_controller_mock().is_geoip_unavailable.return_value = False
 
     test_data = ((
diff --git a/test/panel/header.py b/test/panel/header.py
index 805c8c7..73259da 100644
--- a/test/panel/header.py
+++ b/test/panel/header.py
@@ -87,6 +87,7 @@ class TestHeaderPanel(unittest.TestCase):
   @patch('time.time', Mock(return_value = 1234.5))
   @patch('os.times', Mock(return_value = (0.08, 0.03, 0.0, 0.0, 18759021.31)))
   @patch('os.uname', Mock(return_value = ('Linux', 'odin', '3.5.0-54-generic', '#81~precise1-Ubuntu SMP Tue Jul 15 04:05:58 UTC 2014', 'i686')))
+  @patch('nyx.our_address', Mock(return_value = '174.21.17.28'))
   @patch('stem.util.system.start_time', Mock(return_value = 5678))
   @patch('stem.util.proc.file_descriptors_used', Mock(return_value = 89))
   def test_sample(self, consensus_tracker_mock, resource_tracker_mock, tor_controller_mock):
@@ -101,7 +102,6 @@ class TestHeaderPanel(unittest.TestCase):
     tor_controller_mock().get_info.side_effect = lambda param, default = None: {
       'fingerprint': '1A94D1A794FCB2F8B6CBC179EF8FDD4008A98D3B',
       'status/version/current': 'recommended',
-      'address': '174.21.17.28',
       'process/descriptor-limit': 678,
     }[param]
 
diff --git a/test/tracker/connection_tracker.py b/test/tracker/connection_tracker.py
index 39ded42..50faef1 100644
--- a/test/tracker/connection_tracker.py
+++ b/test/tracker/connection_tracker.py
@@ -49,17 +49,17 @@ class TestConnectionTracker(unittest.TestCase):
     get_value_mock.side_effect = IOError()
 
     with ConnectionTracker(0.01) as daemon:
-      time.sleep(0.03)
+      time.sleep(0.015)
 
       self.assertEqual([connection.Resolver.NETSTAT, connection.Resolver.LSOF], daemon._resolvers)
       self.assertEqual([], daemon.get_value())
 
-      time.sleep(0.05)
+      time.sleep(0.025)
 
       self.assertEqual([connection.Resolver.LSOF], daemon._resolvers)
       self.assertEqual([], daemon.get_value())
 
-      time.sleep(0.05)
+      time.sleep(0.035)
 
       self.assertEqual([], daemon._resolvers)
       self.assertEqual([], daemon.get_value())
@@ -96,7 +96,7 @@ class TestConnectionTracker(unittest.TestCase):
       self.assertEqual(1, len(connections))
 
       self.assertEqual(STEM_CONNECTIONS[0].remote_address, connections[0].remote_address)
-      self.assertTrue(first_start_time < connections[0].start_time < time.time())
+      self.assertTrue(first_start_time <= connections[0].start_time <= time.time())
       self.assertTrue(connections[0].is_legacy)
 
       second_start_time = time.time()
diff --git a/test/tracker/port_usage_tracker.py b/test/tracker/port_usage_tracker.py
index edf1157..7e69f21 100644
--- a/test/tracker/port_usage_tracker.py
+++ b/test/tracker/port_usage_tracker.py
@@ -111,7 +111,7 @@ class TestPortUsageTracker(unittest.TestCase):
       self.assertEqual(0, daemon._failure_count)
 
       daemon.query([37277, 51849], [])
-      time.sleep(0.03)
+      time.sleep(0.015)
       self.assertTrue(daemon.is_alive())
       time.sleep(0.1)
       self.assertFalse(daemon.is_alive())
diff --git a/test/tracker/resource_tracker.py b/test/tracker/resource_tracker.py
index 3f9d927..25837f1 100644
--- a/test/tracker/resource_tracker.py
+++ b/test/tracker/resource_tracker.py
@@ -94,7 +94,7 @@ class TestResourceTracker(unittest.TestCase):
     tor_controller_mock().get_pid.return_value = 12345
 
     with ResourceTracker(0.01) as daemon:
-      time.sleep(0.03)
+      time.sleep(0.015)
 
       self.assertEqual(True, daemon._use_proc)
       resources = daemon.get_value()

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