Ensure tests pass when sun.misc.Unsafe is not present
Motivation: We need to ensure we pass all tests when sun.misc.Unsafe is not present. Modifications: - Make *ByteBufAllocatorTest work whenever sun.misc.Unsafe is present or not - Let Lz4FrameEncoderTest not depend on AbstractByteBufAllocator implementation details which take into account if sun.misc.Unsafe is present or not Result: Tests pass even without sun.misc.Unsafe.
This commit is contained in:
parent
54339c08ac
commit
9b2b3e2512
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package io.netty.buffer;
|
||||
|
||||
import io.netty.util.internal.PlatformDependent;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@ -25,6 +26,11 @@ public abstract class AbstractByteBufAllocatorTest extends ByteBufAllocatorTest
|
||||
@Override
|
||||
protected abstract AbstractByteBufAllocator newAllocator(boolean preferDirect);
|
||||
|
||||
@Override
|
||||
protected boolean isDirectExpected(boolean preferDirect) {
|
||||
return preferDirect && PlatformDependent.hasUnsafe();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final int defaultMaxCapacity() {
|
||||
return AbstractByteBufAllocator.DEFAULT_MAX_CAPACITY;
|
||||
|
@ -37,7 +37,7 @@ public abstract class ByteBufAllocatorTest {
|
||||
ByteBufAllocator allocator = newAllocator(preferDirect);
|
||||
ByteBuf buffer = allocator.buffer(1);
|
||||
try {
|
||||
assertBuffer(buffer, preferDirect, 1, defaultMaxCapacity());
|
||||
assertBuffer(buffer, isDirectExpected(preferDirect), 1, defaultMaxCapacity());
|
||||
} finally {
|
||||
buffer.release();
|
||||
}
|
||||
@ -53,12 +53,14 @@ public abstract class ByteBufAllocatorTest {
|
||||
ByteBufAllocator allocator = newAllocator(preferDirect);
|
||||
ByteBuf buffer = allocator.buffer(1, maxCapacity);
|
||||
try {
|
||||
assertBuffer(buffer, preferDirect, 1, maxCapacity);
|
||||
assertBuffer(buffer, isDirectExpected(preferDirect), 1, maxCapacity);
|
||||
} finally {
|
||||
buffer.release();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract boolean isDirectExpected(boolean preferDirect);
|
||||
|
||||
@Test
|
||||
public void testHeapBuffer() {
|
||||
testHeapBuffer(true);
|
||||
|
@ -18,6 +18,7 @@ package io.netty.handler.codec.compression;
|
||||
import java.io.InputStream;
|
||||
import java.util.zip.Checksum;
|
||||
|
||||
import io.netty.util.internal.PlatformDependent;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@ -37,6 +38,7 @@ import org.mockito.MockitoAnnotations;
|
||||
|
||||
import static io.netty.handler.codec.compression.Lz4Constants.DEFAULT_SEED;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class Lz4FrameEncoderTest extends AbstractEncoderTest {
|
||||
@ -111,7 +113,7 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest {
|
||||
testAllocateBuffer(blockSize, NONALLOCATABLE_SIZE, false);
|
||||
}
|
||||
|
||||
private void testAllocateBuffer(int blockSize, int bufSize, boolean isDirect) {
|
||||
private void testAllocateBuffer(int blockSize, int bufSize, boolean preferDirect) {
|
||||
// allocate the input buffer to an arbitrary size less than the blockSize
|
||||
ByteBuf in = ByteBufAllocator.DEFAULT.buffer(bufSize, bufSize);
|
||||
in.writerIndex(in.capacity());
|
||||
@ -119,13 +121,17 @@ public class Lz4FrameEncoderTest extends AbstractEncoderTest {
|
||||
ByteBuf out = null;
|
||||
try {
|
||||
Lz4FrameEncoder encoder = newEncoder(blockSize, Lz4FrameEncoder.DEFAULT_MAX_ENCODE_SIZE);
|
||||
out = encoder.allocateBuffer(ctx, in, isDirect);
|
||||
out = encoder.allocateBuffer(ctx, in, preferDirect);
|
||||
Assert.assertNotNull(out);
|
||||
if (NONALLOCATABLE_SIZE == bufSize) {
|
||||
Assert.assertFalse(out.isWritable());
|
||||
} else {
|
||||
Assert.assertTrue(out.writableBytes() > 0);
|
||||
Assert.assertEquals(isDirect, out.isDirect());
|
||||
if (!preferDirect) {
|
||||
// Only check if preferDirect is not true as if a direct buffer is returned or not depends on
|
||||
// if sun.misc.Unsafe is present.
|
||||
assertFalse(out.isDirect());
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
in.release();
|
||||
|
Loading…
Reference in New Issue
Block a user