An open API service indexing awesome lists of open source software.

https://github.com/evosec/tomcat-classloader-leak-test

An integration test for your webapp that checks if it cleans up after itself.
https://github.com/evosec/tomcat-classloader-leak-test

classloader integration java leak memory metaspace permgen test tomcat webapp

Last synced: 23 days ago
JSON representation

An integration test for your webapp that checks if it cleans up after itself.

Awesome Lists containing this project

README

          

# tomcat-classloader-leak-test

[![Build Status](https://travis-ci.org/evosec/tomcat-classloader-leak-test.svg?branch=develop)](https://travis-ci.org/evosec/tomcat-classloader-leak-test)

An integration test for your webapp that checks if it cleans up after itself.

## Usage

`pom.xml`:
~~~

de.evosec
tomcat-classloader-leak-test
0.0.1
test

org.codehaus.mojo
build-helper-maven-plugin


add-test-source
generate-test-sources

add-test-source



src/it/java



org.apache.maven.plugins
maven-failsafe-plugin



integration-test
verify



org.springframework.boot:spring-boot-devtools


${project.basedir}/src/it/java

${project.build.finalName}



~~~

`src/it/java/com/example/ClassLoaderLeakTestIT`:
~~~
package com.example;

import java.nio.file.Paths;
import org.junit.Test;
import de.evosec.leaktest.WebAppTest;

public class ClassLoaderLeakTestIT {

@Test
public void test() throws Exception {
String warName = System.getProperty("warName", "demo-0.0.1-SNAPSHOT");
Path warPath = Paths.get("./target/" + warName + ".war");
new WebAppTest().warPath(warPath).contextParameter("spring.profiles.active", "integration").run();
}

}
~~~

## How it works

1. The test setups an embedded Tomcat.
2. Then the war file from the build is added to the Tomcat
3. Wait for the application to start (By default it waits until the root returns HTTP 200 OK)
4. Get a WeakReference to the started Context
5. Stop the application
6. Start creating classes to put the Metaspace/PermGen under pressure - thus triggering GC eventually.
7. Stop when WeakReference is null - thus the Context has been cleaned up properly. If not, then there is a ClassLoader leak.