helix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l...@apache.org
Subject [25/33] helix git commit: Add test to test task retry with and without delay.
Date Wed, 17 Aug 2016 04:27:21 GMT
Add test to test task retry with and without delay.


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

Branch: refs/heads/helix-0.6.x
Commit: e8ecdf389daf5976b726e132d05b343986eae5b2
Parents: ddefebb
Author: Junkai Xue <jxue@linkedin.com>
Authored: Wed Apr 27 16:22:43 2016 -0700
Committer: Lei Xia <lxia@linkedin.com>
Committed: Tue Jul 5 16:17:53 2016 -0700

----------------------------------------------------------------------
 .../apache/helix/integration/task/MockTask.java | 10 ++-
 .../integration/task/TestTaskRetryDelay.java    | 82 ++++++++++++++++++++
 2 files changed, 89 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/helix/blob/e8ecdf38/helix-core/src/test/java/org/apache/helix/integration/task/MockTask.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/integration/task/MockTask.java b/helix-core/src/test/java/org/apache/helix/integration/task/MockTask.java
index f415b8e..3fe1d6f 100644
--- a/helix-core/src/test/java/org/apache/helix/integration/task/MockTask.java
+++ b/helix-core/src/test/java/org/apache/helix/integration/task/MockTask.java
@@ -19,8 +19,6 @@ package org.apache.helix.integration.task;
  * under the License.
  */
 
-import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -34,10 +32,12 @@ public class MockTask implements Task {
   public static final String TIMEOUT_CONFIG = "Timeout";
   public static final String TASK_RESULT_STATUS = "TaskResultStatus";
   public static final String THROW_EXCEPTION = "ThrowException";
+  public static final String FAILURE_COUNT_BEFORE_SUCCESS = "FailureCountBeforeSuccess";
   private final long _delay;
   private volatile boolean _canceled;
   private TaskResult.Status _taskResultStatus;
   private boolean _throwException;
+  private int _expectedToSuccess;
 
   public MockTask(TaskCallbackContext context) {
     Map<String, String> cfg = context.getJobConfig().getJobCommandConfigMap();
@@ -58,6 +58,9 @@ public class MockTask implements Task {
     _throwException = cfg.containsKey(THROW_EXCEPTION) ?
         Boolean.valueOf(cfg.containsKey(THROW_EXCEPTION)) :
         false;
+    _expectedToSuccess =
+        cfg.containsKey(FAILURE_COUNT_BEFORE_SUCCESS) ? Integer.parseInt(cfg.get(
+            FAILURE_COUNT_BEFORE_SUCCESS)) : 0;
   }
 
   @Override
@@ -74,7 +77,8 @@ public class MockTask implements Task {
     }
     timeLeft = expiry - System.currentTimeMillis();
 
-    if (_throwException) {
+    if (_throwException || _expectedToSuccess > 0) {
+      _expectedToSuccess--;
       throw new RuntimeException("Test failed");
     }
 

http://git-wip-us.apache.org/repos/asf/helix/blob/e8ecdf38/helix-core/src/test/java/org/apache/helix/integration/task/TestTaskRetryDelay.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/integration/task/TestTaskRetryDelay.java
b/helix-core/src/test/java/org/apache/helix/integration/task/TestTaskRetryDelay.java
new file mode 100644
index 0000000..7d8ebff
--- /dev/null
+++ b/helix-core/src/test/java/org/apache/helix/integration/task/TestTaskRetryDelay.java
@@ -0,0 +1,82 @@
+package org.apache.helix.integration.task;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.helix.TestHelper;
+import org.apache.helix.task.JobConfig;
+import org.apache.helix.task.TaskState;
+import org.apache.helix.task.Workflow;
+import org.testng.Assert;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import com.google.common.collect.ImmutableMap;
+
+public class TestTaskRetryDelay extends TaskTestBase {
+
+  @BeforeClass
+  public void beforeClass() throws Exception {
+    _numParitions = 1;
+    super.beforeClass();
+  }
+
+  @Test public void testTaskRetryWithDelay() throws Exception {
+    String jobResource = TestHelper.getTestMethodName();
+    JobConfig.Builder jobBuilder = JobConfig.Builder.fromMap(WorkflowGenerator.DEFAULT_JOB_CONFIG);
+    jobBuilder.setJobCommandConfigMap(WorkflowGenerator.DEFAULT_COMMAND_CONFIG)
+        .setMaxAttemptsPerTask(2).setCommand(MockTask.TASK_COMMAND)
+        .setFailureThreshold(Integer.MAX_VALUE).setTaskRetryDelay(2000L)
+        .setJobCommandConfigMap(ImmutableMap.of(MockTask.FAILURE_COUNT_BEFORE_SUCCESS, "2"));
+    Workflow flow =
+        WorkflowGenerator.generateSingleJobWorkflowBuilder(jobResource, jobBuilder).build();
+    _driver.start(flow);
+
+    // Wait until the job completes.
+    TaskTestUtil.pollForWorkflowState(_driver, jobResource, TaskState.COMPLETED);
+
+    long startTime = _driver.getWorkflowContext(jobResource).getStartTime();
+    long finishedTime = _driver.getWorkflowContext(jobResource).getFinishTime();
+
+    // It should finished at least 4 secs
+    Assert.assertTrue(finishedTime - startTime >= 2000L);
+  }
+
+  @Test public void testTaskRetryWithoutDelay() throws Exception {
+    String jobResource = TestHelper.getTestMethodName();
+    JobConfig.Builder jobBuilder = JobConfig.Builder.fromMap(WorkflowGenerator.DEFAULT_JOB_CONFIG);
+    jobBuilder.setJobCommandConfigMap(WorkflowGenerator.DEFAULT_COMMAND_CONFIG)
+        .setMaxAttemptsPerTask(2).setCommand(MockTask.TASK_COMMAND)
+        .setFailureThreshold(Integer.MAX_VALUE)
+        .setJobCommandConfigMap(ImmutableMap.of(MockTask.FAILURE_COUNT_BEFORE_SUCCESS, "1"));
+    Workflow flow =
+        WorkflowGenerator.generateSingleJobWorkflowBuilder(jobResource, jobBuilder).build();
+    _driver.start(flow);
+
+    // Wait until the job completes.
+    TaskTestUtil.pollForWorkflowState(_driver, jobResource, TaskState.COMPLETED);
+
+    long startTime = _driver.getWorkflowContext(jobResource).getStartTime();
+    long finishedTime = _driver.getWorkflowContext(jobResource).getFinishTime();
+
+    // It should finished at less than 2 sec
+    Assert.assertTrue(finishedTime - startTime <= 2000L);
+  }
+}
+


Mime
View raw message