diff options
Diffstat (limited to 'yardstick/tests/unit/common/test_utils.py')
-rw-r--r-- | yardstick/tests/unit/common/test_utils.py | 82 |
1 files changed, 62 insertions, 20 deletions
diff --git a/yardstick/tests/unit/common/test_utils.py b/yardstick/tests/unit/common/test_utils.py index e71d0ff0f..666b29b5f 100644 --- a/yardstick/tests/unit/common/test_utils.py +++ b/yardstick/tests/unit/common/test_utils.py @@ -16,13 +16,15 @@ import mock import os import six from six.moves import configparser +import time import unittest import yardstick from yardstick import ssh import yardstick.error -from yardstick.common import utils from yardstick.common import constants +from yardstick.common import utils +from yardstick.common import exceptions class IterSubclassesTestCase(unittest.TestCase): @@ -305,8 +307,8 @@ power management: """ socket_map = utils.SocketTopology.parse_cpuinfo(cpuinfo) - assert sorted(socket_map.keys()) == [0] - assert sorted(socket_map[0].keys()) == [2, 3, 4] + self.assertEqual(sorted(socket_map.keys()), [0]) + self.assertEqual(sorted(socket_map[0].keys()), [2, 3, 4]) def test_single_socket_hyperthread(self): cpuinfo = """\ @@ -393,11 +395,11 @@ power management: """ socket_map = utils.SocketTopology.parse_cpuinfo(cpuinfo) - assert sorted(socket_map.keys()) == [0] - assert sorted(socket_map[0].keys()) == [1, 2, 3] - assert sorted(socket_map[0][1]) == [5] - assert sorted(socket_map[0][2]) == [6] - assert sorted(socket_map[0][3]) == [7] + self.assertEqual(sorted(socket_map.keys()), [0]) + self.assertEqual(sorted(socket_map[0].keys()), [1, 2, 3]) + self.assertEqual(sorted(socket_map[0][1]), [5]) + self.assertEqual(sorted(socket_map[0][2]), [6]) + self.assertEqual(sorted(socket_map[0][3]), [7]) def test_dual_socket_hyperthread(self): cpuinfo = """\ @@ -592,15 +594,15 @@ power management: """ socket_map = utils.SocketTopology.parse_cpuinfo(cpuinfo) - assert sorted(socket_map.keys()) == [0, 1] - assert sorted(socket_map[0].keys()) == [0, 1, 2] - assert sorted(socket_map[1].keys()) == [26, 27, 28] - assert sorted(socket_map[0][0]) == [44] - assert sorted(socket_map[0][1]) == [1] - assert sorted(socket_map[0][2]) == [2] - assert sorted(socket_map[1][26]) == [85] - assert sorted(socket_map[1][27]) == [86] - assert sorted(socket_map[1][28]) == [43, 87] + self.assertEqual(sorted(socket_map.keys()), [0, 1]) + self.assertEqual(sorted(socket_map[0].keys()), [0, 1, 2]) + self.assertEqual(sorted(socket_map[1].keys()), [26, 27, 28]) + self.assertEqual(sorted(socket_map[0][0]), [44]) + self.assertEqual(sorted(socket_map[0][1]), [1]) + self.assertEqual(sorted(socket_map[0][2]), [2]) + self.assertEqual(sorted(socket_map[1][26]), [85]) + self.assertEqual(sorted(socket_map[1][27]), [86]) + self.assertEqual(sorted(socket_map[1][28]), [43, 87]) def test_dual_socket_no_hyperthread(self): cpuinfo = """\ @@ -796,11 +798,11 @@ power management: """ socket_map = utils.SocketTopology.parse_cpuinfo(cpuinfo) processors = socket_map.processors() - assert processors == [1, 2, 43, 44, 85, 86, 87] + self.assertEqual(processors, [1, 2, 43, 44, 85, 86, 87]) cores = socket_map.cores() - assert cores == [0, 1, 2, 26, 27, 28] + self.assertEqual(cores, [0, 1, 2, 26, 27, 28]) sockets = socket_map.sockets() - assert sockets == [0, 1] + self.assertEqual(sockets, [0, 1]) class ChangeObjToDictTestCase(unittest.TestCase): @@ -1158,3 +1160,43 @@ class ReadMeminfoTestCase(unittest.TestCase): output = utils.read_meminfo(ssh_client) mock_get_client.assert_called_once_with('/proc/meminfo', mock.ANY) self.assertEqual(self.MEMINFO_DICT, output) + + +class TimerTestCase(unittest.TestCase): + + def test__getattr(self): + with utils.Timer() as timer: + time.sleep(1) + self.assertEqual(1, round(timer.total_seconds(), 0)) + self.assertEqual(1, timer.delta.seconds) + + def test__enter_with_timeout(self): + with utils.Timer(timeout=10) as timer: + time.sleep(1) + self.assertEqual(1, round(timer.total_seconds(), 0)) + + def test__enter_with_timeout_exception(self): + with self.assertRaises(exceptions.TimerTimeout): + with utils.Timer(timeout=1): + time.sleep(2) + + +class WaitUntilTrueTestCase(unittest.TestCase): + + def test_no_timeout(self): + self.assertIsNone(utils.wait_until_true(lambda: True, + timeout=1, sleep=1)) + + def test_timeout_generic_exception(self): + with self.assertRaises(exceptions.WaitTimeout): + self.assertIsNone(utils.wait_until_true(lambda: False, + timeout=1, sleep=1)) + + def test_timeout_given_exception(self): + class MyTimeoutException(exceptions.YardstickException): + message = 'My timeout exception' + + with self.assertRaises(MyTimeoutException): + self.assertIsNone( + utils.wait_until_true(lambda: False, timeout=1, sleep=1, + exception=MyTimeoutException)) |