1 # Copyright (c) 2017 Intel Corporation
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
20 from yardstick.benchmark.runners.search import SearchRunner
21 from yardstick.benchmark.runners.search import SearchRunnerHelper
22 from yardstick.common import exceptions as y_exc
25 class TestSearchRunnerHelper(unittest.TestCase):
27 def test___call__(self):
32 benchmark = mock.Mock()
33 method = getattr(benchmark(), 'my_method')
34 helper = SearchRunnerHelper(
35 benchmark, 'my_method', scenario_cfg, {}, mock.Mock())
37 with helper.get_benchmark_instance():
40 method.assert_called_once()
42 def test___call___error(self):
47 helper = SearchRunnerHelper(
48 mock.Mock(), 'my_method', scenario_cfg, {}, mock.Mock())
50 with self.assertRaises(RuntimeError):
53 @mock.patch.object(time, 'sleep')
54 @mock.patch.object(time, 'time')
55 def test_is_not_done(self, mock_time, *args):
60 mock_time.side_effect = range(1000)
62 helper = SearchRunnerHelper(
63 mock.Mock(), 'my_method', scenario_cfg, {}, mock.Mock())
66 for index in helper.is_not_done():
70 self.assertGreaterEqual(index, 10)
72 @mock.patch.object(time, 'sleep')
73 def test_is_not_done_immediate_stop(self, *args):
80 helper = SearchRunnerHelper(
81 mock.Mock(), 'my_method', scenario_cfg, {}, mock.Mock())
84 for index in helper.is_not_done():
88 self.assertEqual(index, -1)
91 class TestSearchRunner(unittest.TestCase):
93 def test__worker_run_once(self):
107 runner = SearchRunner({})
108 runner.worker_helper = mock.Mock(side_effect=update)
110 self.assertFalse(runner._worker_run_once('sequence 1'))
112 def test__worker_run_once_done(self):
114 args[-1].update(data)
131 runner = SearchRunner({})
132 runner.worker_helper = mock.Mock(side_effect=update)
134 self.assertTrue(runner._worker_run_once('sequence 1'))
136 def test__worker_run_once_assertion_error_assert(self):
137 runner = SearchRunner({})
138 runner.sla_action = 'assert'
139 runner.worker_helper = mock.Mock(side_effect=y_exc.SLAValidationError)
141 with self.assertRaises(y_exc.SLAValidationError):
142 runner._worker_run_once('sequence 1')
144 def test__worker_run_once_assertion_error_monitor(self):
145 runner = SearchRunner({})
146 runner.sla_action = 'monitor'
147 runner.worker_helper = mock.Mock(side_effect=y_exc.SLAValidationError)
149 self.assertFalse(runner._worker_run_once('sequence 1'))
151 def test__worker_run_once_non_assertion_error_none(self):
152 runner = SearchRunner({})
153 runner.worker_helper = mock.Mock(side_effect=RuntimeError)
155 self.assertTrue(runner._worker_run_once('sequence 1'))
157 def test__worker_run_once_non_assertion_error(self):
158 runner = SearchRunner({})
159 runner.sla_action = 'monitor'
160 runner.worker_helper = mock.Mock(side_effect=RuntimeError)
162 self.assertFalse(runner._worker_run_once('sequence 1'))
164 def test__worker_run(self):
166 'runner': {'interval': 0, 'timeout': 1},
169 runner = SearchRunner({})
170 runner._worker_run_once = mock.Mock(side_effect=[0, 0, 1])
172 runner._worker_run(mock.Mock(), 'my_method', scenario_cfg, {})
174 def test__worker_run_immediate_stop(self):
181 runner = SearchRunner({})
182 runner._worker_run(mock.Mock(), 'my_method', scenario_cfg, {})
184 @mock.patch('yardstick.benchmark.runners.search.multiprocessing')
185 def test__run_benchmark(self, mock_multi_process):
190 runner = SearchRunner({})
191 runner._run_benchmark(mock.Mock(), 'my_method', scenario_cfg, {})
192 mock_multi_process.Process.assert_called_once()