commit 2269d007c5ba9ba1207a49d1440e33284e57581d Author: Damian Johnson <ata...@torproject.org> Date: Sun Jan 5 15:09:27 2014 -0800
Test Daemon's general functionality Check that the Daemon's pause method and that it executes the given task at a set rate. --- arm/util/tracker.py | 12 ++++++++++-- test/util/tracker/daemon.py | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/arm/util/tracker.py b/arm/util/tracker.py index 8febb12..1a716d3 100644 --- a/arm/util/tracker.py +++ b/arm/util/tracker.py @@ -218,7 +218,7 @@ class Daemon(threading.Thread): time_since_last_ran = time.time() - self._last_ran if self._is_paused or time_since_last_ran < self._rate: - sleep_duration = max(0.2, self._rate - time_since_last_ran) + sleep_duration = max(0.02, self._rate - time_since_last_ran) with self._pause_condition: if not self._halt: @@ -248,7 +248,7 @@ class Daemon(threading.Thread): :returns: **bool** indicating if our run was successful or not """ - pass + return True def run_counter(self): """ @@ -306,6 +306,14 @@ class Daemon(threading.Thread): self._process_pid = tor_pid self._process_name = tor_cmd if tor_cmd else 'tor' + def __enter__(self): + self.start() + return self + + def __exit__(self, exit_type, value, traceback): + self.stop() + self.join() + class ConnectionTracker(Daemon): """ diff --git a/test/util/tracker/daemon.py b/test/util/tracker/daemon.py index 9bde71f..9d409e2 100644 --- a/test/util/tracker/daemon.py +++ b/test/util/tracker/daemon.py @@ -1,8 +1,9 @@ +import time import unittest from arm.util.tracker import Daemon -from mock import patch +from mock import Mock, patch class TestDaemon(unittest.TestCase): @@ -17,7 +18,7 @@ class TestDaemon(unittest.TestCase): daemon = Daemon(0.05) - self.assertEqual(0.05, daemon._rate) + self.assertEqual(0.05, daemon.get_rate()) self.assertEqual(12345, daemon._process_pid) self.assertEqual('local_tor', daemon._process_name) @@ -45,3 +46,31 @@ class TestDaemon(unittest.TestCase): daemon = Daemon(0.05) self.assertEqual(None, daemon._process_pid) self.assertEqual('tor', daemon._process_name) + self.assertEqual(0, system_mock.call_count) + + @patch('arm.util.tracker.tor_controller', Mock(return_value = Mock())) + @patch('arm.util.tracker.system', Mock(return_value = Mock())) + def test_daemon_calls_task(self): + # Check that our Daemon calls the task method at the given rate. + + with Daemon(0.01) as daemon: + time.sleep(0.05) + self.assertTrue(2 < daemon.run_counter()) + + @patch('arm.util.tracker.tor_controller', Mock(return_value = Mock())) + @patch('arm.util.tracker.system', Mock(return_value = Mock())) + def test_pausing_daemon(self): + # Check that we can pause and unpause daemon. + + with Daemon(0.01) as daemon: + time.sleep(0.05) + self.assertTrue(2 < daemon.run_counter()) + + daemon.set_paused(True) + daemon._run_counter = 0 + time.sleep(0.05) + self.assertEqual(0, daemon.run_counter()) + + daemon.set_paused(False) + time.sleep(0.05) + self.assertTrue(2 < daemon.run_counter()) _______________________________________________ tor-commits mailing list tor-commits@lists.torproject.org https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits