zhijiangW commented on a change in pull request #11515: [FLINK-16744][task] implement channel state persistence for unaligned checkpoints URL: https://github.com/apache/flink/pull/11515#discussion_r405604653
########## File path: flink-runtime/src/test/java/org/apache/flink/runtime/checkpoint/channel/ChannelStateWriteRequestExecutorImplTest.java ########## @@ -0,0 +1,203 @@ +/* + * 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.checkpoint.channel; + +import org.apache.flink.util.ExceptionUtils; +import org.apache.flink.util.function.BiConsumerWithException; + +import org.junit.Test; + +import javax.annotation.Nonnull; + +import java.io.IOException; +import java.util.Arrays; +import java.util.concurrent.LinkedBlockingDeque; + +import static org.apache.flink.runtime.checkpoint.channel.ChannelStateWriteRequestDispatcher.NO_OP; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * {@link ChannelStateWriteRequestExecutorImpl} test. + */ +public class ChannelStateWriteRequestExecutorImplTest { + + @Test(expected = IllegalStateException.class) + public void testCloseAfterSubmit() throws Exception { + testCloseAfterSubmit(ChannelStateWriteRequestExecutor::submit); + } + + @Test(expected = IllegalStateException.class) + public void testCloseAfterSubmitPriority() throws Exception { + testCloseAfterSubmit(ChannelStateWriteRequestExecutor::submitPriority); + } + + @Test + public void testSubmitFailure() throws Exception { + testSubmitFailure(ChannelStateWriteRequestExecutor::submit); + } + + @Test + public void testSubmitPriorityFailure() throws Exception { + testSubmitFailure(ChannelStateWriteRequestExecutor::submitPriority); + } + + private void testCloseAfterSubmit(BiConsumerWithException<ChannelStateWriteRequestExecutor, ChannelStateWriteRequest, Exception> requestFun) throws Exception { + WorkerClosingDeque closingDeque = new WorkerClosingDeque(); + ChannelStateWriteRequestExecutorImpl worker = new ChannelStateWriteRequestExecutorImpl(NO_OP, closingDeque); + closingDeque.setWorker(worker); + TestWriteRequest request = new TestWriteRequest(); + requestFun.accept(worker, request); + assertTrue(closingDeque.isEmpty()); + assertFalse(request.isCancelled()); + } + + private void testSubmitFailure(BiConsumerWithException<ChannelStateWriteRequestExecutor, ChannelStateWriteRequest, Exception> submitAction) throws Exception { + TestWriteRequest request = new TestWriteRequest(); + LinkedBlockingDeque<ChannelStateWriteRequest> deque = new LinkedBlockingDeque<>(); + try { + submitAction.accept(new ChannelStateWriteRequestExecutorImpl(NO_OP, deque), request); + } catch (IllegalStateException e) { + // expected: executor not started; + return; + } finally { + assertTrue(request.cancelled); + assertTrue(deque.isEmpty()); + } + throw new RuntimeException("expected exception not thrown"); + } + + @Test + @SuppressWarnings("CallToThreadRun") + public void testCleanup() throws IOException { + TestWriteRequest request = new TestWriteRequest(); + LinkedBlockingDeque<ChannelStateWriteRequest> deque = new LinkedBlockingDeque<>(); + deque.add(request); + TestRequestDispatcher requestProcessor = new TestRequestDispatcher(); + ChannelStateWriteRequestExecutorImpl worker = new ChannelStateWriteRequestExecutorImpl(requestProcessor, deque); + + worker.close(); + worker.run(); + + assertTrue(requestProcessor.isStopped()); + assertTrue(deque.isEmpty()); + assertTrue(request.isCancelled()); + } + + @Test + public void testIgnoresInterruptsWhileRunning() throws Exception { + TestRequestDispatcher requestProcessor = new TestRequestDispatcher(); + LinkedBlockingDeque<ChannelStateWriteRequest> deque = new LinkedBlockingDeque<>(); + try (ChannelStateWriteRequestExecutorImpl worker = new ChannelStateWriteRequestExecutorImpl(requestProcessor, deque)) { + worker.start(); + worker.getThread().interrupt(); + worker.submit(new TestWriteRequest()); + worker.getThread().interrupt(); + while (!deque.isEmpty()) { + Thread.sleep(100); + } + } + } + + @Test + public void testCanBeClosed() throws IOException { + TestRequestDispatcher requestProcessor = new TestRequestDispatcher(); + try (ChannelStateWriteRequestExecutorImpl worker = new ChannelStateWriteRequestExecutorImpl(requestProcessor)) { + worker.start(); + } + } + + @Test + public void testRecordsException() throws Exception { + TestException testException = new TestException(); + TestRequestDispatcher requestProcessor = new TestRequestDispatcher() { + @Override + public void dispatch(ChannelStateWriteRequest request) { + throw testException; + } + }; + LinkedBlockingDeque<ChannelStateWriteRequest> deque = new LinkedBlockingDeque<>(Arrays.asList(new TestWriteRequest())); + try (ChannelStateWriteRequestExecutorImpl worker = new ChannelStateWriteRequestExecutorImpl(requestProcessor, deque)) { Review comment: I am not quite clear what is this test motivation for. When the dispatcher throws exception, it would terminate the internal thread inside `ChannelStateWriteRequestExecutorImpl`. And when the `worker#close`, it should throw exception actually, i think we should verify the exception is same with `testException` ---------------------------------------------------------------- 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