You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ariatosca.apache.org by da...@apache.org on 2016/11/08 12:29:18 UTC

[10/11] incubator-ariatosca git commit: add tests

add tests


Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/afe39fa5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/afe39fa5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/afe39fa5

Branch: refs/heads/ARIA-10-task-retries
Commit: afe39fa5e58c3b474f2f5d4edeb576dac2321efb
Parents: 5cdf148
Author: Dan Kilman <da...@gigaspaces.com>
Authored: Tue Nov 8 14:09:35 2016 +0200
Committer: Dan Kilman <da...@gigaspaces.com>
Committed: Tue Nov 8 14:09:35 2016 +0200

----------------------------------------------------------------------
 aria/storage/models.py              |   2 +-
 aria/storage/structures.py          |  10 +--
 tests/workflows/core/test_engine.py | 116 ++++++++++++++++++++++++++++++-
 3 files changed, 119 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/afe39fa5/aria/storage/models.py
----------------------------------------------------------------------
diff --git a/aria/storage/models.py b/aria/storage/models.py
index d262825..e74d733 100644
--- a/aria/storage/models.py
+++ b/aria/storage/models.py
@@ -400,7 +400,7 @@ class Task(Model):
     ended_at = Field(type=datetime, default=None)
     max_retries = Field(type=int, default=1)
     retry_count = Field(type=int, default=0)
