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.tests.unit import STL_MOCKS
22 STLClient = mock.MagicMock()
23 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
27 from yardstick.benchmark.runners.search import SearchRunner
28 from yardstick.benchmark.runners.search import SearchRunnerHelper
31 class TestSearchRunnerHelper(unittest.TestCase):
33 def test___call__(self):
34 cls = mock.MagicMock()
35 aborted = mock.MagicMock()
41 method = getattr(benchmark, 'my_method')
42 helper = SearchRunnerHelper(
43 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(
58 cls, 'my_method', scenario_cfg, {}, aborted)
60 with self.assertRaises(RuntimeError):
63 @mock.patch.object(time, 'sleep')
64 @mock.patch.object(time, 'time')
65 def test_is_not_done(self, mock_time, *args):
66 cls = mock.MagicMock()
67 aborted = mock.MagicMock()
72 mock_time.side_effect = range(1000)
74 helper = SearchRunnerHelper(
75 cls, 'my_method', scenario_cfg, {}, aborted)
78 for index in helper.is_not_done():
82 self.assertGreaterEqual(index, 10)
84 @mock.patch.object(time, 'sleep')
85 def test_is_not_done_immediate_stop(self, *args):
86 cls = mock.MagicMock()
87 aborted = mock.MagicMock()
94 helper = SearchRunnerHelper(
95 cls, 'my_method', scenario_cfg, {}, aborted)
98 for index in helper.is_not_done():
102 self.assertEqual(index, -1)
105 class TestSearchRunner(unittest.TestCase):
107 def test__worker_run_once(self):
109 args[-1].update(data)
121 runner = SearchRunner({})
122 runner.worker_helper = mock.MagicMock(side_effect=update)
124 self.assertFalse(runner._worker_run_once('sequence 1'))
126 def test__worker_run_once_done(self):
128 args[-1].update(data)
145 runner = SearchRunner({})
146 runner.worker_helper = mock.MagicMock(side_effect=update)
148 self.assertTrue(runner._worker_run_once('sequence 1'))
150 def test__worker_run_once_assertion_error_assert(self):
151 runner = SearchRunner({})
152 runner.sla_action = 'assert'
153 runner.worker_helper = mock.MagicMock(side_effect=AssertionError)
155 with self.assertRaises(AssertionError):
156 runner._worker_run_once('sequence 1')
158 def test__worker_run_once_assertion_error_monitor(self):
159 runner = SearchRunner({})
160 runner.sla_action = 'monitor'
161 runner.worker_helper = mock.MagicMock(side_effect=AssertionError)
163 self.assertFalse(runner._worker_run_once('sequence 1'))
165 def test__worker_run_once_non_assertion_error_none(self):
166 runner = SearchRunner({})
167 runner.worker_helper = mock.MagicMock(side_effect=RuntimeError)
169 self.assertTrue(runner._worker_run_once('sequence 1'))
171 def test__worker_run_once_non_assertion_error(self):
172 runner = SearchRunner({})
173 runner.sla_action = 'monitor'
174 runner.worker_helper = mock.MagicMock(side_effect=RuntimeError)
176 self.assertFalse(runner._worker_run_once('sequence 1'))
178 def test__worker_run(self):
179 cls = mock.MagicMock()
181 'runner': {'interval': 0, 'timeout': 1},
184 runner = SearchRunner({})
185 runner._worker_run_once = mock.MagicMock(side_effect=[0, 0, 1])
187 runner._worker_run(cls, 'my_method', scenario_cfg, {})
189 def test__worker_run_immediate_stop(self):
190 cls = mock.MagicMock()
197 runner = SearchRunner({})
198 runner._worker_run(cls, 'my_method', scenario_cfg, {})
200 @mock.patch('yardstick.benchmark.runners.search.multiprocessing')
201 def test__run_benchmark(self, mock_multi_process):
202 cls = mock.MagicMock()
207 runner = SearchRunner({})
208 runner._run_benchmark(cls, 'my_method', scenario_cfg, {})
209 self.assertEqual(mock_multi_process.Process.call_count, 1)