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_r397720848
########## File path: flink-clients/src/test/java/org/apache/flink/client/program/PerJobMiniClusterClientTest.java ########## @@ -0,0 +1,119 @@ +/* + * 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.JobID; +import org.apache.flink.api.common.JobSubmissionResult; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.runtime.jobgraph.JobGraph; +import org.apache.flink.runtime.jobmaster.JobResult; +import org.apache.flink.runtime.minicluster.MiniCluster; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.concurrent.CompletableFuture; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link PerJobMiniClusterClient}. + */ +public class PerJobMiniClusterClientTest { + + @Test + public void testClusterShutdown() { + runClusterShutdownTest(false); + } + + @Test + public void testClusterShutdownWithException() { + runClusterShutdownTest(true); + } + + @Test + public void testRequestResultBeforeSubmit() { + PerJobMiniClusterClient client = new PerJobMiniClusterClient(new Configuration(), mock(MiniCluster.class)); + try { + client.requestJobResult(JobID.generate()); + Assert.fail("This should have failed."); + } catch (IllegalStateException ignored) { + // That's what we want + } + } + + @Test + public void testSubmitTwice() { + MiniCluster miniCluster = mock(MiniCluster.class); + PerJobMiniClusterClient client = new PerJobMiniClusterClient(new Configuration(), miniCluster); + + JobGraph jobGraph = new JobGraph("test"); + when(miniCluster.submitJob(jobGraph)).thenReturn(new CompletableFuture<>()); + client.submitJob(jobGraph); + try { + client.submitJob(jobGraph); + Assert.fail("This should have failed."); + } catch (IllegalStateException ignored) { + // That's what we want + } + } + + @Test + public void testRequestResultWithDifferentJobID() { + MiniCluster miniCluster = mock(MiniCluster.class); + PerJobMiniClusterClient client = new PerJobMiniClusterClient(new Configuration(), miniCluster); + + JobGraph jobGraph = new JobGraph("test"); + when(miniCluster.submitJob(jobGraph)).thenReturn(new CompletableFuture<>()); + client.submitJob(jobGraph); + try { + client.requestJobResult(JobID.generate()); + Assert.fail("This should have failed."); + } catch (IllegalStateException ignored) { + // That's what we want + } + } + + private void runClusterShutdownTest(boolean withException) { + MiniCluster miniCluster = mock(MiniCluster.class); + CompletableFuture<JobSubmissionResult> submissionResult = new CompletableFuture<>(); + CompletableFuture<JobResult> jobResult = new CompletableFuture<>(); + + JobGraph jobGraph = new JobGraph("test"); + when(miniCluster.submitJob(jobGraph)).thenReturn(submissionResult); + when(miniCluster.requestJobResult(jobGraph.getJobID())).thenReturn(jobResult); + + PerJobMiniClusterClient clusterClient = spy(new PerJobMiniClusterClient(new Configuration(), miniCluster)); + + CompletableFuture<JobID> jobIdFuture = clusterClient.submitJob(jobGraph); + // Trigger fetching the JobResult + jobIdFuture.complete(jobGraph.getJobID()); + + if (withException) { + jobResult.completeExceptionally(new RuntimeException("oups")); + } else { + jobResult.complete(mock(JobResult.class)); + } + + verify(clusterClient).shutDownCluster(); Review comment: We should try to reduce the usage of Mockito as it makes testing code harder to refactor. I think the problem here is that we don't have a proper `MiniCluster` interface which we could instrument. Maybe we should introduce this instead of using Mockito. ---------------------------------------------------------------- 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