[tor-commits] [stem/master] Test slow_listener example

atagar at torproject.org atagar at torproject.org
Fri Oct 2 23:16:05 UTC 2020


commit 2880be78fd34fd48939ddb417d2c4b7d725e57f6
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Oct 2 13:23:46 2020 -0700

    Test slow_listener example
---
 test/unit/examples.py | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/test/unit/examples.py b/test/unit/examples.py
index 73bae36c..5e2bbc78 100644
--- a/test/unit/examples.py
+++ b/test/unit/examples.py
@@ -930,8 +930,25 @@ class TestExamples(unittest.TestCase):
         if os.path.exists('/tmp/descriptor_dump'):
           os.remove('/tmp/descriptor_dump')
 
-  def test_slow_listener(self):
-    pass
+  @patch('time.sleep')
+  @patch('time.time', Mock(return_value = 123))
+  @patch('stem.control.Controller.authenticate', Mock())
+  @patch('stem.control.Controller.is_alive', Mock(return_value = True))
+  @patch('stem.control.Controller.from_port', spec = Controller)
+  @patch('sys.stdout', new_callable = io.StringIO)
+  def test_slow_listener(self, stdout_mock, from_port_mock, sleep_mock):
+    controller = Controller(stem.socket.ControlSocket())
+    from_port_mock.return_value = controller
+
+    # emits a BW event when the example runs time.sleep() at the end, but *not*
+    # within the listener
+
+    bw_event = ControlMessage.from_str('650 BW 15 25', 'EVENT', normalize = True)
+    sleep_mock.side_effect = lambda duration: controller._handle_event(bw_event) if duration == 10 else None
+
+    import slow_listener
+
+    self.assertEqual("processing a BW event that's 0.0 seconds old (0 more events are waiting)\n", stdout_mock.getvalue())
 
   @patch('stem.descriptor.remote.DescriptorDownloader')
   @patch('sys.stdout', new_callable = io.StringIO)





More information about the tor-commits mailing list