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_r388738882
 
 

 ##########
 File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/netty/ByteBufUtilsTest.java
 ##########
 @@ -0,0 +1,128 @@
+/*
+ * 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 {
+       private static final byte ACCUMULATION_BYTE = 0x7d;
+       private static final byte NON_ACCUMULATION_BYTE = 0x23;
+
+       @Test
+       public void testAccumulateWithoutCopy() {
+               int sourceLength = 128;
+               int sourceReaderIndex = 32;
+               int expectedAccumulationSize = 16;
+
+               ByteBuf src = createSourceBuffer(sourceLength, 
sourceReaderIndex, expectedAccumulationSize);
+               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(sourceReaderIndex, src.readerIndex());
+               verifyBufferContent(src, sourceReaderIndex, 
expectedAccumulationSize);
+       }
+
+       @Test
+       public void testAccumulateWithCopy() {
+               int sourceLength = 128;
+               int firstSourceReaderIndex = 32;
+               int secondSourceReaderIndex = 0;
+               int expectedAccumulationSize = 128;
+
+               int firstCopyLength = sourceLength - firstSourceReaderIndex;
+               int secondCopyLength = expectedAccumulationSize - 
firstCopyLength;
+
+               ByteBuf firstSource = createSourceBuffer(sourceLength, 
firstSourceReaderIndex, firstCopyLength);
+               ByteBuf secondSource = createSourceBuffer(sourceLength, 
secondSourceReaderIndex, secondCopyLength);
+
+               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(sourceLength, 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(secondSourceReaderIndex + secondCopyLength, 
secondSource.readerIndex());
+               assertEquals(expectedAccumulationSize, target.readableBytes());
+
+               verifyBufferContent(accumulated, 0, expectedAccumulationSize);
+       }
+
+       /**
+        * Create a source buffer whose length is size. The content between 
readerIndex and
 
 Review comment:
   Added the tags.

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