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):
28 cls = mock.MagicMock()
29 aborted = mock.MagicMock()
35 method = getattr(benchmark, 'my_method')
36 helper = SearchRunnerHelper(
37 cls, 'my_method', scenario_cfg, {}, aborted)
39 with helper.get_benchmark_instance():
42 method.assert_called_once()
44 def test___call___error(self):
45 cls = mock.MagicMock()
46 aborted = mock.MagicMock()
51 helper = SearchRunnerHelper(
52 cls, 'my_method', scenario_cfg, {}, aborted)
54 with self.assertRaises(RuntimeError):
57 @mock.patch.object(time, 'sleep')
58 @mock.patch.object(time, 'time')
59 def test_is_not_done(self, mock_time, *args):
60 cls = mock.MagicMock()
61 aborted = mock.MagicMock()
66 mock_time.side_effect = range(1000)
68 helper = SearchRunnerHelper(
69 cls, 'my_method', scenario_cfg, {}, aborted)
72 for index in helper.is_not_done():
76 self.assertGreaterEqual(index, 10)
78 @mock.patch.object(time, 'sleep')
79 def test_is_not_done_immediate_stop(self, *args):
80 cls = mock.MagicMock()
81 aborted = mock.MagicMock()
88 helper = SearchRunnerHelper(
89 cls, 'my_method', scenario_cfg, {}, aborted)
92 for index in helper.is_not_done():
96 self.assertEqual(index, -1)
99 class TestSearchRunner(unittest.TestCase):
101 def test__worker_run_once(self):
103 args[-1].update(data)
115 runner = SearchRunner({})
116 runner.worker_helper = mock.MagicMock(side_effect=update)
118 self.assertFalse(runner._worker_run_once('sequence 1'))
120 def test__worker_run_once_done(self):
122 args[-1].update(data)
139 runner = SearchRunner({})
140 runner.worker_helper = mock.MagicMock(side_effect=update)
142 self.assertTrue(runner._worker_run_once('sequence 1'))
144 def test__worker_run_once_assertion_error_assert(self):
145 runner = SearchRunner({})
146 runner.sla_action = 'assert'
147 runner.worker_helper = mock.MagicMock(side_effect=y_exc.SLAValidationError)
149 with self.assertRaises(y_exc.SLAValidationError):
150 runner._worker_run_once('sequence 1')
152 def test__worker_run_once_assertion_error_monitor(self):
153 runner = SearchRunner({})
154 runner.sla_action = 'monitor'
155 runner.worker_helper = mock.MagicMock(side_effect=y_exc.SLAValidationError)
157 self.assertFalse(runner._worker_run_once('sequence 1'))
159 def test__worker_run_once_non_assertion_error_none(self):
160 runner = SearchRunner({})
161 runner.worker_helper = mock.MagicMock(side_effect=RuntimeError)
163 self.assertTrue(runner._worker_run_once('sequence 1'))
165 def test__worker_run_once_non_assertion_error(self):
166 runner = SearchRunner({})
167 runner.sla_action = 'monitor'
168 runner.worker_helper = mock.MagicMock(side_effect=RuntimeError)
170 self.assertFalse(runner._worker_run_once('sequence 1'))
172 def test__worker_run(self):
173 cls = mock.MagicMock()
175 'runner': {'interval': 0, 'timeout': 1},
178 runner = SearchRunner({})
179 runner._worker_run_once = mock.MagicMock(side_effect=[0, 0, 1])
181 runner._worker_run(cls, 'my_method', scenario_cfg, {})
183 def test__worker_run_immediate_stop(self):
184 cls = mock.MagicMock()
191 runner = SearchRunner({})
192 runner._worker_run(cls, 'my_method', scenario_cfg, {})
194 @mock.patch('yardstick.benchmark.runners.search.multiprocessing')
195 def test__run_benchmark(self, mock_multi_process):
196 cls = mock.MagicMock()
201 runner = SearchRunner({})
202 runner._run_benchmark(cls, 'my_method', scenario_cfg, {})
203 mock_multi_process.Process.assert_called_once()