def setUp(self): super(TestDispatcher, self).setUp() self.context = utils.dummy_context() self.thm = scheduler.ThreadGroupManager() self.svc = service.EngineService('HOST', 'TOPIC') self.svc.engine_id = '1234'
def test_resume_action(self): mock_action = mock.Mock() mock_load = self.patchobject(actionm.Action, 'load', return_value=mock_action) tgm = scheduler.ThreadGroupManager() tgm.resume_action('action0123') mock_load.assert_called_once_with(tgm.db_session, 'action0123') mock_action.signal.assert_called_once_with(mock_action.SIG_RESUME)
def test_start(self): def f(): pass mock_group = mock.Mock() self.mock_tg.return_value = mock_group tgm = scheduler.ThreadGroupManager() tgm.start(f) mock_group.add_thread.assert_called_once_with(f)
def test_suspend_action(self): mock_action = mock.Mock() mock_load = self.patchobject(actionm.Action, 'load', return_value=mock_action) tgm = scheduler.ThreadGroupManager() tgm.suspend_action('action0123') mock_load.assert_called_once_with(tgm.db_session, 'action0123', project_safe=False) mock_action.signal.assert_called_once_with(mock_action.SIG_SUSPEND)
def test_start_action(self): mock_group = mock.Mock() self.mock_tg.return_value = mock_group tgm = scheduler.ThreadGroupManager() tgm.start_action('0123', '4567') mock_group.add_thread.assert_called_once_with(actionm.ActionProc, tgm.db_session, '0123', '4567') mock_thread = mock_group.add_thread.return_value self.assertEqual(mock_thread, tgm.workers['0123']) mock_thread.link.assert_called_once_with(mock.ANY, '0123')
def test_start_action(self, mock_action_acquire, mock_action_acquire_1st): mock_group = mock.Mock() self.mock_tg.return_value = mock_group action = mock.Mock() action.id = '0123' mock_action_acquire.return_value = action mock_action_acquire_1st.return_value = None tgm = scheduler.ThreadGroupManager() tgm.start_action('4567', '0123') mock_group.add_thread.assert_called_once_with( tgm._start_with_trace, oslo_context.get_current(), None, actionm.ActionProc, tgm.db_session, '0123')
def test_start_action_no_action_id(self, mock_acquire_action): mock_action = mock.Mock() mock_action.id = '0123' mock_action.action = 'CLUSTER_CREATE' mock_acquire_action.side_effect = [mock_action, None] mock_group = mock.Mock() self.mock_tg.return_value = mock_group tgm = scheduler.ThreadGroupManager() tgm.start_action('4567') mock_group.add_thread.assert_called_once_with( tgm._start_with_trace, oslo_context.get_current(), None, actionm.ActionProc, tgm.db_session, '0123')
def test_start_action(self, mock_action_acquire, mock_action_acquire_1st): mock_group = mock.Mock() self.mock_tg.return_value = mock_group action = mock.Mock() action.id = '0123' mock_action_acquire.return_value = action mock_action_acquire_1st.return_value = None tgm = scheduler.ThreadGroupManager() tgm.start_action('4567', '0123') mock_group.add_thread.assert_called_once_with(actionm.ActionProc, tgm.db_session, '0123') mock_thread = mock_group.add_thread.return_value self.assertEqual(mock_thread, tgm.workers['0123']) mock_thread.link.assert_called_once_with(mock.ANY, '0123')
def test_start_action_no_action_id(self, mock_acquire_action): mock_action = mock.Mock() mock_action.id = '0123' mock_action.action = 'CLUSTER_CREATE' mock_acquire_action.side_effect = [mock_action, None] mock_group = mock.Mock() self.mock_tg.return_value = mock_group tgm = scheduler.ThreadGroupManager() tgm.start_action('4567') mock_group.add_thread.assert_called_once_with(actionm.ActionProc, tgm.db_session, '0123') mock_thread = mock_group.add_thread.return_value self.assertEqual(mock_thread, tgm.workers['0123']) mock_thread.link.assert_called_once_with(mock.ANY, '0123')
def test_start_action_batch_control(self, mock_acquire_action, mock_sleep): mock_action1 = mock.Mock() mock_action1.id = 'ID1' mock_action1.action = 'NODE_CREATE' mock_action2 = mock.Mock() mock_action2.id = 'ID2' mock_action2.action = 'CLUSTER_CREATE' mock_action3 = mock.Mock() mock_action3.id = 'ID3' mock_action3.action = 'NODE_DELETE' mock_acquire_action.side_effect = [ mock_action1, mock_action2, mock_action3, None ] mock_group = mock.Mock() self.mock_tg.return_value = mock_group cfg.CONF.set_override('max_actions_per_batch', 1, enforce_type=True) cfg.CONF.set_override('batch_interval', 3, enforce_type=True) tgm = scheduler.ThreadGroupManager() tgm.start_action('4567') mock_sleep.assert_called_once_with(3)
def test_start_action_multiple_batches(self, mock_acquire_action, mock_sleep): action_types = ['NODE_CREATE', 'NODE_DELETE'] actions = [] for index in range(10): mock_action = mock.Mock() mock_action.id = 'ID%d' % (index + 1) mock_action.action = action_types[index % 2] actions.append(mock_action) # Add a None at the end to end the process. actions.insert(len(actions), None) mock_acquire_action.side_effect = actions mock_group = mock.Mock() self.mock_tg.return_value = mock_group cfg.CONF.set_override('max_actions_per_batch', 3) cfg.CONF.set_override('batch_interval', 5) tgm = scheduler.ThreadGroupManager() tgm.start_action(worker_id='4567') self.assertEqual(mock_sleep.call_count, 3) self.assertEqual(mock_group.add_thread.call_count, 10)