def test_start_thread_raise_exception(self): target = mock.Mock() target.side_effect = MyException() tw = utils.ThreadsWrapper(target) tw.start_thread() tw.join_threads() self.assertEqual(type(tw.errors[0]), MyException)
def test_join_threads(self): thread_1 = mock.Mock() thread_2 = mock.Mock() tw = utils.ThreadsWrapper() tw.threads = [thread_1, thread_2] tw.join_threads() thread_1.join.assert_called_once() thread_2.join.assert_called_once()
def test_start_thread(self): target = mock.Mock() target_params = {'param1': 'val1', 'param2': 'val2'} tw = utils.ThreadsWrapper(target) tw.start_thread(**target_params) tw.join_threads() target.assert_has_calls([mock.call(param1='val1', param2='val2')]) self.assertIsInstance(tw.threads[0], threading.Thread) self.assertEqual(len(tw.errors), 0)
def _run_command(self, cmd, hosts, **kwargs): driver_host_pairs = self._map_hosts_to_driver(hosts) tw = utils.ThreadsWrapper() for driver, host in driver_host_pairs: kwargs['host'] = host fn = getattr(driver, cmd) tw.start_thread(fn, **kwargs) tw.join_threads() if tw.errors: raise error.PowerManagementError( 'There are some errors when working the driver. ' 'Please, check logs for more details.')