You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2021/10/26 02:06:05 UTC

[GitHub] [flink] ruanhang1993 commented on a change in pull request #17556: [FLINK-24627][tests] add some Junit5 extensions to replace the existed Junit4 rules

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



##########
File path: flink-runtime/src/test/java/org/apache/flink/runtime/testutils/MiniClusterExtension.java
##########
@@ -0,0 +1,215 @@
+/*
+ * 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.runtime.testutils;
+
+import org.apache.flink.api.common.time.Deadline;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.configuration.CoreOptions;
+import org.apache.flink.configuration.JobManagerOptions;
+import org.apache.flink.configuration.MemorySize;
+import org.apache.flink.configuration.RestOptions;
+import org.apache.flink.configuration.TaskManagerOptions;
+import org.apache.flink.configuration.UnmodifiableConfiguration;
+import org.apache.flink.core.testutils.CustomExtension;
+import org.apache.flink.runtime.messages.Acknowledge;
+import org.apache.flink.runtime.minicluster.MiniCluster;
+import org.apache.flink.runtime.minicluster.MiniClusterConfiguration;
+import org.apache.flink.util.ExceptionUtils;
+import org.apache.flink.util.Preconditions;
+import org.apache.flink.util.concurrent.FutureUtils;
+
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.rules.TemporaryFolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.URI;
+import java.time.Duration;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+/** Extension which starts a {@link MiniCluster} for testing purposes. */
+public class MiniClusterExtension implements CustomExtension {
+    private static final MemorySize DEFAULT_MANAGED_MEMORY_SIZE = MemorySize.parse("80m");
+
+    protected final Logger log = LoggerFactory.getLogger(getClass());
+
+    private final TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+    private final MiniClusterResourceConfiguration miniClusterResourceConfiguration;
+
+    private MiniCluster miniCluster = null;
+
+    private int numberSlots = -1;
+
+    private UnmodifiableConfiguration restClusterClientConfig;
+
+    public MiniClusterExtension(
+            final MiniClusterResourceConfiguration miniClusterResourceConfiguration) {
+        this.miniClusterResourceConfiguration =
+                Preconditions.checkNotNull(miniClusterResourceConfiguration);
+    }
+
+    public int getNumberSlots() {
+        return numberSlots;
+    }
+
+    public MiniCluster getMiniCluster() {
+        return miniCluster;
+    }
+
+    public UnmodifiableConfiguration getClientConfiguration() {
+        return restClusterClientConfig;
+    }
+
+    public URI getRestAddres() {
+        return miniCluster.getRestAddress().join();
+    }
+
+    @Override
+    public void before(ExtensionContext context) throws Exception {
+        temporaryFolder.create();
+
+        startMiniCluster();
+
+        numberSlots =
+                miniClusterResourceConfiguration.getNumberSlotsPerTaskManager()
+                        * miniClusterResourceConfiguration.getNumberTaskManagers();
+    }
+
+    public void cancelAllJobs() {
+        try {
+            final Deadline jobCancellationDeadline =
+                    Deadline.fromNow(
+                            Duration.ofMillis(
+                                    miniClusterResourceConfiguration
+                                            .getShutdownTimeout()
+                                            .toMilliseconds()));
+
+            final List<CompletableFuture<Acknowledge>> jobCancellationFutures =
+                    miniCluster.listJobs()
+                            .get(
+                                    jobCancellationDeadline.timeLeft().toMillis(),
+                                    TimeUnit.MILLISECONDS)
+                            .stream()
+                            .filter(status -> !status.getJobState().isGloballyTerminalState())
+                            .map(status -> miniCluster.cancelJob(status.getJobId()))
+                            .collect(Collectors.toList());
+
+            FutureUtils.waitForAll(jobCancellationFutures)
+                    .get(jobCancellationDeadline.timeLeft().toMillis(), TimeUnit.MILLISECONDS);
+
+            CommonTestUtils.waitUntilCondition(
+                    () -> {
+                        final long unfinishedJobs =
+                                miniCluster.listJobs()
+                                        .get(
+                                                jobCancellationDeadline.timeLeft().toMillis(),
+                                                TimeUnit.MILLISECONDS)
+                                        .stream()
+                                        .filter(
+                                                status ->
+                                                        !status.getJobState()
+                                                                .isGloballyTerminalState())
+                                        .count();
+                        return unfinishedJobs == 0;
+                    },
+                    jobCancellationDeadline);
+        } catch (Exception e) {
+            log.warn("Exception while shutting down remaining jobs.", e);

Review comment:
       I don't think it is a failure for the test when cancelling jobs fails. Cancelling jobs is not a part of the test after all. 




-- 
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-unsubscribe@flink.apache.org

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