-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds memory leak checking in unit test
- Loading branch information
Guan Hao
committed
May 2, 2020
1 parent
f6c3591
commit 1926856
Showing
11 changed files
with
254 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
33 changes: 0 additions & 33 deletions
33
brpc-java-communication/src/test/java/com/baidu/brpc/buffer/ByteBufTest.java
This file was deleted.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
...a-communication/src/test/java/com/baidu/brpc/protocol/http/BrpcHttpObjectDecoderTest.java
This file contains 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,68 @@ | ||
package com.baidu.brpc.protocol.http; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.baidu.brpc.test.DetectLeak; | ||
import com.baidu.brpc.test.DetectMemoryLeakRule; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.PooledByteBufAllocator; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.util.ReferenceCountUtil; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.nio.charset.Charset; | ||
|
||
@Slf4j | ||
public class BrpcHttpObjectDecoderTest { | ||
|
||
@DetectLeak | ||
protected PooledByteBufAllocator alloc; | ||
@Rule | ||
public DetectMemoryLeakRule detectMemoryLeakRule = new DetectMemoryLeakRule(this); | ||
|
||
@Test | ||
public void testDecodePartial() throws Exception { | ||
BrpcHttpObjectDecoder decoder = BrpcHttpObjectDecoder.getDecoder(true); | ||
ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class); | ||
when(ctx.alloc()).thenReturn(alloc); | ||
ByteBuf buf = alloc.buffer(1024); | ||
String[] testRequest = new String[]{ | ||
"GET / HTTP/1.1", | ||
"Host: localhost", | ||
"Content-Length: 4096", | ||
"", | ||
"abc" | ||
}; // partial request | ||
buf.writeBytes(StringUtils.join(testRequest, "\n\r").getBytes(Charset.forName("UTF-8"))); | ||
Object message = decoder.decode(ctx, buf); | ||
assertThat(message).isNull(); | ||
ReferenceCountUtil.release(buf); | ||
} | ||
|
||
@Test | ||
public void testDecode() throws Exception { | ||
BrpcHttpObjectDecoder decoder = BrpcHttpObjectDecoder.getDecoder(true); | ||
ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class); | ||
when(ctx.alloc()).thenReturn(alloc); | ||
ByteBuf buf = alloc.buffer(1024); | ||
String[] testRequest = new String[]{ | ||
"GET / HTTP/1.1", | ||
"Host: localhost", | ||
"Content-Length: 10", | ||
"", | ||
"1234567890" | ||
}; // partial request | ||
buf.writeBytes(StringUtils.join(testRequest, "\n\r").getBytes(Charset.forName("UTF-8"))); | ||
Object message = decoder.decode(ctx, buf); | ||
assertThat(message).isNotNull(); | ||
ReferenceCountUtil.release(buf); | ||
ReferenceCountUtil.release(message); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
brpc-java-communication/src/test/java/com/baidu/brpc/test/DetectLeak.java
This file contains 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,11 @@ | ||
package com.baidu.brpc.test; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface DetectLeak { | ||
} |
101 changes: 101 additions & 0 deletions
101
brpc-java-communication/src/test/java/com/baidu/brpc/test/DetectMemoryLeakRule.java
This file contains 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,101 @@ | ||
package com.baidu.brpc.test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.netty.buffer.PoolArenaMetric; | ||
import io.netty.buffer.PooledByteBufAllocator; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.rules.TestRule; | ||
import org.junit.runner.Description; | ||
import org.junit.runners.model.Statement; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
public class DetectMemoryLeakRule implements TestRule { | ||
|
||
private final Object instance; | ||
|
||
public DetectMemoryLeakRule(Object instance) { | ||
this.instance = instance; | ||
} | ||
|
||
@Override | ||
public Statement apply(final Statement base, Description description) { | ||
Class<?> testClass = description.getTestClass(); | ||
final List<Field> allocators = new ArrayList<Field>(); | ||
for (Field field : testClass.getDeclaredFields()) { | ||
DetectLeak detectLeak = field.getAnnotation(DetectLeak.class); | ||
if (detectLeak == null) { | ||
continue; | ||
} | ||
if (!PooledByteBufAllocator.class.equals(field.getType())) { | ||
continue; | ||
} | ||
field.setAccessible(true); | ||
allocators.add(field); | ||
} | ||
return new Statement() { | ||
@Override | ||
public void evaluate() throws Throwable { | ||
setupPools(allocators); | ||
base.evaluate(); | ||
checkLeaks(allocators); | ||
} | ||
}; | ||
} | ||
|
||
private void setupPools(List<Field> allocators) { | ||
Iterator<Field> it = allocators.iterator(); | ||
while (it.hasNext()) { | ||
Field field = it.next(); | ||
PooledByteBufAllocator alloc = new PooledByteBufAllocator(true, 0, 1, 8192, 11, 0, 0, 0, false); | ||
try { | ||
field.set(instance, alloc); | ||
} catch (Exception ex) { | ||
log.warn("Failed to process field {} for memory leak detection", field.getName()); | ||
it.remove(); | ||
} | ||
} | ||
} | ||
|
||
private void checkLeaks(List<Field> allocators) { | ||
for (Field field : allocators) { | ||
PooledByteBufAllocator alloc; | ||
try { | ||
alloc = (PooledByteBufAllocator) field.get(instance); | ||
} catch (Exception ex) { | ||
log.warn("Failed to process field {} for memory leak detection", field.getName(), ex); | ||
continue; | ||
} | ||
assertThat(alloc).as("PooledByteBufAllocator").isNotNull(); | ||
assertThat(getActiveHeapBuffers(alloc)).as("active heap memory").isZero(); | ||
assertThat(getActiveDirectBuffers(alloc)).as("active direct memory").isZero(); | ||
} | ||
} | ||
|
||
private static int getActiveDirectBuffers(PooledByteBufAllocator alloc) { | ||
int directActive = 0, directAlloc = 0, directDealloc = 0; | ||
for (PoolArenaMetric arena : alloc.metric().directArenas()) { | ||
directActive += arena.numActiveAllocations(); | ||
directAlloc += arena.numAllocations(); | ||
directDealloc += arena.numDeallocations(); | ||
} | ||
log.info("direct memory usage, active: {}, alloc: {}, dealloc: {}", directActive, directAlloc, directDealloc); | ||
return directActive; | ||
} | ||
|
||
private static int getActiveHeapBuffers(PooledByteBufAllocator alloc) { | ||
int heapActive = 0, heapAlloc = 0, heapDealloc = 0; | ||
for (PoolArenaMetric arena : alloc.metric().heapArenas()) { | ||
heapActive += arena.numActiveAllocations(); | ||
heapAlloc += arena.numAllocations(); | ||
heapDealloc += arena.numDeallocations(); | ||
} | ||
log.info("heap memory usage, active: {}, alloc: {}, dealloc: {}", heapActive, heapAlloc, heapDealloc); | ||
return heapActive; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
brpc-java-communication/src/test/java/com/baidu/brpc/test/IntegrationTest.java
This file contains 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,7 @@ | ||
package com.baidu.brpc.test; | ||
|
||
/** | ||
* Marker interface for Integration Test | ||
*/ | ||
public interface IntegrationTest { | ||
} |
This file contains 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,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration status="WARN"> | ||
<Appenders> | ||
<Console name="Console" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> | ||
</Console> | ||
</Appenders> | ||
<Loggers> | ||
<Root level="INFO"> | ||
<AppenderRef ref="Console"/> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
This file contains 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
This file contains 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