ruanhang1993 commented on a change in pull request #17556:
URL: https://github.com/apache/flink/pull/17556#discussion_r747349693



##########
File path: 
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/RetryTestExecutionExtension.java
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.
+ */
+
+package org.apache.flink.testutils.junit;
+
+import org.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.ConditionEvaluationResult;
+import org.junit.jupiter.api.extension.ExecutionCondition;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
+import org.opentest4j.TestAbortedException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Map;
+
+import static org.apache.flink.testutils.junit.RetryExtension.RETRY_KEY;
+import static org.apache.flink.testutils.junit.RetryExtension.RETRY_NAMESPACE;
+import static org.apache.flink.testutils.junit.RetryExtension.getTestMethodKey;
+
+/** Extension to decide whether a retry test should run. */
+public class RetryTestExecutionExtension
+        implements ExecutionCondition, TestExecutionExceptionHandler, 
AfterEachCallback {
+    public static final Logger LOG = 
LoggerFactory.getLogger(RetryTestExecutionExtension.class);
+    private final int retryIndex;
+    private final int totalTimes;
+    private Class<? extends Throwable> repeatableException;
+
+    public RetryTestExecutionExtension(
+            int retryIndex, int totalTimes, Class<? extends Throwable> 
repeatableException) {
+        this.retryIndex = retryIndex;
+        this.totalTimes = totalTimes;
+        this.repeatableException = repeatableException;
+    }
+
+    @Override
+    public ConditionEvaluationResult 
evaluateExecutionCondition(ExtensionContext context) {
+        Map<String, Boolean> shouldRetry =
+                (Map<String, Boolean>) 
context.getStore(RETRY_NAMESPACE).get(RETRY_KEY);
+        String method = getTestMethodKey(context);
+        if (!shouldRetry.get(method)) {
+            return ConditionEvaluationResult.disabled(method + "have already 
passed or failed.");
+        }
+        return ConditionEvaluationResult.enabled(
+                String.format("Test %s: [%d/%d]", method, retryIndex, 
totalTimes));
+    }
+
+    @Override
+    public void handleTestExecutionException(ExtensionContext context, 
Throwable throwable)
+            throws Throwable {
+        String method = getTestMethodKey(context);
+        if (repeatableException != null) {
+            // RetryOnException
+            if (repeatableException.isAssignableFrom(throwable.getClass())) {
+                // 1. continue retrying when get some repeatable exceptions
+                String retryMsg =
+                        String.format(
+                                "Retry test %s[%d/%d] failed with repeatable 
exception, continue retrying.",
+                                method, retryIndex, totalTimes);
+                LOG.error(retryMsg, throwable);
+                throw new TestAbortedException(retryMsg);

Review comment:
       Because I do not find a way to control the execution of test cases 
except `ExecutionCondition`, the behavior of `RetryExtension` is different from 
`RetryRule`.
   When use the `RetryExtension`, the test case will be planned to execute 
fixed times as the `times` in `RetryOnException` and `RetryOnFailure`. We could 
control which attempt to skip by `ExecutionCondition`. When we throw 
`TestAbortedException` for a test case, this test case will not be marked as 
failed, but as ignored. 
   Compare the test result of `RetryOnExceptionExtensionTest`(RetryExtension) 
and `RetryOnExceptionTest`(RetryRule), the difference  could be seen more 
clearly.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to