def test_init_error(self): with self.assertRaises(AttributeError): scheduler.TaskThread(mock.Mock()) with self.assertRaises(AttributeError): scheduler.TaskThread(None) with self.assertRaises(AttributeError): scheduler.TaskThread(scheduler.TaskBlock(0, None, None))
def test_run_success(self, mock_time): mock_time.sleep.return_value = None thread = scheduler.TaskThread(self.task_block) thread.run() self.assertEqual(thread.status, scheduler.TASK_SUCCEEDED) self.assertNotEqual(thread._get_metric("starttime"), None) self.assertNotEqual(thread._get_metric("endtime"), None) self.assertNotEqual(thread._get_metric("duration"), None)
def test_run_failure(self, mock_time): mock_time.sleep.return_value = None self.task.run.side_effect = StandardError("Test") thread = scheduler.TaskThread(self.task_block) thread.run() self.assertEqual(thread.status, scheduler.TASK_FAILED) self.assertNotEqual(thread._get_metric("starttime"), None) self.assertNotEqual(thread._get_metric("endtime"), None) self.assertNotEqual(thread._get_metric("duration"), None)
def test_set_metric(self): thread = scheduler.TaskThread(self.task_block) thread._set_metric("a", 1) thread._set_metric("b", None) self.assertEqual(thread._get_metric("a"), 1) self.assertEqual(thread._get_metric("b"), None) # reset value for name thread._set_metric("a", 2) self.assertEqual(thread._get_metric("a"), 2)
def test_init_ok(self): thread = scheduler.TaskThread(self.task_block) self.assertEqual(thread.daemon, True) self.assertEqual(thread.refresh_timeout, 0.5) self.assertEqual(thread.uid, 123) self.assertEqual(thread.status, scheduler.TASK_PENDING) self.assertEqual(thread.name, "Task[123]") self.assertEqual(thread.on_task_started, None) self.assertEqual(thread.on_task_cancelled, None) self.assertEqual(thread.on_task_succeeded, None) self.assertEqual(thread.on_task_failed, None)
def test_safe_exec(self, mock_logger): thread = scheduler.TaskThread(self.task_block) thread._safe_exec(None) self.assertEqual(mock_logger.debug.call_count, 0) # function that does not raise error ok_func = mock.Mock() thread._safe_exec(ok_func, a=1, b=2) ok_func.assert_called_once_with(a=1, b=2) # function that raises error err_func = mock.Mock() err_func.side_effect = StandardError("Test") thread._safe_exec(err_func, a=1, b=2) err_func.assert_called_once_with(a=1, b=2)
def test_cancel(self): thread = scheduler.TaskThread(self.task_block) self.assertEqual(thread.is_cancelled, False) thread.cancel() self.assertEqual(thread.is_cancelled, True)
def test_get_metric(self): thread = scheduler.TaskThread(self.task_block) thread._set_metric("a", 1) self.assertEqual(thread._get_metric("a"), 1) self.assertEqual(thread._get_metric("b"), None)