From 5b387975b8bd3810b1339646e85eeb12d1d468bb Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 12 Dec 2019 16:18:27 +0100 Subject: [PATCH] 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 --- .../SimpleLeakAwareCompositeByteBufTest.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/buffer/src/test/java/io/netty/buffer/SimpleLeakAwareCompositeByteBufTest.java b/buffer/src/test/java/io/netty/buffer/SimpleLeakAwareCompositeByteBufTest.java index 580bcdbb72..e173ddcb5c 100644 --- a/buffer/src/test/java/io/netty/buffer/SimpleLeakAwareCompositeByteBufTest.java +++ b/buffer/src/test/java/io/netty/buffer/SimpleLeakAwareCompositeByteBufTest.java @@ -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());