def test_with_statement(self, mock_context_manager_setup, mock_context_manager_cleanup): with context.ContextManager(mock.MagicMock()): mock_context_manager_setup.assert_called_once_with() mock_context_manager_setup.reset_mock() self.assertFalse(mock_context_manager_cleanup.called) self.assertFalse(mock_context_manager_setup.called) mock_context_manager_cleanup.assert_called_once_with()
def test_with_statement_excpetion_during_setup( self, mock_context_manager_setup, mock_context_manager_cleanup): mock_context_manager_setup.side_effect = Exception("abcdef") try: with context.ContextManager(mock.MagicMock()): pass except Exception: pass finally: mock_context_manager_setup.assert_called_once_with() mock_context_manager_cleanup.assert_called_once_with()
def test_cleanup(self, mock_context_get): mock_context = mock.MagicMock() mock_context.return_value = mock.MagicMock(__lt__=lambda x, y: True) mock_context_get.return_value = mock_context ctx_object = {"config": {"a": [], "b": []}} manager = context.ContextManager(ctx_object) manager.cleanup() mock_context_get.assert_has_calls( [mock.call("a"), mock.call("b")], any_order=True) mock_context.assert_has_calls( [mock.call(ctx_object), mock.call(ctx_object)], any_order=True) mock_context.return_value.assert_has_calls( [mock.call.cleanup(), mock.call.cleanup()], any_order=True)
def test_setup(self, mock_context_get): mock_context = mock.MagicMock() mock_context.return_value = mock.MagicMock(__lt__=lambda x, y: True) mock_context_get.return_value = mock_context ctx_object = {"config": {"a": [], "b": []}} manager = context.ContextManager(ctx_object) result = manager.setup() self.assertEqual(result, ctx_object) mock_context_get.assert_has_calls( [mock.call("a"), mock.call("b")], any_order=True) mock_context.assert_has_calls( [mock.call(ctx_object), mock.call(ctx_object)], any_order=True) self.assertEqual([mock_context(), mock_context()], manager._visited) mock_context.return_value.assert_has_calls( [mock.call.setup(), mock.call.setup()], any_order=True)
def run(self): """Run the benchmark according to the test configuration. Test configuration is specified on engine initialization. :returns: List of dicts, each dict containing the results of all the corresponding benchmark test launches """ self.task.update_status(consts.TaskStatus.RUNNING) for name in self.config: for n, kw in enumerate(self.config[name]): key = {"name": name, "pos": n, "kw": kw} LOG.info("Running benchmark with key: \n%s" % json.dumps(key, indent=2)) runner_obj = self._get_runner(kw) is_done = threading.Event() unexpected_failure = {} consumer = threading.Thread(target=self.consume_results, args=(key, self.task, is_done, unexpected_failure, runner_obj)) consumer.start() context_obj = self._prepare_context(kw.get("context", {}), name, self.admin) self.duration = 0 self.full_duration = 0 try: with rutils.Timer() as timer: with context.ContextManager(context_obj): self.duration = runner_obj.run( name, context_obj, kw.get("args", {})) except Exception as e: LOG.exception(e) unexpected_failure["exc"] = e finally: self.full_duration = timer.duration() is_done.set() consumer.join() self.task.update_status(consts.TaskStatus.FINISHED)