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
24 class TestSearchRunnerHelper(unittest.TestCase):
26 def test___call__(self):
27 cls = mock.MagicMock()
28 aborted = mock.MagicMock()
34 method = getattr(benchmark, 'my_method')
35 helper = SearchRunnerHelper(
36 cls, 'my_method', scenario_cfg, {}, aborted)
38 with helper.get_benchmark_instance():
41 self.assertEqual(method.call_count, 1)
43 def test___call___error(self):
44 cls = mock.MagicMock()
45 aborted = mock.MagicMock()
50 helper = SearchRunnerHelper(
51 cls, 'my_method', scenario_cfg, {}, aborted)
53 with self.assertRaises(RuntimeError):
56 @mock.patch.object(time, 'sleep')
57 @mock.patch.object(time, 'time')
58 def test_is_not_done(self, mock_time, *args):
59 cls = mock.MagicMock()
60 aborted = mock.MagicMock()
65 mock_time.side_effect = range(1000)
67 helper = SearchRunnerHelper(
68 cls, 'my_method', scenario_cfg, {}, aborted)
71 for index in helper.is_not_done():
75 self.assertGreaterEqual(index, 10)
77 @mock.patch.object(time, 'sleep')
78 def test_is_not_done_immediate_stop(self, *args):
79 cls = mock.MagicMock()
80 aborted = mock.MagicMock()
87 helper = SearchRunnerHelper(
88 cls, 'my_method', scenario_cfg, {}, aborted)
91 for index in helper.is_not_done():
95 self.assertEqual(index, -1)
98 class TestSearchRunner(unittest.TestCase):
100 def test__worker_run_once(self):
102 args[-1].update(data)
114 runner = SearchRunner({})
115 runner.worker_helper = mock.MagicMock(side_effect=update)
117 self.assertFalse(runner._worker_run_once('sequence 1'))
119 def test__worker_run_once_done(self):
121 args[-1].update(data)
138 runner = SearchRunner({})
139 runner.worker_helper = mock.MagicMock(side_effect=update)
141 self.assertTrue(runner._worker_run_once('sequence 1'))
143 def test__worker_run_once_assertion_error_assert(self):
144 runner = SearchRunner({})
145 runner.sla_action = 'assert'
146 runner.worker_helper = mock.MagicMock(side_effect=AssertionError)
148 with self.assertRaises(AssertionError):
149 runner._worker_run_once('sequence 1')
151 def test__worker_run_once_assertion_error_monitor(self):
152 runner = SearchRunner({})
153 runner.sla_action = 'monitor'
154 runner.worker_helper = mock.MagicMock(side_effect=AssertionError)
156 self.assertFalse(runner._worker_run_once('sequence 1'))
158 def test__worker_run_once_non_assertion_error_none(self):
159 runner = SearchRunner({})
160 runner.worker_helper = mock.MagicMock(side_effect=RuntimeError)
162 self.assertTrue(runner._worker_run_once('sequence 1'))
164 def test__worker_run_once_non_assertion_error(self):
165 runner = SearchRunner({})
166 runner.sla_action = 'monitor'
167 runner.worker_helper = mock.MagicMock(side_effect=RuntimeError)
169 self.assertFalse(runner._worker_run_once('sequence 1'))
171 def test__worker_run(self):
172 cls = mock.MagicMock()
174 'runner': {'interval': 0, 'timeout': 1},
177 runner = SearchRunner({})
178 runner._worker_run_once = mock.MagicMock(side_effect=[0, 0, 1])
180 runner._worker_run(cls, 'my_method', scenario_cfg, {})
182 def test__worker_run_immediate_stop(self):
183 cls = mock.MagicMock()
190 runner = SearchRunner({})
191 runner._worker_run(cls, 'my_method', scenario_cfg, {})
193 @mock.patch('yardstick.benchmark.runners.search.multiprocessing')
194 def test__run_benchmark(self, mock_multi_process):
195 cls = mock.MagicMock()
200 runner = SearchRunner({})
201 runner._run_benchmark(cls, 'my_method', scenario_cfg, {})
202 self.assertEqual(mock_multi_process.Process.call_count, 1)