Search in sources :

Example 1 with JreMemoryLeakPreventionListener

use of org.apache.catalina.core.JreMemoryLeakPreventionListener in project tomcat by apache.

the class TestVirtualContext method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    Tomcat tomcat = getTomcatInstance();
    // BZ 49218: The test fails if JreMemoryLeakPreventionListener is not
    // present. The listener affects the JVM, and thus not only the current,
    // but also the subsequent tests that are run in the same JVM. So it is
    // fair to add it in every test.
    tomcat.getServer().addLifecycleListener(new JreMemoryLeakPreventionListener());
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) JreMemoryLeakPreventionListener(org.apache.catalina.core.JreMemoryLeakPreventionListener)

Example 2 with JreMemoryLeakPreventionListener

use of org.apache.catalina.core.JreMemoryLeakPreventionListener in project tomcat by apache.

the class TestWebappClassLoaderThreadLocalMemoryLeak method testThreadLocalLeak1.

@Test
public void testThreadLocalLeak1() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // Need to make sure we see a leak for the right reasons
    tomcat.getServer().addLifecycleListener(new JreMemoryLeakPreventionListener());
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "leakServlet1", "org.apache.tomcat.unittest.TesterLeakingServlet1");
    ctx.addServletMappingDecoded("/leak1", "leakServlet1");
    tomcat.start();
    Executor executor = tomcat.getConnector().getProtocolHandler().getExecutor();
    ((ThreadPoolExecutor) executor).setThreadRenewalDelay(-1);
    // Configure logging filter to check leak message appears
    LogValidationFilter f = new LogValidationFilter("The web application [ROOT] created a ThreadLocal with key of");
    LogManager.getLogManager().getLogger("org.apache.catalina.loader.WebappClassLoaderBase").setFilter(f);
    // Need to force loading of all web application classes via the web
    // application class loader
    loadClass("TesterCounter", (WebappClassLoader) ctx.getLoader().getClassLoader());
    loadClass("TesterLeakingServlet1", (WebappClassLoader) ctx.getLoader().getClassLoader());
    // This will trigger the ThreadLocal creation
    int rc = getUrl("http://localhost:" + getPort() + "/leak1", new ByteChunk(), null);
    // Make sure request is OK
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    // Destroy the context
    ctx.stop();
    tomcat.getHost().removeChild(ctx);
    ctx = null;
    // Make sure we have a memory leak
    String[] leaks = ((StandardHost) tomcat.getHost()).findReloadedContextMemoryLeaks();
    Assert.assertNotNull(leaks);
    Assert.assertTrue(leaks.length > 0);
    // Make sure the message was logged
    Assert.assertEquals(1, f.getMessageCount());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) Executor(java.util.concurrent.Executor) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) StandardHost(org.apache.catalina.core.StandardHost) JreMemoryLeakPreventionListener(org.apache.catalina.core.JreMemoryLeakPreventionListener) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 3 with JreMemoryLeakPreventionListener

use of org.apache.catalina.core.JreMemoryLeakPreventionListener in project tomcat by apache.

the class TestWebappClassLoaderThreadLocalMemoryLeak method testThreadLocalLeak2.

@Test
public void testThreadLocalLeak2() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // Need to make sure we see a leak for the right reasons
    tomcat.getServer().addLifecycleListener(new JreMemoryLeakPreventionListener());
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "leakServlet2", "org.apache.tomcat.unittest.TesterLeakingServlet2");
    ctx.addServletMappingDecoded("/leak2", "leakServlet2");
    tomcat.start();
    Executor executor = tomcat.getConnector().getProtocolHandler().getExecutor();
    ((ThreadPoolExecutor) executor).setThreadRenewalDelay(-1);
    // Configure logging filter to check leak message appears
    LogValidationFilter f = new LogValidationFilter("The web application [ROOT] created a ThreadLocal with key of");
    LogManager.getLogManager().getLogger("org.apache.catalina.loader.WebappClassLoaderBase").setFilter(f);
    // Need to force loading of all web application classes via the web
    // application class loader
    loadClass("TesterCounter", (WebappClassLoader) ctx.getLoader().getClassLoader());
    loadClass("TesterThreadScopedHolder", (WebappClassLoader) ctx.getLoader().getClassLoader());
    loadClass("TesterLeakingServlet2", (WebappClassLoader) ctx.getLoader().getClassLoader());
    // This will trigger the ThreadLocal creation
    int rc = getUrl("http://localhost:" + getPort() + "/leak2", new ByteChunk(), null);
    // Make sure request is OK
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    // Destroy the context
    ctx.stop();
    tomcat.getHost().removeChild(ctx);
    ctx = null;
    // Make sure we have a memory leak
    String[] leaks = ((StandardHost) tomcat.getHost()).findReloadedContextMemoryLeaks();
    Assert.assertNotNull(leaks);
    Assert.assertTrue(leaks.length > 0);
    // Make sure the message was logged
    Assert.assertEquals(1, f.getMessageCount());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) Executor(java.util.concurrent.Executor) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) StandardHost(org.apache.catalina.core.StandardHost) JreMemoryLeakPreventionListener(org.apache.catalina.core.JreMemoryLeakPreventionListener) ThreadPoolExecutor(org.apache.tomcat.util.threads.ThreadPoolExecutor) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 4 with JreMemoryLeakPreventionListener

use of org.apache.catalina.core.JreMemoryLeakPreventionListener in project tomcat by apache.

the class TestWarDirContext method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    Tomcat tomcat = getTomcatInstance();
    // The test fails if JreMemoryLeakPreventionListener is not
    // present. The listener affects the JVM, and thus not only the current,
    // but also the subsequent tests that are run in the same JVM. So it is
    // fair to add it in every test.
    tomcat.getServer().addLifecycleListener(new JreMemoryLeakPreventionListener());
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) JreMemoryLeakPreventionListener(org.apache.catalina.core.JreMemoryLeakPreventionListener)

Aggregations

JreMemoryLeakPreventionListener (org.apache.catalina.core.JreMemoryLeakPreventionListener)4 Tomcat (org.apache.catalina.startup.Tomcat)4 Executor (java.util.concurrent.Executor)2 Context (org.apache.catalina.Context)2 StandardHost (org.apache.catalina.core.StandardHost)2 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)2 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)2 ThreadPoolExecutor (org.apache.tomcat.util.threads.ThreadPoolExecutor)2 Test (org.junit.Test)2