Coder Social home page Coder Social logo

evosec / tomcat-classloader-leak-test Goto Github PK

View Code? Open in Web Editor NEW
3.0 4.0 3.0 16.25 MB

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

License: Other

Ruby 6.60% Java 93.40%
java tomcat webapp classloader memory leak integration test permgen metaspace

tomcat-classloader-leak-test's Introduction

tomcat-classloader-leak-test

Build Status

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

Usage

pom.xml:

<dependency>
    <groupId>de.evosec</groupId>
    <artifactId>tomcat-classloader-leak-test</artifactId>
    <version>0.0.1</version>
    <scope>test</scope>
</dependency>

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>add-test-source</id>
            <phase>generate-test-sources</phase>
            <goals>
                <goal>add-test-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>src/it/java</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <classpathDependencyExcludes>
                    <classpathDependencyExclude>org.springframework.boot:spring-boot-devtools</classpathDependencyExclude>
                </classpathDependencyExcludes>
                <argLine><![CDATA[-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${project.build.directory} -XX:MaxMetaspaceSize=160m]]></argLine>
                <testSourceDirectory>${project.basedir}/src/it/java</testSourceDirectory>
                <systemPropertyVariables>
                    <warName>${project.build.finalName}</warName>
                </systemPropertyVariables>
            </configuration>
        </execution>
    </executions>
</plugin>

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.

tomcat-classloader-leak-test's People

Contributors

arlol avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.