def test_retry_on_exception(self): """ calling _retry() should call try until the task succeeds. """ task_config = TaskConfig( {"options": {"retries": 5, "retry_interval": 1, "retry_interval_add": 1}} ) task = BaseTask(self.project_config, task_config, self.org_config) task._try = mock.Mock(side_effect=[Exception, Exception, 1]) task._retry() self.assertEqual(task._try.call_count, 3)
def test_poll(self): task = BaseTask(self.project_config, self.task_config, self.org_config) task.i = 0 def mimic_polling(): task.i += 1 if task.i > 3: task.poll_complete = True task._poll_action = mock.Mock(side_effect=mimic_polling) task._poll() self.assertEqual(4, task.poll_count) self.assertEqual(1, task.poll_interval_level) self.assertEqual(2, task.poll_interval_s)
def test_init_options__project_config_integer(self): self.project_config.config["foo"] = {"bar": 32} self.task_config.config["options"] = { "test_option": "$project_config.foo__bar" } task = BaseTask(self.project_config, self.task_config, self.org_config) self.assertEqual("32", task.options["test_option"])
def test_init_options__project_config_substitution__substring(self): self.project_config.config["foo"] = {"bar": "baz"} self.task_config.config["options"] = { "test_option": "before $project_config.foo__bar after" } task = BaseTask(self.project_config, self.task_config, self.org_config) self.assertEqual("before baz after", task.options["test_option"])
def _create_task(self, task_config=None, org_config=None): if not task_config: task_config = {} if not org_config: org_config = {} task = BaseTask( project_config=self.project_config, task_config=TaskConfig(task_config), org_config=DummyOrgConfig(org_config), ) return task
def test_init_options__not_shared(self): self.project_config.config["foo"] = {"bar": "baz"} self.task_config.config["options"] = {} task1 = BaseTask(self.project_config, self.task_config, self.org_config) self.task_config.options["test_option"] = "baz" task1._init_options({}) self.task_config.options["test_option"] = "jazz" task2 = BaseTask(self.project_config, self.task_config, self.org_config) task2._init_options({}) self.assertEqual("baz", task1.options["test_option"]) self.assertEqual("jazz", task2.options["test_option"])
def test_retry_until_too_many(self): """ calling _retry should call try until the retry count is exhausted. """ task_config = TaskConfig( {"options": {"retries": 5, "retry_interval": 1, "retry_interval_add": 1}} ) task = BaseTask(self.project_config, task_config, self.org_config) task._try = mock.Mock( side_effect=[ RuntimeError(5), RuntimeError(4), RuntimeError(3), RuntimeError(2), RuntimeError(1), RuntimeError(0), ] ) with self.assertRaises(RuntimeError) as cm: task._retry() self.assertEqual(cm.exception.args[0], 0) # assert it was the final call self.assertEqual(task._try.call_count, 6) self.assertEqual(task.options["retry_interval"], 6)
def test_poll_action(self): task = BaseTask(self.project_config, self.task_config, self.org_config) with self.assertRaises(NotImplementedError): task._poll_action()
def test_is_retry_valid(self): task = BaseTask(self.project_config, self.task_config, self.org_config) self.assertTrue(task._is_retry_valid(None))
def test_run_task(self): task = BaseTask(self.project_config, self.task_config, self.org_config) with self.assertRaises(NotImplementedError): task()
def test_option_overrides(self): task = BaseTask( self.project_config, self.task_config, self.org_config, foo="bar" ) self.assertEqual("bar", task.options["foo"])
def test_task_is_callable(self): """ BaseTask is Callable """ task = BaseTask(self.project_config, self.task_config, self.org_config) self.assertIsInstance(task, collections.abc.Callable)