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

[tor-commits] [stem/master] Unit test for add_event_listener() example



commit 1a6a4d55701aaa199c14e1ef1ee1b6455922d761
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Mon Nov 5 08:02:15 2012 -0800

    Unit test for add_event_listener() example
    
    Adding a unit test for the pydoc example in add_event_listener(). I really,
    really don't like how the tests for these documentation examples are turning
    out. The end result looks precious little like the example and is convoluted as
    hell.
    
    We should revisit how we do documentation example testing in the future, but
    that'll be a subproject in itself.
---
 run_tests.py                   |    2 +
 stem/control.py                |    2 +-
 test/unit/response/__init__.py |    1 +
 test/unit/response/events.py   |   43 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 47 insertions(+), 1 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index b4f0c06..f490da7 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -27,6 +27,7 @@ import test.unit.descriptor.networkstatus.document_v2
 import test.unit.descriptor.networkstatus.document_v3
 import test.unit.response.control_line
 import test.unit.response.control_message
+import test.unit.response.events
 import test.unit.response.getinfo
 import test.unit.response.getconf
 import test.unit.response.protocolinfo
@@ -133,6 +134,7 @@ UNIT_TESTS = (
   test.unit.tutorial.TestTutorial,
   test.unit.response.control_message.TestControlMessage,
   test.unit.response.control_line.TestControlLine,
+  test.unit.response.events.TestEvents,
   test.unit.response.getinfo.TestGetInfoResponse,
   test.unit.response.getconf.TestGetConfResponse,
   test.unit.response.singleline.TestSingleLineResponse,
diff --git a/stem/control.py b/stem/control.py
index e07658d..c2a94aa 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -1422,7 +1422,7 @@ class Controller(BaseController):
     return response.entries
   
   def _handle_event(self, event_message):
-    stem.response.convert("EVENT", event_message, arrived_at=time.time())
+    stem.response.convert("EVENT", event_message, arrived_at = time.time())
     
     with self._event_listeners_lock:
       for event_type, event_listeners in self._event_listeners.items():
diff --git a/test/unit/response/__init__.py b/test/unit/response/__init__.py
index 530a5d3..51f3cd1 100644
--- a/test/unit/response/__init__.py
+++ b/test/unit/response/__init__.py
@@ -5,6 +5,7 @@ Unit tests for stem.response.
 __all__ = [
   "control_message",
   "control_line",
+  "events",
   "getinfo",
   "getconf",
   "protocolinfo",
diff --git a/test/unit/response/events.py b/test/unit/response/events.py
new file mode 100644
index 0000000..b1111bc
--- /dev/null
+++ b/test/unit/response/events.py
@@ -0,0 +1,43 @@
+"""
+Unit tests for the stem.response.events classes.
+"""
+
+import threading
+import unittest
+
+import stem.response
+import test.mocking as mocking
+
+class TestEvents(unittest.TestCase):
+  def test_example(self):
+    """
+    Exercises the add_event_listener() pydoc example, but without the sleep().
+    """
+    
+    import time
+    from stem.control import Controller, EventType
+    
+    def print_bw(event):
+      msg = "sent: %i, received: %i" % (event.written, event.read)
+      self.assertEqual("sent: 25, received: 15", msg)
+    
+    def event_sender():
+      for i in xrange(3):
+        controller_event = mocking.get_message("650 BW 15 25")
+        stem.response.convert("EVENT", controller_event, arrived_at = 25)
+        print_bw(controller_event)
+        time.sleep(0.05)
+    
+    controller = mocking.get_object(Controller, {
+      'authenticate': mocking.no_op(),
+      'add_event_listener': mocking.no_op(),
+    })
+    
+    controller.authenticate()
+    controller.add_event_listener(print_bw, EventType.BW)
+    
+    events_thread = threading.Thread(target = event_sender)
+    events_thread.start()
+    time.sleep(0.2)
+    events_thread.join()
+



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