This is an automated email from the ASF dual-hosted git repository.

remm pushed a commit to branch 11.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/11.0.x by this push:
     new b5830addd5 Add shutdown port testing
b5830addd5 is described below

commit b5830addd5ca82790c1a3ba5f1e19c0de934df80
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 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: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to