tillrohrmann commented on a change in pull request #11473: [FLINK-16705] Ensure 
MiniCluster shutdown does not interfere with JobResult retrieval
URL: https://github.com/apache/flink/pull/11473#discussion_r400229264
 
 

 ##########
 File path: 
flink-clients/src/test/java/org/apache/flink/client/program/PerJobMiniClusterTest.java
 ##########
 @@ -0,0 +1,234 @@
+/*
+ * 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.client.program;
+
+import org.apache.flink.api.common.JobExecutionResult;
+import org.apache.flink.api.common.JobStatus;
+import org.apache.flink.api.common.JobSubmissionResult;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.execution.JobClient;
+import org.apache.flink.runtime.execution.Environment;
+import org.apache.flink.runtime.jobgraph.JobGraph;
+import org.apache.flink.runtime.jobgraph.JobVertex;
+import org.apache.flink.runtime.jobmaster.JobResult;
+import org.apache.flink.runtime.minicluster.MiniCluster;
+import org.apache.flink.runtime.testtasks.NoOpInvokable;
+import org.apache.flink.runtime.testutils.CancelableInvokable;
+
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.mockito.Mockito;
+
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+/**
+ * Tests for {@link PerJobMiniCluster}.
+ */
+public class PerJobMiniClusterTest {
+
+       @ClassRule
+       public static TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+       private MiniCluster miniCluster;
+
+       @BeforeClass
+       public static void beforeClass() throws Exception {
+               temporaryFolder.create();
+       }
+
+       @Test
+       public void testJobExecution() throws Exception {
+               PerJobMiniCluster perJobMiniCluster = initializeMiniCluster();
+
+               JobClient jobClient = 
perJobMiniCluster.submitJob(getNoopJobGraph()).get();
+
+               JobExecutionResult jobExecutionResult = 
jobClient.getJobExecutionResult(getClass().getClassLoader()).get();
+               assertThat(jobExecutionResult, is(notNullValue()));
+
+               Map<String, Object> actual = 
jobClient.getAccumulators(getClass().getClassLoader()).get();
+               assertThat(actual, is(notNullValue()));
+
+               assertThatMiniClusterIsShutdown();
+       }
+
+       @Test
+       public void testJobClient() throws Exception {
+               PerJobMiniCluster perJobMiniCluster = initializeMiniCluster();
+
+               JobGraph cancellableJobGraph = getCancellableJobGraph();
+               JobClient jobClient = perJobMiniCluster
+                       .submitJob(cancellableJobGraph)
+                       .get();
+
+               assertThat(jobClient.getJobID(), 
is(cancellableJobGraph.getJobID()));
+               assertThat(jobClient.getJobStatus().get(), 
is(JobStatus.RUNNING));
+
+               jobClient.cancel().get();
+
+               try {
+                       
jobClient.getJobExecutionResult(getClass().getClassLoader()).get();
+               } catch (ExecutionException e) {
+                       assertThat(e.getMessage(), containsString("Failed to 
convert JobResult to JobExecutionResult."));
+               }
+
+               assertThatMiniClusterIsShutdown();
+       }
+
+       @Test
+       public void testJobClientSavepoint() throws Exception {
+               JobGraph jobGraph = new JobGraph();
+               String savepointPath = 
temporaryFolder.getRoot().getAbsolutePath();
+
+               PerJobMiniCluster perJobMiniCluster = new PerJobMiniCluster(new 
Configuration(), config -> {
+                       // Use a mock in this test case to test calling of the 
savepoint methods
+                       miniCluster = Mockito.mock(MiniCluster.class);
+
+                       CompletableFuture<JobSubmissionResult> 
jobSubmissionFuture = new CompletableFuture<>();
+                       jobSubmissionFuture.complete(new 
JobSubmissionResult(jobGraph.getJobID()));
+                       
Mockito.when(miniCluster.submitJob(Mockito.any())).thenReturn(jobSubmissionFuture);
+
+                       CompletableFuture<JobResult> jobResultFuture = new 
CompletableFuture<>();
+                       jobResultFuture.complete(
+                               new JobResult.Builder()
+                                       .jobId(jobGraph.getJobID())
+                                       .netRuntime(42L)
+                                       .build());
+                       
Mockito.when(miniCluster.requestJobResult(Mockito.any())).thenReturn(jobResultFuture);
+
+                       return miniCluster;
+               });
+
+               JobClient jobClient = 
perJobMiniCluster.submitJob(jobGraph).get();
+
+               jobClient.triggerSavepoint(savepointPath);
+               
Mockito.verify(miniCluster).triggerSavepoint(jobGraph.getJobID(), 
savepointPath, false);
+
+               jobClient.stopWithSavepoint(true, savepointPath);
+               
Mockito.verify(miniCluster).stopWithSavepoint(jobGraph.getJobID(), 
savepointPath, true);
+       }
+
+       @Test
+       public void testSubmissionError() throws Exception {
+               PerJobMiniCluster perJobMiniCluster = initializeMiniCluster();
+
+               // JobGraph is not a valid job
+               JobGraph jobGraph = new JobGraph();
+
+               try {
+                       perJobMiniCluster.submitJob(jobGraph).get();
+               } catch (ExecutionException e) {
+                       // That's what we want
+                       assertThat(e.getMessage(), containsString("Error 
submitting job to MiniCluster"));
+               }
+
+               assertThatMiniClusterIsShutdown();
+       }
+
+       @Test
+       public void testMultipleExecutions() throws Exception {
+               PerJobMiniCluster perJobMiniCluster = initializeMiniCluster();
+               {
+                       JobClient jobClient = 
perJobMiniCluster.submitJob(getNoopJobGraph()).get();
+                       
jobClient.getJobExecutionResult(getClass().getClassLoader()).get();
+                       assertThatMiniClusterIsShutdown();
+               }
+               {
+                       JobClient jobClient = 
perJobMiniCluster.submitJob(getNoopJobGraph()).get();
+                       
jobClient.getJobExecutionResult(getClass().getClassLoader()).get();
+                       assertThatMiniClusterIsShutdown();
+               }
+       }
+
+       @Test
+       public void testJobClientInteractionAfterShutdown() throws Exception {
+               PerJobMiniCluster perJobMiniCluster = initializeMiniCluster();
+               JobClient jobClient = 
perJobMiniCluster.submitJob(getNoopJobGraph()).get();
+               
jobClient.getJobExecutionResult(getClass().getClassLoader()).get();
+               assertThatMiniClusterIsShutdown();
+
+               try {
+                       jobClient.cancel();
 
 Review comment:
   `fail` is missing.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to