[tor-commits] [stem/master] Test fibonacci examples

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


commit 68ab78c21c15461936050bfaf1908abf033de85d
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue Sep 29 14:58:44 2020 -0700

    Test fibonacci examples
---
 docs/_static/example/fibonacci_multiprocessing.py | 18 ++++++++++-------
 docs/_static/example/fibonacci_threaded.py        | 24 +++++++++++++----------
 test/unit/examples.py                             | 21 ++++++++++++++++----
 3 files changed, 42 insertions(+), 21 deletions(-)

diff --git a/docs/_static/example/fibonacci_multiprocessing.py b/docs/_static/example/fibonacci_multiprocessing.py
index e909e169..4ad3fe48 100644
--- a/docs/_static/example/fibonacci_multiprocessing.py
+++ b/docs/_static/example/fibonacci_multiprocessing.py
@@ -7,14 +7,18 @@ def fibonacci(n):
   else:
     return fibonacci(n-2) + fibonacci(n-1)
 
-# calculate fibonacci sequences four times in parallel
+def main():
+  # calculate fibonacci sequences four times in parallel
 
-start_time, threads = time.time(), []
+  start_time, threads = time.time(), []
 
-for i in range(4):
-  threads.append(stem.util.system.DaemonTask(fibonacci, (35,), start = True))
+  for i in range(4):
+    threads.append(stem.util.system.DaemonTask(fibonacci, (35,), start = True))
 
-for t in threads:
-  t.join()
+  for t in threads:
+    t.join()
 
-print('took %0.1f seconds' % (time.time() - start_time))
+  print('took %0.1f seconds' % (time.time() - start_time))
+
+if __name__ == '__main__':
+  main()
diff --git a/docs/_static/example/fibonacci_threaded.py b/docs/_static/example/fibonacci_threaded.py
index f40b7401..0e449122 100644
--- a/docs/_static/example/fibonacci_threaded.py
+++ b/docs/_static/example/fibonacci_threaded.py
@@ -7,18 +7,22 @@ def fibonacci(n):
   else:
     return fibonacci(n-2) + fibonacci(n-1)
 
-# calculate fibonacci sequences four times in parallel
+def main():
+  # calculate fibonacci sequences four times in parallel
 
-start_time, threads = time.time(), []
+  start_time, threads = time.time(), []
 
-for i in range(4):
-  t = threading.Thread(target = fibonacci, args = (35,))
-  t.setDaemon(True)
-  t.start()
+  for i in range(4):
+    t = threading.Thread(target = fibonacci, args = (35,))
+    t.setDaemon(True)
+    t.start()
 
-  threads.append(t)
+    threads.append(t)
 
-for t in threads:
-  t.join()
+  for t in threads:
+    t.join()
 
-print('took %0.1f seconds' % (time.time() - start_time))
+  print('took %0.1f seconds' % (time.time() - start_time))
+
+if __name__ == '__main__':
+  main()
diff --git a/test/unit/examples.py b/test/unit/examples.py
index d9a3f6fa..07b6ec00 100644
--- a/test/unit/examples.py
+++ b/test/unit/examples.py
@@ -578,11 +578,24 @@ class TestExamples(unittest.TestCase):
 
     self.assertEqual(EXPECTED_EXIT_USED, stdout_mock.getvalue())
 
-  def test_fibonacci_multiprocessing(self):
-    pass
+  @patch('sys.stdout', new_callable = io.StringIO)
+  def test_fibonacci_multiprocessing(self, stdout_mock):
+    # This example intentionally takes a long time (~11 seconds), so replacing
+    # the work it does with a no-op.
 
-  def test_fibonacci_threaded(self):
-    pass
+    with patch('fibonacci_multiprocessing.fibonacci', Mock(return_value = 5)):
+      import fibonacci_multiprocessing
+
+      fibonacci_multiprocessing.main()
+      self.assertEqual('took 0.0 seconds\n', stdout_mock.getvalue())
+
+  @patch('sys.stdout', new_callable = io.StringIO)
+  def test_fibonacci_threaded(self, stdout_mock):
+    with patch('fibonacci_threaded.fibonacci', Mock(return_value = 5)):
+      import fibonacci_threaded
+
+      fibonacci_threaded.main()
+      self.assertEqual('took 0.0 seconds\n', stdout_mock.getvalue())
 
   def test_get_hidden_service_descriptor(self):
     pass





More information about the tor-commits mailing list