-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[AVRO-2245] Improve java tests for compression codecs #351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
lang/java/avro/src/test/java/org/apache/avro/file/TestAllCodecs.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* | ||
| * 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.avro.file; | ||
|
|
||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.Parameterized; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
|
|
||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| @RunWith(Parameterized.class) | ||
| public class TestAllCodecs { | ||
|
|
||
| @Parameterized.Parameters(name = "{index}: codec={0}") | ||
| public static Collection<Object[]> data() { | ||
| return Arrays.asList(new Object[][] { | ||
| { "bzip2", BZip2Codec.class }, | ||
| { "zstandard", ZstandardCodec.class }, | ||
| { "null", NullCodec.class }, | ||
| { "xz", XZCodec.class }, | ||
| { "snappy", SnappyCodec.class }, | ||
| { "deflate", DeflateCodec.class }, | ||
| }); | ||
| } | ||
|
|
||
| @Parameterized.Parameter(0) | ||
| public String codec; | ||
|
|
||
| @Parameterized.Parameter(1) | ||
| public Class<? extends Codec> codecClass; | ||
|
|
||
|
|
||
| @Test | ||
| public void testCodec() throws IOException { | ||
| int inputSize = 500_000; | ||
|
|
||
| byte[] input = generateTestData(inputSize); | ||
|
|
||
| Codec codecInstance = CodecFactory.fromString(codec).createInstance(); | ||
| assertTrue(codecClass.isInstance(codecInstance)); | ||
| assertTrue(codecInstance.getName().equals(codec)); | ||
|
|
||
| ByteBuffer inputByteBuffer = ByteBuffer.wrap(input); | ||
| ByteBuffer compressedBuffer = codecInstance.compress(inputByteBuffer); | ||
|
|
||
| int compressedSize = compressedBuffer.remaining(); | ||
|
|
||
| // Make sure something returned | ||
| assertTrue(compressedSize > 0); | ||
|
|
||
| // While the compressed size could in many real cases | ||
| // *increase* compared to the input size, our input data | ||
| // is extremely easy to compress and all Avro's compression algorithms | ||
| // should have a compression ratio greater than 1 (except 'null'). | ||
| assertTrue(compressedSize < inputSize || codec.equals("null")); | ||
|
|
||
| // Decompress the data | ||
| ByteBuffer decompressedBuffer = codecInstance.decompress(compressedBuffer); | ||
|
|
||
| // Validate the the input and output are equal. | ||
| inputByteBuffer.rewind(); | ||
| Assert.assertEquals(decompressedBuffer, inputByteBuffer); | ||
| } | ||
|
|
||
| // Generate some test data that will compress easily | ||
| public static byte[] generateTestData(int inputSize) { | ||
| byte[] arr = new byte[inputSize]; | ||
| for (int i = 0; i < arr.length; i++) { | ||
| arr[i] = (byte) (65 + i % 10); | ||
| } | ||
|
|
||
| return arr; | ||
| } | ||
| } | ||
66 changes: 0 additions & 66 deletions
66
lang/java/avro/src/test/java/org/apache/avro/file/TestBZip2Codec.java
This file was deleted.
Oops, something went wrong.
62 changes: 0 additions & 62 deletions
62
lang/java/avro/src/test/java/org/apache/avro/file/TestZstandardCodec.java
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, the original tests allocated a 2 times bigger buffer than the original input, which was supposed to test AVRO-1326 is fixed. If i'm not mistaken, with refactor this test like you did, we lose this verification.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's true. Although I'd argue that this test wasn't really successfully testing anything before. (The test doesn't even clearly verify AVRO-1326, although it sort of does...it would be much more straightforward to allocate a larger buffer and call
rewindandlimit. Instead it's actually compressing the second (empty) half of the ByteBuffer. If we allocatedinputSize * 2 + 1theassert(inputSize == outputSize)check would fail.) I honestly can't tell if that assertion passes out of lucky coincidence or if the test was written to be confusing. I think the first. :)Anyways: there's actually still a couple little bugs in that code similar to what AVRO-1326 fixes. (With how the compression code is called currently, I don't think these bugs would ever get exercised, but still...they should probably get fixed). There's another PR here: #352 to fix those.
As part of that PR I was going to add another test in
TestAllCodecsto verify the changes. Without the changes in #352 the test fails, so, not adding it here; I'll add it there once this is merged. (Basically, this tests that we do the right thing for both compression and decompression, even if we start out in somewhere in the middle of the input bytebuffer).