zhijiangW commented on a change in pull request #10375: [FLINK-14845][runtime] 
Introduce data compression to reduce disk and network IO of shuffle.
URL: https://github.com/apache/flink/pull/10375#discussion_r354093649
 
 

 ##########
 File path: 
flink-runtime/src/test/java/org/apache/flink/runtime/io/network/buffer/BufferCompressionTest.java
 ##########
 @@ -0,0 +1,205 @@
+/*
+ * 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.buffer;
+
+import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.core.memory.MemorySegmentFactory;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.Arrays;
+import java.util.Collection;
+
+import static junit.framework.TestCase.assertEquals;
+import static junit.framework.TestCase.assertFalse;
+import static junit.framework.TestCase.assertNull;
+import static junit.framework.TestCase.assertTrue;
+
+/**
+ * Tests for {@link BufferCompressor} and {@link BufferDecompressor}.
+ */
+@RunWith(Parameterized.class)
+public class BufferCompressionTest {
+
+       private static final int BUFFER_SIZE = 4 * 1024 * 1024;
+
+       private static final int NUM_LONGS = BUFFER_SIZE / 8;
+
+       private final boolean compressInPlace;
+
+       private final boolean decompressInPlace;
+
+       private final BufferCompressor compressor;
+
+       private final BufferDecompressor decompressor;
+
+       private final Buffer bufferToCompress;
+
+       @Parameters(name = "isDirect = {0}, codec = {1}, compressInPlace = {2}, 
decompressInPlace = {3}")
+       public static Collection<Object[]> parameters() {
+               return Arrays.asList(new Object[][] {
+                       {true, "LZ4", true, false},
+                       {true, "LZ4", false, true},
+                       {true, "LZ4", false, false},
+                       {false, "LZ4", true, false},
+                       {false, "LZ4", false, true},
+                       {false, "LZ4", false, false},
+               });
+       }
+
+       public BufferCompressionTest(
+                       boolean isDirect,
+                       String compressCodec,
+                       boolean compressInPlace,
+                       boolean decompressInPlace) {
+               this.compressInPlace = compressInPlace;
+               this.decompressInPlace = decompressInPlace;
+               this.compressor = new BufferCompressor(BUFFER_SIZE, 
compressCodec);
+               this.decompressor = new BufferDecompressor(BUFFER_SIZE, 
compressCodec);
+               this.bufferToCompress = 
createBufferAndFillWithLongValues(isDirect);
+       }
+
+       @Test
+       public void testCompressAndDecompressNetWorkBuffer() {
+               Buffer compressedBuffer = compress(compressor, 
bufferToCompress, compressInPlace);
+               assertTrue(compressedBuffer.isCompressed());
+
+               Buffer decompressedBuffer = decompress(decompressor, 
compressedBuffer, decompressInPlace);
+               assertFalse(decompressedBuffer.isCompressed());
+
+               verifyDecompressionResult(decompressedBuffer, 0, NUM_LONGS);
+       }
+
+       @Test
+       public void testCompressAndDecompressReadOnlySlicedNetworkBuffer() {
+               int offset = NUM_LONGS / 4 * 8;
+               int length = NUM_LONGS / 2 * 8;
+
+               Buffer readOnlySlicedBuffer = 
bufferToCompress.readOnlySlice(offset, length);
+               Buffer compressedBuffer = compress(compressor, 
readOnlySlicedBuffer, compressInPlace);
+               assertTrue(compressedBuffer.isCompressed());
+
+               Buffer decompressedBuffer = decompress(decompressor, 
compressedBuffer, decompressInPlace);
+               assertFalse(decompressedBuffer.isCompressed());
+
+               verifyDecompressionResult(decompressedBuffer, NUM_LONGS / 4, 
NUM_LONGS / 2);
+       }
+
+       @Test(expected = IllegalArgumentException.class)
+       public void testCompressEmptyBuffer() {
+               compress(compressor, bufferToCompress.readOnlySlice(0, 0), 
compressInPlace);
+       }
+
+       @Test(expected = IllegalArgumentException.class)
+       public void testDecompressEmptyBuffer() {
+               Buffer readOnlySlicedBuffer = bufferToCompress.readOnlySlice(0, 
0);
+               readOnlySlicedBuffer.setCompressed(true);
+
+               decompress(decompressor, readOnlySlicedBuffer, 
decompressInPlace);
+       }
+
+       @Test(expected = IllegalArgumentException.class)
+       public void testCompressBufferWithNonZeroReadOffset() {
+               bufferToCompress.setReaderIndex(1);
+
+               compress(compressor, bufferToCompress, compressInPlace);
+       }
+
+       @Test(expected = IllegalArgumentException.class)
+       public void testDecompressBufferWithNonZeroReadOffset() {
+               bufferToCompress.setReaderIndex(1);
+               bufferToCompress.setCompressed(true);
+
+               decompress(decompressor, bufferToCompress, decompressInPlace);
+       }
+
+       @Test(expected = IllegalArgumentException.class)
+       public void testCompressNull() {
+               assertNull(compress(compressor, null, compressInPlace));
 
 Review comment:
   `assertNull` would not be invoked because `compress()` would throw exception 
and finish the test directly. So we can remove the `assertNull`.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to