[tor-commits] [stem/master] Speed up event receipt tests

atagar at torproject.org atagar at torproject.org
Sun Mar 22 22:32:49 UTC 2015


commit 6d4d4816ff5cea7611bde36256d1f1b5e9eab5e9
Author: Sebastian Hahn <sebastian at torproject.org>
Date:   Sat Mar 21 04:37:03 2015 +0100

    Speed up event receipt tests
    
    Use CONF_CHANGED instead of BW events, they arrive much quicker and
    don't require us to wait so long. Execution time for the controller
    tests go from 1m25.348s to 0m6.053s when selecting all connection
    configurations.
---
 run_tests.py                     |    9 ++---
 test/integ/control/controller.py |   72 ++++++++++++++++++++------------------
 2 files changed, 43 insertions(+), 38 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index 912a8d3..7ae495e 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -11,6 +11,7 @@ import sys
 import threading
 import time
 import unittest
+import traceback
 
 try:
   from StringIO import StringIO
@@ -339,13 +340,13 @@ def _run_test(args, test_class, output_filters, logging_buffer):
 
   try:
     suite = unittest.TestLoader().loadTestsFromName(test_class)
-  except AttributeError, e:
+  except AttributeError as e:
     # should only come up if user provided '--test' for something that doesn't exist
-    println(e, ERROR)
+    println("%s\n" % e, ERROR)
     println(" no such test", ERROR)
     return None
-  except Exception, e:
-    println(e, ERROR)
+  except Exception as e:
+    println("%s\n" % e, ERROR)
     println(" failed", ERROR)
     return None
 
diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py
index 0a0e112..7873acc 100644
--- a/test/integ/control/controller.py
+++ b/test/integ/control/controller.py
@@ -39,10 +39,6 @@ TEST_ROUTER_STATUS_ENTRY = None
 
 
 class TestController(unittest.TestCase):
-  # TODO: We should find an event we can reliably trigger rather than using BW
-  # events with sleeps. This both slows our tests and can be unreliable on
-  # systems under a heavy load.
-
   def test_from_port(self):
     """
     Basic sanity check for the from_port constructor.
@@ -123,17 +119,18 @@ class TestController(unittest.TestCase):
     runner = test.runner.get_runner()
 
     with runner.get_tor_controller() as controller:
-      controller.add_event_listener(listener1, EventType.BW)
-      controller.add_event_listener(listener2, EventType.BW, EventType.DEBUG)
+      controller.add_event_listener(listener1, EventType.CONF_CHANGED)
+      controller.add_event_listener(listener2, EventType.CONF_CHANGED, EventType.DEBUG)
 
-      # BW events occure at the rate of one per second, so wait a bit to let
-      # some accumulate.
+      # The NodeFamily is a harmless option we can toggle
+      controller.set_conf('NodeFamily', 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF')
 
-      event_notice1.wait(4)
-      self.assertTrue(len(event_buffer1) >= 1)
+      # Wait for the event. Assert that we get it within 10 seconds
+      self.assertTrue(event_notice1.wait(10))
+      self.assertEqual(len(event_buffer1), 1)
       event_notice1.clear()
 
-      event_notice2.wait(4)
+      self.assertTrue(event_notice2.wait(10))
       self.assertTrue(len(event_buffer2) >= 1)
       event_notice2.clear()
 
@@ -143,20 +140,19 @@ class TestController(unittest.TestCase):
 
       buffer2_size = len(event_buffer2)
 
-      event_notice1.wait(4)
-      self.assertTrue(len(event_buffer1) >= 2)
+      controller.set_conf('NodeFamily', 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')
+      self.assertTrue(event_notice1.wait(10))
+      self.assertEqual(len(event_buffer1), 2)
+      event_notice1.clear()
 
-      event_notice2.wait(4)
       self.assertEqual(buffer2_size, len(event_buffer2))
 
       for event in event_buffer1:
         self.assertTrue(isinstance(event, stem.response.events.Event))
-        self.assertEqual(2, len(event.positional_args))
+        self.assertEqual(0, len(event.positional_args))
         self.assertEqual({}, event.keyword_args)
 
-        self.assertTrue(isinstance(event, stem.response.events.BandwidthEvent))
-        self.assertTrue(hasattr(event, 'read'))
-        self.assertTrue(hasattr(event, 'written'))
+        self.assertTrue(isinstance(event, stem.response.events.ConfChangedEvent))
 
   @require_controller
   def test_reattaching_listeners(self):
@@ -175,13 +171,12 @@ class TestController(unittest.TestCase):
     runner = test.runner.get_runner()
 
     with runner.get_tor_controller() as controller:
-      controller.add_event_listener(listener, EventType.BW)
+      controller.add_event_listener(listener, EventType.CONF_CHANGED)
 
-      # Get a BW event or two. These should be emitted each second but under
-      # heavy system load that's not always the case.
-
-      event_notice.wait(4)
-      self.assertTrue(len(event_buffer) >= 1)
+      controller.set_conf('NodeFamily', 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')
+      self.assertTrue(event_notice.wait(10))
+      self.assertEqual(len(event_buffer), 1)
+      event_notice.clear()
 
       # disconnect and check that we stop getting events
 
@@ -189,21 +184,27 @@ class TestController(unittest.TestCase):
       event_notice.clear()
       event_buffer = []
 
-      event_notice.wait(2)
-      self.assertTrue(len(event_buffer) == 0)
+      # Spawn a second controller and trigger an event
+      controller2 = runner.get_tor_controller()
+      controller2.connect()
+      controller2.authenticate(password = test.runner.CONTROL_PASSWORD)
+      controller2.set_conf('NodeFamily', 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB')
+      controller2.close()
+      self.assertEqual(len(event_buffer), 0)
 
       # reconnect and check that we get events again
 
       controller.connect()
       controller.authenticate(password = test.runner.CONTROL_PASSWORD)
 
-      event_notice.wait(4)
-      self.assertTrue(len(event_buffer) >= 1)
+      controller.set_conf('NodeFamily', 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC')
+      self.assertTrue(event_notice.wait(10))
+      self.assertEqual(len(event_buffer), 1)
+      event_notice.clear()
 
       # disconnect
 
       controller.close()
-      event_notice.clear()
       event_buffer = []
 
       # reconnect and check that we get events again
@@ -211,13 +212,14 @@ class TestController(unittest.TestCase):
       controller.connect()
       stem.connection.authenticate(controller, password = test.runner.CONTROL_PASSWORD)
 
-      event_notice.wait(4)
-      self.assertTrue(len(event_buffer) >= 1)
+      controller.set_conf('NodeFamily', 'DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD')
+      self.assertTrue(event_notice.wait(10))
+      self.assertEqual(len(event_buffer), 1)
+      event_notice.clear()
 
       # disconnect
 
       controller.close()
-      event_notice.clear()
       event_buffer = []
 
       # Reconnect and check that we get events again. This is being done by
@@ -233,8 +235,10 @@ class TestController(unittest.TestCase):
         else:
           controller.msg('AUTHENTICATE')
 
-        event_notice.wait(4)
-        self.assertTrue(len(event_buffer) >= 1)
+        controller.set_conf('NodeFamily', 'EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE')
+        self.assertTrue(event_notice.wait(10))
+        self.assertEqual(len(event_buffer), 1)
+        event_notice.clear()
 
   @require_controller
   def test_getinfo(self):





More information about the tor-commits mailing list