ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/org/apache/catalina/valves/TestStuckThreadDetectionValve.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/org/apache/catalina/valves/TestStuckThreadDetectionValve.java
Status
scanned
Type
file
Name
TestStuckThreadDetectionValve.java
Extension
.java
Programming language
Java
Mime type
text/plain
File type
ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
5770 (5.6 KB)
MD5
207c27535dacb767f009bc2efcc0b911
SHA1
6883816858c82579b12541327e16a38367708d7e
SHA256
f18c7857573b822c55118d68a6088430055b3ecca3ff46cc536811372b6da52c
SHA512

      
    
SHA1_git
2c7abbcd29a27f634480b47a2e26c70e5d5ae530
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
TestStuckThreadDetectionValve.java | 5.6 KB |

/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.catalina.valves; import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.apache.catalina.Wrapper; import org.apache.catalina.core.StandardContext; import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.TomcatBaseTest; import org.apache.tomcat.util.buf.ByteChunk; public class TestStuckThreadDetectionValve extends TomcatBaseTest { private StandardContext context; private Tomcat tomcat; @Override @Before public void setUp() throws Exception { super.setUp(); tomcat = getTomcatInstance(); File docBase = new File(System.getProperty("java.io.tmpdir")); context = (StandardContext) tomcat.addContext("", docBase.getAbsolutePath()); } @Test public void testDetection() throws Exception { // second, we test the actual effect of the flag on the startup StickingServlet stickingServlet = new StickingServlet(8000L); Wrapper servlet = Tomcat.addServlet(context, "myservlet", stickingServlet); servlet.addMapping("/myservlet"); StuckThreadDetectionValve valve = new StuckThreadDetectionValve(); valve.setThreshold(2); context.addValve(valve); context.setBackgroundProcessorDelay(1); tomcat.start(); Assert.assertEquals(0, valve.getStuckThreadIds().length); final ByteChunk result = new ByteChunk(); Thread asyncThread = new Thread() { @Override public void run() { try { getUrl("http://localhost:" + getPort() + "/myservlet", result, null); } catch (IOException ioe) { ioe.printStackTrace(); } } }; asyncThread.start(); try { Thread.sleep(500L); Assert.assertEquals(0, valve.getStuckThreadIds().length); Thread.sleep(5000L); Assert.assertEquals(1, valve.getStuckThreadIds().length); } finally { asyncThread.join(20000); // check that we did not reach the join timeout Assert.assertFalse(asyncThread.isAlive()); } Assert.assertFalse(stickingServlet.wasInterrupted); Assert.assertTrue(result.toString().startsWith("OK")); } @Test public void testInterruption() throws Exception { // second, we test the actual effect of the flag on the startup StickingServlet stickingServlet = new StickingServlet(TimeUnit.SECONDS.toMillis(20L)); Wrapper servlet = Tomcat.addServlet(context, "myservlet", stickingServlet); servlet.addMapping("/myservlet"); StuckThreadDetectionValve valve = new StuckThreadDetectionValve(); valve.setThreshold(2); valve.setInterruptThreadThreshold(5); context.addValve(valve); context.setBackgroundProcessorDelay(1); tomcat.start(); Assert.assertEquals(0, valve.getStuckThreadIds().length); final ByteChunk result = new ByteChunk(); Thread asyncThread = new Thread() { @Override public void run() { try { getUrl("http://localhost:" + getPort() + "/myservlet", result, null); } catch (IOException ioe) { ioe.printStackTrace(); } } }; asyncThread.start(); try { Thread.sleep(4000L); Assert.assertEquals(1, valve.getStuckThreadIds().length); } finally { asyncThread.join(20000); // check that we did not reach the join timeout Assert.assertFalse(asyncThread.isAlive()); } Assert.assertTrue(stickingServlet.wasInterrupted); Assert.assertEquals(0, valve.getStuckThreadIds().length); Assert.assertTrue(result.toString().startsWith("OK")); } private static class StickingServlet extends HttpServlet { private static final long serialVersionUID = 1L; private final long delay; boolean wasInterrupted = false; StickingServlet(long delay) { this.delay = delay; } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { try { Thread.sleep(delay); } catch (InterruptedException e) { wasInterrupted = true; } resp.setContentType("text/plain"); resp.getWriter().println("OK"); } } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
22.04
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9