ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/org/apache/catalina/connector/TestMaxConnections.java

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

      
    
Rootfs path

      
    
Size
5783 (5.6 KB)
MD5
8a44ad5619aea3ef2697e687059ecc03
SHA1
76be4c6bd1fbb0768a29814299318a94f9cba467
SHA256
ad307a21170e71dfaf5d1896cf02549ac030418c0bf87add304a3bd87ea3f435
SHA512

      
    
SHA1_git
105eff0cea7bf01c7670f9febb1b85fcb473d5c0
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
TestMaxConnections.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.connector; import java.io.IOException; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.junit.Assert; import org.junit.Test; import org.apache.catalina.core.StandardContext; import org.apache.catalina.startup.SimpleHttpClient; import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.TomcatBaseTest; public class TestMaxConnections extends TomcatBaseTest { private static final int MAX_CONNECTIONS = 3; public static final int soTimeout = 5000; public static final int connectTimeout = 1000; @Test public void testConnector() throws Exception { init(); ConnectThread[] t = new ConnectThread[10]; for (int i = 0; i < t.length; i++) { t[i] = new ConnectThread(); t[i].setName("ConnectThread[" + i + "]"); } for (ConnectThread thread : t) { thread.start(); Thread.sleep(50); } for (ConnectThread connectThread : t) { connectThread.join(); } Assert.assertEquals(MAX_CONNECTIONS, SimpleServlet.getMaxConnections()); } private class ConnectThread extends Thread { @Override public void run() { try { TestClient client = new TestClient(); client.doHttp10Request(); } catch (Exception x) { // NO-OP. Some connections are expected to fail. } } } private synchronized void init() throws Exception { Tomcat tomcat = getTomcatInstance(); StandardContext root = (StandardContext) tomcat.addContext("", SimpleHttpClient.TEMP_DIR); root.setUnloadDelay(soTimeout); Tomcat.addServlet(root, "Simple", new SimpleServlet()); root.addServletMappingDecoded("/test", "Simple"); Assert.assertTrue(tomcat.getConnector().setProperty("maxKeepAliveRequests", "1")); Assert.assertTrue(tomcat.getConnector().setProperty("maxThreads", "10")); Assert.assertTrue(tomcat.getConnector().setProperty("connectionTimeout", "20000")); Assert.assertTrue(tomcat.getConnector().setProperty("keepAliveTimeout", "50000")); Assert.assertTrue(tomcat.getConnector().setProperty("maxConnections", Integer.toString(MAX_CONNECTIONS))); Assert.assertTrue(tomcat.getConnector().setProperty("acceptCount", "1")); tomcat.start(); } private class TestClient extends SimpleHttpClient { private void doHttp10Request() throws Exception { setPort(getPort()); long start = System.currentTimeMillis(); // Open connection connect(connectTimeout, soTimeout); // Send request in two parts String[] request = new String[1]; // @formatter:off request[0] = "GET /test HTTP/1.0" + CRLF + CRLF; // @formatter:on setRequest(request); boolean passed = false; processRequest(false); // blocks until response has been read long stop = System.currentTimeMillis(); log.info(Thread.currentThread().getName() + " Request complete:" + (stop - start) + " ms."); passed = (this.readLine() == null); // Close the connection disconnect(); reset(); Assert.assertTrue(passed); } @Override public boolean isResponseBodyOK() { return true; } } private static class SimpleServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static int currentConnections = 0; private static int maxConnections = 0; @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { increment(); System.out.println("Processing thread: " + Thread.currentThread().getName()); try { Thread.sleep(soTimeout * 4 / 5); } catch (InterruptedException x) { } resp.setContentLength(0); resp.flushBuffer(); decrement(); } private static synchronized void increment() { currentConnections++; if (currentConnections > maxConnections) { maxConnections = currentConnections; } } private static synchronized void decrement() { currentConnections--; } public static synchronized int getMaxConnections() { return maxConnections; } } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
22.75
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