rkhachatryan commented on a change in pull request #13351:
URL: https://github.com/apache/flink/pull/13351#discussion_r496663165



##########
File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/checkpoint/channel/ChannelStateChunkReaderTest.java
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.api.java.tuple.Tuple2;
+import org.apache.flink.core.fs.FSDataInputStream;
+import org.apache.flink.core.memory.MemorySegmentFactory;
+import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.FreeingBufferRecycler;
+import org.apache.flink.runtime.io.network.buffer.NetworkBuffer;
+import org.apache.flink.runtime.state.memory.ByteStreamStateHandle;
+
+import org.junit.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.apache.flink.util.Preconditions.checkArgument;
+import static org.apache.flink.util.Preconditions.checkState;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * {@link ChannelStateChunkReader} test.
+ */
+public class ChannelStateChunkReaderTest {
+
+       @Test(expected = TestException.class)
+       public void testBufferRecycledOnFailure() throws IOException {
+               FailingChannelStateSerializer serializer = new 
FailingChannelStateSerializer();
+               TestRecoveredChannelStateHandler handler = new 
TestRecoveredChannelStateHandler();
+
+               try (FSDataInputStream stream = geStream(serializer, 10)) {
+                       new 
ChannelStateChunkReader(serializer).readChunk(stream, 
serializer.getHeaderLength(), handler, "channelInfo");
+                       checkState(serializer.failed);
+                       checkState(!handler.requestedBuffers.isEmpty());
+               } finally {
+                       
assertTrue(handler.requestedBuffers.stream().allMatch(TestChannelStateByteBuffer::isRecycled));
+               }
+       }
+
+       @Test
+       public void testBuffersNotRequestedForEmptyStream() throws IOException {
+               ChannelStateSerializer serializer = new 
ChannelStateSerializerImpl();
+               TestRecoveredChannelStateHandler handler = new 
TestRecoveredChannelStateHandler();
+
+               try (FSDataInputStream stream = geStream(serializer, 0)) {
+                       new 
ChannelStateChunkReader(serializer).readChunk(stream, 
serializer.getHeaderLength(), handler, "channelInfo");
+               } finally {
+                       assertTrue(handler.requestedBuffers.isEmpty());
+               }
+       }
+
+       @Test
+       public void testNoSeekUnnecessarily() throws IOException {
+               final int offset = 123;
+               final FSDataInputStream stream = new FSDataInputStream() {
+                       @Override
+                       public long getPos() {
+                               return offset;
+                       }
+
+                       @Override
+                       public void seek(long ignored) {
+                               fail();
+                       }
+
+                       @Override
+                       public int read() {
+                               return 0;
+                       }
+               };
+
+               new ChannelStateChunkReader(new ChannelStateSerializerImpl())
+                       .readChunk(stream, offset, new 
TestRecoveredChannelStateHandler(), "channelInfo");
+       }
+
+       private static class TestRecoveredChannelStateHandler implements 
RecoveredChannelStateHandler<Object, Object> {
+               private final List<TestChannelStateByteBuffer> requestedBuffers 
= new ArrayList<>();
+
+               @Override
+               public Tuple2<ChannelStateByteBuffer, Object> getBuffer(Object 
o) {
+                       TestChannelStateByteBuffer buffer = new 
TestChannelStateByteBuffer();
+                       requestedBuffers.add(buffer);
+                       return Tuple2.of(buffer, null);
+               }
+
+               @Override
+               public void recover(Object o, Object o2) {
+               }
+
+               @Override
+               public void close() throws Exception {
+               }
+

Review comment:
       Removed newline.




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


Reply via email to