From ee05df7f55b5416b4e200f45039643cfbabdfafe Mon Sep 17 00:00:00 2001 From: Vincent Llorens Date: Tue, 7 Jun 2016 14:57:17 +0200 Subject: [PATCH] add unit tests on the Manager class We provide here 2 test suites. The first one test the base class "Manager". Because some of its methods are abstract, we made a second test suite that test a simple implementation of the Manager class. Change-Id: I2f619b752699fae0fdb45f399bdef84b6892c65e --- synergy/common/manager.py | 10 +- synergy/tests/test_manager.py | 27 ------ synergy/tests/test_manager_abstract.py | 59 ++++++++++++ synergy/tests/test_manager_implementation.py | 99 ++++++++++++++++++++ 4 files changed, 165 insertions(+), 30 deletions(-) delete mode 100644 synergy/tests/test_manager.py create mode 100644 synergy/tests/test_manager_abstract.py create mode 100644 synergy/tests/test_manager_implementation.py diff --git a/synergy/common/manager.py b/synergy/common/manager.py index 8c828dc..88e216d 100644 --- a/synergy/common/manager.py +++ b/synergy/common/manager.py @@ -99,10 +99,7 @@ class Manager(Thread): def setStatus(self, status): with self.condition: self.status = status - self.condition.notifyAll() - # if self.status == "RUNNING": - # self.__task() def stop(self): if self.isAlive(): @@ -112,6 +109,13 @@ class Manager(Thread): self.join() def run(self): + """Periodically run the Manager task. + + Note: + This method will be automatically called by Thread.start(). + One should not manually call this method. + See https://docs.python.org/2/library/threading.html#thread-objects + """ while not self.stop_event.is_set(): try: self.task() diff --git a/synergy/tests/test_manager.py b/synergy/tests/test_manager.py deleted file mode 100644 index e02e79c..0000000 --- a/synergy/tests/test_manager.py +++ /dev/null @@ -1,27 +0,0 @@ -# coding: utf-8 -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - - -from synergy.common.manager import Manager -from synergy.tests import base - - -class TestManager(base.TestCase): - - def setUp(self): - super(TestManager, self).setUp() - self.manager = Manager(name="dummy_manager") - - def test_name(self): - self.assertEqual(self.manager.getName(), "dummy_manager") diff --git a/synergy/tests/test_manager_abstract.py b/synergy/tests/test_manager_abstract.py new file mode 100644 index 0000000..6ccc4f7 --- /dev/null +++ b/synergy/tests/test_manager_abstract.py @@ -0,0 +1,59 @@ +# coding: utf-8 +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +""" +Tests the (almost) abstract Manager class. + +Since most of the functionalities of this class are present only in an +implementation of the class, we can just do very basic testing. +""" + +from synergy.common.manager import Manager +from synergy.tests import base + + +class TestManager(base.TestCase): + + def setUp(self): + super(TestManager, self).setUp() + self.manager = Manager(name="dummy_manager") + + def test_get_name(self): + self.assertEqual("dummy_manager", self.manager.getName()) + + def test_get_managers_empty(self): + self.assertEqual({}, self.manager.getManagers()) + + def test_get_options(self): + self.assertEqual([], self.manager.getOptions()) + + def test_is_autostart(self): + self.assertEqual(False, self.manager.isAutoStart()) + + def test_set_autostart(self): + self.manager.setAutoStart(True) + self.assertEqual(True, self.manager.isAutoStart()) + + def test_get_rate(self): + self.assertEqual(-1, self.manager.getRate()) + + def test_set_rate(self): + self.manager.setRate(10) + self.assertEqual(10, self.manager.getRate()) + + def test_get_status(self): + self.assertEqual("CREATED", self.manager.getStatus()) + + def test_set_status(self): + self.manager.setStatus("TEST") + self.assertEqual("TEST", self.manager.getStatus()) diff --git a/synergy/tests/test_manager_implementation.py b/synergy/tests/test_manager_implementation.py new file mode 100644 index 0000000..81392c5 --- /dev/null +++ b/synergy/tests/test_manager_implementation.py @@ -0,0 +1,99 @@ +# coding: utf-8 +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +""" +Tests a simple implementation of the Manager class. + +""" + +from synergy.common.manager import Manager +from synergy.tests import base + + +class SimpleManager(Manager): + """Simple implementation of the Manager class. + + This class maintains an integer counter. + The manager task is to increment the counter by one. + Only one event is possible: increment the counter by 10. + The counter is set to 0 on setup, not on init. + """ + + def __init__(self, name): + super(SimpleManager, self).__init__(name) + self._counter = None + + def _reset_counter(self): + self._counter = None + + def execute(self, command, *args, **kargs): + if command == "RESET": + self._reset_counter() + + def task(self): + self._counter += 1 + + def doOnEvent(self, event_type, *args, **kargs): + if event_type == "JUMP10": + self._counter += 10 + + def setup(self): + self._counter = 0 + + def destroy(self): + self._counter = None + + +class TestManagerImplementation(base.TestCase): + + def setUp(self): + super(TestManagerImplementation, self).setUp() + self.manager = SimpleManager(name="simple_manager") + # We set the manager list to be the manager itself for the sake of + # simplicity. + self.manager.managers = {"simple_manager": self.manager} + + def test_execute(self): + self.manager.setup() + self.manager.execute("RESET") + self.assertIsNone(self.manager._counter) + + def test_notify(self): + self.manager.setup() + self.manager.notify(event_type="JUMP10", manager_name="simple_manager") + self.assertEqual(10, self.manager._counter) + + def test_setup(self): + self.assertIsNone(self.manager._counter) + self.manager.setup() + self.assertEqual(0, self.manager._counter) + + def test_destroy(self): + self.manager.setup() + self.manager.destroy() + self.assertIsNone(self.manager._counter) + + def test_do_on_event(self): + self.manager.setup() + self.manager.doOnEvent(event_type="JUMP10") + self.assertEqual(10, self.manager._counter) + + def test_run_stop(self): + self.manager.setup() + self.manager.rate = 1.0 / 60 / 1000 # to sleep only 1 ms and not 1 min + + self.manager.start() + self.assertEqual(True, self.manager.isAlive()) + + self.manager.stop() + self.assertEqual(False, self.manager.isAlive())