This is an automated email from the ASF dual-hosted git repository. remm pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/9.0.x by this push: new 9201726b76 Add shutdown port testing 9201726b76 is described below commit 9201726b7687f58c77d3fb48d8394dd50b9680cd Author: remm <r...@apache.org> AuthorDate: Tue Oct 29 15:38:50 2024 +0100 Add shutdown port testing --- .../catalina/startup/TestTomcatStandalone.java | 59 +++++++++++++++++++++- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/test/org/apache/catalina/startup/TestTomcatStandalone.java b/test/org/apache/catalina/startup/TestTomcatStandalone.java index ebb763ad6f..deb8c15913 100644 --- a/test/org/apache/catalina/startup/TestTomcatStandalone.java +++ b/test/org/apache/catalina/startup/TestTomcatStandalone.java @@ -18,14 +18,21 @@ package org.apache.catalina.startup; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.Socket; import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; +import java.util.Enumeration; import org.junit.Assert; import org.junit.Test; import org.apache.catalina.Context; +import org.apache.catalina.LifecycleException; +import org.apache.catalina.LifecycleState; import org.apache.catalina.startup.TestTomcat.HelloWorld; import org.apache.tomcat.util.buf.ByteChunk; @@ -96,12 +103,60 @@ public class TestTomcatStandalone extends LoggingBaseTest { ctx.addServletMappingDecoded("/", "myServlet"); tomcat.start(); + // Emulate Tomcat main thread + new Thread() { + @Override + public void run() { + tomcat.getServer().await(); + try { + tomcat.stop(); + } catch (LifecycleException e) { + } + } + }.start(); + InetAddress localAddress = null; + Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); + while (networkInterfaces.hasMoreElements()) { + NetworkInterface ni = networkInterfaces.nextElement(); + if (!ni.isLoopback() && ni.isUp()) { + Enumeration<InetAddress> addresses = ni.getInetAddresses(); + while (addresses.hasMoreElements()) { + InetAddress address = addresses.nextElement(); + if (address instanceof Inet4Address) { + localAddress = address; + } + } + } + } ByteChunk res = TomcatBaseTest.getUrl("http://localhost:" + tomcat.getConnector().getLocalPort() + "/"); Assert.assertEquals("Hello world", res.toString()); - tomcat.stop(); - tomcat.destroy(); + // Use the shutdown command + if (localAddress != null) { + // Don't listen to non loopback + Exception ex = null; + try (Socket s = new Socket(localAddress, 8005)) { + s.getOutputStream().write("GOAWAY".getBytes(StandardCharsets.ISO_8859_1)); + } catch (Exception e) { + ex = e; + } + Assert.assertNotNull(ex); + } + + try (Socket s = new Socket(InetAddress.getLoopbackAddress(), 8005)) { + // Bad command + s.getOutputStream().write("GOAWAY".getBytes(StandardCharsets.ISO_8859_1)); + } + Thread.sleep(100); + Assert.assertEquals(LifecycleState.STARTED, tomcat.getService().getState()); + + try (Socket s = new Socket(InetAddress.getLoopbackAddress(), 8005)) { + s.getOutputStream().write("SHUTDOWN".getBytes(StandardCharsets.ISO_8859_1)); + } + Thread.sleep(100); + Assert.assertNotEquals(LifecycleState.STARTED, tomcat.getService().getState()); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org