This is an automated email from the ASF dual-hosted git repository.
remm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push:
new 15e3010ef0 Add shutdown port testing
15e3010ef0 is described below
commit 15e3010ef06535177e4e173c239e54b6bef840d9
Author: remm <[email protected]>
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 796bc63f0a..54c90a84d5 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;
@@ -94,12 +101,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: [email protected]
For additional commands, e-mail: [email protected]