-    retry_interval = Field(type=int, default=0)
+    retry_interval = Field(type=(int, float), default=0)
 
     # Operation specific fields
     name = Field(type=basestring)

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/afe39fa5/aria/storage/structures.py
----------------------------------------------------------------------
diff --git a/aria/storage/structures.py b/aria/storage/structures.py
index ea4cf3a..c692d36 100644
--- a/aria/storage/structures.py
+++ b/aria/storage/structures.py
@@ -195,17 +195,17 @@ class Model(object):
         :param fields: each item is validated and transformed into instance attributes.
         """
         self._assert_model_have_id_field(**fields)
-        missing_fileds, unexpected_fileds = self._setup_fields(fields)
+        missing_fields, unexpected_fields = self._setup_fields(fields)
 
-        if missing_fileds:
+        if missing_fields:
             raise StorageError(
                 'Model {name} got missing keyword arguments: {fields}'.format(
-                    name=self.__class__.__name__, fields=missing_fileds))
+                    name=self.__class__.__name__, fields=missing_fields))
 
-        if unexpected_fileds:
+        if unexpected_fields:
             raise StorageError(
                 'Model {name} got unexpected keyword arguments: {fields}'.format(
-                    name=self.__class__.__name__, fields=unexpected_fileds))
+                    name=self.__class__.__name__, fields=unexpected_fields))
 
     def __repr__(self):
         return '{name}(fields={0})'.format(sorted(self.fields), name=self.__class__.__name__)

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/afe39fa5/tests/workflows/core/test_engine.py
----------------------------------------------------------------------
diff --git a/tests/workflows/core/test_engine.py b/tests/workflows/core/test_engine.py
index 27eb0a7..db03761 100644
--- a/tests/workflows/core/test_engine.py
+++ b/tests/workflows/core/test_engine.py
@@ -13,8 +13,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from datetime import datetime
 import logging
+import time
+from datetime import datetime
 
 import pytest
 
@@ -45,13 +46,15 @@ class BaseTest(object):
         eng.execute()
 
     @staticmethod
-    def _op(func, ctx, inputs=None):
+    def _op(func, ctx, inputs=None, max_retries=None, retry_interval=None):
         return api.task.OperationTask(
             name='task',
             operation_details={'operation': 'tests.workflows.core.test_engine.{name}'.format(
                 name=func.__name__)},
             node_instance=ctx.model.node_instance.get('dependency_node_instance'),
-            inputs=inputs
+            inputs=inputs,
+            max_retries=max_retries,
+            retry_interval=retry_interval
         )
 
     @pytest.fixture(scope='function', autouse=True)
@@ -199,6 +202,104 @@ class TestEngine(BaseTest):
         assert global_test_holder.get('sent_task_signal_calls') == 2
 
 
+class TestRetries(BaseTest):
+
+    def test_one_max_retries_and_success_on_retry(self, workflow_context, executor):
+        @workflow
+        def mock_workflow(ctx, graph):
+            op = self._op(mock_conditional_failure_task, ctx,
+                          inputs={'fail_on_invocations_less_than': 1},
+                          max_retries=1)
+            graph.add_tasks(op)
+        self._execute(
+            workflow_func=mock_workflow,
+            workflow_context=workflow_context,
+            executor=executor)
+        assert workflow_context.states == ['start', 'success']
+        assert workflow_context.exception is None
+        assert len(global_test_holder.get('invocations', [])) == 2
+        assert global_test_holder.get('sent_task_signal_calls') == 2
+
+    def test_one_max_retries_and_failure_on_retry(self, workflow_context, executor):
+        @workflow
+        def mock_workflow(ctx, graph):
+            op = self._op(mock_conditional_failure_task, ctx,
+                          inputs={'fail_on_invocations_less_than': 2},
+                          max_retries=1)
+            graph.add_tasks(op)
+        with pytest.raises(exceptions.ExecutorException):
+            self._execute(
+                workflow_func=mock_workflow,
+                workflow_context=workflow_context,
+                executor=executor)
+        assert workflow_context.states == ['start', 'failure']
+        assert isinstance(workflow_context.exception, exceptions.ExecutorException)
+        assert len(global_test_holder.get('invocations', [])) == 2
+        assert global_test_holder.get('sent_task_signal_calls') == 2
+
+    def test_two_max_retries_and_success_on_first_retry(self, workflow_context, executor):
+        @workflow
+        def mock_workflow(ctx, graph):
+            op = self._op(mock_conditional_failure_task, ctx,
+                          inputs={'fail_on_invocations_less_than': 1},
+                          max_retries=2)
+            graph.add_tasks(op)
+        self._execute(
+            workflow_func=mock_workflow,
+            workflow_context=workflow_context,
+            executor=executor)
+        assert workflow_context.states == ['start', 'success']
+        assert workflow_context.exception is None
+        assert len(global_test_holder.get('invocations', [])) == 2
+        assert global_test_holder.get('sent_task_signal_calls') == 2
+
+    def test_two_max_retries_and_success_on_second_retry(self, workflow_context, executor):
+        @workflow
+        def mock_workflow(ctx, graph):
+            op = self._op(mock_conditional_failure_task, ctx,
+                          inputs={'fail_on_invocations_less_than': 2},
+                          max_retries=2)
+            graph.add_tasks(op)
+        self._execute(
+            workflow_func=mock_workflow,
+            workflow_context=workflow_context,
+            executor=executor)
+        assert workflow_context.states == ['start', 'success']
+        assert workflow_context.exception is None
+        assert len(global_test_holder.get('invocations', [])) == 3
+        assert global_test_holder.get('sent_task_signal_calls') == 3
+
+    def test_retry_interval_float(self, workflow_context, executor):
+        self._test_retry_interval(retry_interval=0.3,
+                                  workflow_context=workflow_context,
+                                  executor=executor)
+
+    def test_retry_interval_int(self, workflow_context, executor):
+        self._test_retry_interval(retry_interval=1,
+                                  workflow_context=workflow_context,
+                                  executor=executor)
+
+    def _test_retry_interval(self, retry_interval, workflow_context, executor):
+        @workflow
+        def mock_workflow(ctx, graph):
+            op = self._op(mock_conditional_failure_task, ctx,
+                          inputs={'fail_on_invocations_less_than': 1},
+                          max_retries=1,
+                          retry_interval=retry_interval)
+            graph.add_tasks(op)
+        self._execute(
+            workflow_func=mock_workflow,
+            workflow_context=workflow_context,
+            executor=executor)
+        assert workflow_context.states == ['start', 'success']
+        assert workflow_context.exception is None
+        invocations = global_test_holder.get('invocations', [])
+        assert len(invocations) == 2
+        invocation1, invocation2 = invocations
+        assert invocation2 - invocation1 >= retry_interval
+        assert global_test_holder.get('sent_task_signal_calls') == 2
+
+
 def mock_success_task():
     pass
 
@@ -210,3 +311,12 @@ def mock_failed_task():
 def mock_ordered_task(counter):
     invocations = global_test_holder.setdefault('invocations', [])
     invocations.append(counter)
+
+
+def mock_conditional_failure_task(fail_on_invocations_less_than):
+    invocations = global_test_holder.setdefault('invocations', [])
+    try:
+        if len(invocations) < fail_on_invocations_less_than:
+            raise RuntimeError
+    finally:
+        invocations.append(time.time())