gaoyunhaii commented on a change in pull request #7368: [FLINK-10742][network] 
Let Netty use Flink's buffers directly in credit-based mode
URL: https://github.com/apache/flink/pull/7368#discussion_r386162539
 
 

 ##########
 File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/netty/ByteBufUtilsTest.java
 ##########
 @@ -0,0 +1,111 @@
+/*
+ * 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.io.network.netty;
+
+import org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf;
+import org.apache.flink.shaded.netty4.io.netty.buffer.Unpooled;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+/**
+ * Tests the methods in {@link ByteBufUtils}.
+ */
+public class ByteBufUtilsTest {
+
+       @Test
+       public void testAccumulateWithoutCopy() {
+               final int sourceLength = 128;
+               final int sourceStartPosition = 32;
+               final int expectedAccumulationSize = 16;
+
+               ByteBuf src = createSourceBuffer(sourceLength, 
sourceStartPosition);
+
+               ByteBuf target = Unpooled.buffer(expectedAccumulationSize);
+
+               // If src has enough data and no data has been copied yet, src 
will be returned without modification.
+               ByteBuf accumulated = ByteBufUtils.accumulate(target, src, 
expectedAccumulationSize, target.readableBytes());
+
+               assertSame(src, accumulated);
+               assertEquals(sourceStartPosition, src.readerIndex());
+
+               verifyBufferContent(src, sourceStartPosition, sourceLength - 
sourceStartPosition, sourceStartPosition);
+       }
+
+       @Test
+       public void testAccumulateWithCopy() {
+               final int firstSourceLength = 128;
+               final int firstSourceStartPosition = 32;
+               final int secondSourceLength = 64;
+               final int secondSourceStartPosition = 0;
+               final int expectedAccumulationSize = 128;
+
+               final int firstCopyLength = firstSourceLength - 
firstSourceStartPosition;
+               final int secondCopyLength = expectedAccumulationSize - 
firstCopyLength;
+
+               ByteBuf firstSource = createSourceBuffer(firstSourceLength, 
firstSourceStartPosition);
+               ByteBuf secondSource = createSourceBuffer(secondSourceLength, 
secondSourceStartPosition);
+
+               ByteBuf target = Unpooled.buffer(expectedAccumulationSize);
+
+               // If src does not have enough data, src will be copied into 
target and null will be returned.
+               ByteBuf accumulated = ByteBufUtils.accumulate(
+                       target,
+                       firstSource,
+                       expectedAccumulationSize,
+                       target.readableBytes());
+               assertNull(accumulated);
+               assertEquals(firstSourceLength, firstSource.readerIndex());
+               assertEquals(firstCopyLength, target.readableBytes());
+
+               // The remaining data will be copied from the second buffer, 
and the target buffer will be returned
+               // after all data is accumulated.
+               accumulated = ByteBufUtils.accumulate(
+                       target,
+                       secondSource,
+                       expectedAccumulationSize,
+                       target.readableBytes());
+               assertSame(target, accumulated);
+               assertEquals(secondSourceStartPosition + secondCopyLength, 
secondSource.readerIndex());
+               assertEquals(expectedAccumulationSize, target.readableBytes());
+
+               verifyBufferContent(accumulated, 0, firstCopyLength, 
firstSourceStartPosition);
 
 Review comment:
   The previous checking is split into two parts since the content is not the 
same byte value. According to the following comments, the content is changed to 
the same byte value, thus the two checking is also merged.

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