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



##########
File path: 
flink-test-utils-parent/flink-test-utils-junit/src/main/java/org/apache/flink/testutils/junit/RetryExtension.java
##########
@@ -0,0 +1,131 @@
+/*
+ * 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.AfterAllCallback;
+import org.junit.jupiter.api.extension.Extension;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
+import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+
+/** An extension to let failed test retry. */
+public class RetryExtension implements TestTemplateInvocationContextProvider, 
AfterAllCallback {
+    public static final Logger LOG = 
LoggerFactory.getLogger(RetryExtension.class);
+    public static final ExtensionContext.Namespace RETRY_NAMESPACE =
+            ExtensionContext.Namespace.create("retryLog");
+    public static final String RETRY_KEY = "testRetry";
+
+    @Override
+    public boolean supportsTestTemplate(ExtensionContext context) {
+        Method testMethod = context.getRequiredTestMethod();
+        RetryOnFailure retryOnFailure = 
testMethod.getAnnotation(RetryOnFailure.class);
+        RetryOnException retryOnException = 
testMethod.getAnnotation(RetryOnException.class);
+
+        if (retryOnFailure == null && retryOnException == null) {
+            // if nothing is specified on the test method, fall back to 
annotations on the class
+            retryOnFailure = 
context.getTestClass().get().getAnnotation(RetryOnFailure.class);
+            retryOnException = 
context.getTestClass().get().getAnnotation(RetryOnException.class);
+        }
+        return retryOnException != null || retryOnFailure != null;
+    }
+
+    @Override
+    public Stream<TestTemplateInvocationContext> 
provideTestTemplateInvocationContexts(
+            ExtensionContext context) {
+        Method testMethod = context.getRequiredTestMethod();
+        RetryOnFailure retryOnFailure = 
testMethod.getAnnotation(RetryOnFailure.class);
+        RetryOnException retryOnException = 
testMethod.getAnnotation(RetryOnException.class);
+
+        if (retryOnFailure == null && retryOnException == null) {
+            // if nothing is specified on the test method, fall back to 
annotations on the class
+            retryOnFailure = 
context.getTestClass().get().getAnnotation(RetryOnFailure.class);

Review comment:
       I can not find other way to get this information without using 
`ExtensionContext`. But I will try to refactor this part and improve its 
readability.




-- 
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