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
This commit is contained in:
parent
1de615b32e
commit
ee05df7f55
@ -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()
|
||||
|
@ -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")
|
59
synergy/tests/test_manager_abstract.py
Normal file
59
synergy/tests/test_manager_abstract.py
Normal file
@ -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())
|
99
synergy/tests/test_manager_implementation.py
Normal file
99
synergy/tests/test_manager_implementation.py
Normal file
@ -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())
|
Loading…
x
Reference in New Issue
Block a user