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.
16 from __future__ import absolute_import
21 from tests.unit import STL_MOCKS
23 STLClient = mock.MagicMock()
24 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
28 from yardstick.benchmark.runners.search import SearchRunner
29 from yardstick.benchmark.runners.search import SearchRunnerHelper
32 class TestSearchRunnerHelper(unittest.TestCase):
34 def test___call__(self):
35 cls = mock.MagicMock()
36 aborted = mock.MagicMock()
42 method = getattr(benchmark, 'my_method')
43 helper = SearchRunnerHelper(cls, 'my_method', scenario_cfg, {}, aborted)
45 with helper.get_benchmark_instance():
48 self.assertEqual(method.call_count, 1)
50 def test___call___error(self):
51 cls = mock.MagicMock()
52 aborted = mock.MagicMock()
57 helper = SearchRunnerHelper(cls, 'my_method', scenario_cfg, {}, aborted)
59 with self.assertRaises(RuntimeError):
62 @mock.patch('yardstick.benchmark.runners.search.time')
63 def test_is_not_done(self, mock_time):
64 cls = mock.MagicMock()
65 aborted = mock.MagicMock()
70 mock_time.time.side_effect = range(1000)
72 helper = SearchRunnerHelper(cls, 'my_method', scenario_cfg, {}, aborted)
75 for index in helper.is_not_done():
79 self.assertGreaterEqual(index, 10)
81 @mock.patch('yardstick.benchmark.runners.search.time')
82 def test_is_not_done_immediate_stop(self, mock_time):
83 cls = mock.MagicMock()
84 aborted = mock.MagicMock()
91 helper = SearchRunnerHelper(cls, 'my_method', scenario_cfg, {}, aborted)
94 for index in helper.is_not_done():
98 self.assertEqual(index, -1)
100 class TestSearchRunner(unittest.TestCase):
102 def test__worker_run_once(self):
104 args[-1].update(data)
116 runner = SearchRunner({})
117 runner.worker_helper = mock.MagicMock(side_effect=update)
119 self.assertFalse(runner._worker_run_once('sequence 1'))
121 def test__worker_run_once_done(self):
123 args[-1].update(data)
140 runner = SearchRunner({})
141 runner.worker_helper = mock.MagicMock(side_effect=update)
143 self.assertTrue(runner._worker_run_once('sequence 1'))
145 def test__worker_run_once_assertion_error_assert(self):
146 runner = SearchRunner({})
147 runner.sla_action = 'assert'
148 runner.worker_helper = mock.MagicMock(side_effect=AssertionError)
150 with self.assertRaises(AssertionError):
151 runner._worker_run_once('sequence 1')
153 def test__worker_run_once_assertion_error_monitor(self):
154 runner = SearchRunner({})
155 runner.sla_action = 'monitor'
156 runner.worker_helper = mock.MagicMock(side_effect=AssertionError)
158 self.assertFalse(runner._worker_run_once('sequence 1'))
160 def test__worker_run_once_non_assertion_error_none(self):
161 runner = SearchRunner({})
162 runner.worker_helper = mock.MagicMock(side_effect=RuntimeError)
164 self.assertTrue(runner._worker_run_once('sequence 1'))
166 def test__worker_run_once_non_assertion_error(self):
167 runner = SearchRunner({})
168 runner.sla_action = 'monitor'
169 runner.worker_helper = mock.MagicMock(side_effect=RuntimeError)
171 self.assertFalse(runner._worker_run_once('sequence 1'))
173 def test__worker_run(self):
174 cls = mock.MagicMock()
176 'runner': {'interval': 0, 'timeout': 1},
179 runner = SearchRunner({})
180 runner._worker_run_once = mock.MagicMock(side_effect=[0, 0, 1])
182 runner._worker_run(cls, 'my_method', scenario_cfg, {})
184 def test__worker_run_immediate_stop(self):
185 cls = mock.MagicMock()
192 runner = SearchRunner({})
193 runner._worker_run(cls, 'my_method', scenario_cfg, {})
195 @mock.patch('yardstick.benchmark.runners.search.multiprocessing')
196 def test__run_benchmark(self, mock_multi_process):
197 cls = mock.MagicMock()
202 runner = SearchRunner({})
203 runner._run_benchmark(cls, 'my_method', scenario_cfg, {})
204 self.assertEqual(mock_multi_process.Process.call_count, 1)