Add unit test for leak aware CompositeByteBuf that proves that there is no NPE (#9875)

Motivation:

https://github.com/netty/netty/issues/9873 reported a NPE in previous version of netty. We should add a unit test to verify there is no more NPE

Modifications:

Add a unit test

Result:

Prove that https://github.com/netty/netty/issues/9873 is fixed
This commit is contained in:
Norman Maurer 2019-12-12 16:18:27 +01:00 committed by GitHub
parent 0992718f87
commit 9a0ccf24f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,7 +15,9 @@
*/
package io.netty.buffer;
import io.netty.util.ByteProcessor;
import io.netty.util.ResourceLeakTracker;
import org.hamcrest.CoreMatchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -23,7 +25,9 @@ import org.junit.Test;
import java.util.ArrayDeque;
import java.util.Queue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class SimpleLeakAwareCompositeByteBufTest extends WrappedCompositeByteBufTest {
@ -131,6 +135,26 @@ public class SimpleLeakAwareCompositeByteBufTest extends WrappedCompositeByteBuf
assertWrapped(newBuffer(8).asReadOnly());
}
@Test
public void forEachByteUnderLeakDetectionShouldNotThrowException() {
CompositeByteBuf buf = (CompositeByteBuf) newBuffer(8);
assertThat(buf, CoreMatchers.instanceOf(SimpleLeakAwareCompositeByteBuf.class));
CompositeByteBuf comp = (CompositeByteBuf) newBuffer(8);
assertThat(comp, CoreMatchers.instanceOf(SimpleLeakAwareCompositeByteBuf.class));
ByteBuf inner = comp.alloc().directBuffer(1).writeByte(0);
comp.addComponent(true, inner);
buf.addComponent(true, comp);
assertEquals(-1, buf.forEachByte(new ByteProcessor() {
@Override
public boolean process(byte value) {
return true;
}
}));
assertTrue(buf.release());
}
protected final void assertWrapped(ByteBuf buf) {
try {
assertSame(clazz, buf.getClass());