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_r405425100
 
 

 ##########
 File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/checkpoint/channel/ChannelStateReaderImplTest.java
 ##########
 @@ -0,0 +1,205 @@
+package org.apache.flink.runtime.checkpoint.channel;
+/*
+ * 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.
+ */
+
+import org.apache.flink.core.memory.HeapMemorySegment;
+import org.apache.flink.runtime.checkpoint.OperatorSubtaskState;
+import org.apache.flink.runtime.checkpoint.StateObjectCollection;
+import org.apache.flink.runtime.checkpoint.TaskStateSnapshot;
+import org.apache.flink.runtime.io.network.buffer.FreeingBufferRecycler;
+import org.apache.flink.runtime.io.network.buffer.NetworkBuffer;
+import org.apache.flink.runtime.jobgraph.OperatorID;
+import org.apache.flink.runtime.state.InputChannelStateHandle;
+import org.apache.flink.runtime.state.memory.ByteStreamStateHandle;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+
+import static java.util.Collections.singletonList;
+import static java.util.stream.Collectors.toMap;
+import static 
org.apache.flink.runtime.checkpoint.channel.ChannelStateReader.ReadResult.HAS_MORE_DATA;
+import static 
org.apache.flink.runtime.checkpoint.channel.ChannelStateReader.ReadResult.NO_MORE_DATA;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * {@link ChannelStateReaderImpl} test.
+ */
+public class ChannelStateReaderImplTest {
+
+       private static final InputChannelInfo CHANNEL = new InputChannelInfo(1, 
2);
+       private static final byte[] DATA = generateData(10);
+       private ChannelStateReaderImpl reader;
+
+       @Before
+       public void init() {
+               reader = getReader(CHANNEL, DATA);
+       }
+
+       @After
+       public void tearDown() throws Exception {
+               reader.close();
+       }
+
+       @Test
+       public void testDifferentBufferSizes() throws Exception {
+               for (int bufferSize = 1; bufferSize < 2 * DATA.length; 
bufferSize++) {
+                       try (ChannelStateReaderImpl reader = getReader(CHANNEL, 
DATA)) { // re-create reader to re-read the same channel
+                               readAndVerify(bufferSize, CHANNEL, DATA, 
reader);
+                       }
+               }
+       }
+
+       @Test
+       public void testWithOffsets() throws IOException {
+               Map<InputChannelStateHandle, byte[]> handlesAndBytes = 
generateHandlesWithBytes(10, 20);
+               ChannelStateReader reader = new 
ChannelStateReaderImpl(taskStateSnapshot(handlesAndBytes.keySet()), new 
ChannelStateSerializerImpl());
+               for (Map.Entry<InputChannelStateHandle, byte[]> e : 
handlesAndBytes.entrySet()) {
+                       readAndVerify(42, e.getKey().getInfo(), e.getValue(), 
reader);
+               }
+       }
+
+       @Test(expected = Exception.class)
+       public void testReadOnlyOnce() throws IOException {
+               reader.readInputData(CHANNEL, getBuffer(DATA.length));
+               reader.readInputData(CHANNEL, getBuffer(DATA.length));
+       }
+
+       @Test(expected = Exception.class)
+       public void testReadClosed() throws Exception {
+               reader.close();
+               reader.readInputData(CHANNEL, getBuffer(DATA.length));
+       }
+
+       @Test(expected = IllegalArgumentException.class)
+       public void testReadWrongChannelState() throws IOException {
+               InputChannelInfo wrongChannel = new 
InputChannelInfo(CHANNEL.getGateIdx() + 1, CHANNEL.getInputChannelIdx() + 1);
+               reader.readInputData(wrongChannel, getBuffer(DATA.length));
+       }
+
+       private TaskStateSnapshot 
taskStateSnapshot(Collection<InputChannelStateHandle> inputChannelStateHandles) 
{
+               return new TaskStateSnapshot(Collections.singletonMap(
+                       new OperatorID(),
+                       new OperatorSubtaskState(
+                               StateObjectCollection.empty(),
+                               StateObjectCollection.empty(),
+                               StateObjectCollection.empty(),
+                               StateObjectCollection.empty(),
+                               new 
StateObjectCollection<>(inputChannelStateHandles),
+                               StateObjectCollection.empty()
+                       )));
+       }
+
+       private static byte[] generateData(int len) {
 
 Review comment:
   nit: this can be reused in other places, like 
`ChannelStateSerializerImplTest#getData(int len)`, 
`ChannelStateSerializerTest#randomBytes`

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