|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 IBM Corporation and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + *******************************************************************************/ |
| 10 | +package io.openliberty.transport.http_fat; |
| 11 | + |
| 12 | +import static org.junit.Assert.assertNotNull; |
| 13 | +import static org.junit.Assert.assertTrue; |
| 14 | + |
| 15 | +import java.io.BufferedReader; |
| 16 | +import java.io.InputStreamReader; |
| 17 | +import java.io.OutputStream; |
| 18 | +import java.net.Socket; |
| 19 | +import java.util.logging.Logger; |
| 20 | + |
| 21 | +import org.junit.After; |
| 22 | +import org.junit.AfterClass; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.BeforeClass; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | + |
| 28 | +import com.ibm.websphere.simplicity.config.HttpEndpoint; |
| 29 | +import com.ibm.websphere.simplicity.config.ServerConfiguration; |
| 30 | + |
| 31 | +import componenttest.annotation.Server; |
| 32 | +import componenttest.custom.junit.runner.FATRunner; |
| 33 | +import componenttest.topology.impl.LibertyServer; |
| 34 | + |
| 35 | +/** |
| 36 | + * Test to ensure that the tcpOptions inactivityTimeout works. |
| 37 | + */ |
| 38 | +@RunWith(FATRunner.class) |
| 39 | +//@Mode(TestMode.FULL) |
| 40 | +public class InactivityTimeoutTests { |
| 41 | + |
| 42 | + static final Logger LOG = Logger.getLogger(InactivityTimeoutTests.class.getName()); |
| 43 | + |
| 44 | + @Server("InactivityTimeout") |
| 45 | + public static LibertyServer server; |
| 46 | + |
| 47 | + @BeforeClass |
| 48 | + public static void setup() throws Exception { |
| 49 | + // Start the server and use the class name so we can find logs easily. |
| 50 | + server.startServer(InactivityTimeoutTests.class.getSimpleName() + ".log"); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Save the server configuration before each test, this should be the default server |
| 55 | + * configuration. |
| 56 | + * |
| 57 | + * @throws Exception |
| 58 | + */ |
| 59 | + @Before |
| 60 | + public void beforeTest() throws Exception { |
| 61 | + server.saveServerConfiguration(); |
| 62 | + |
| 63 | + ServerConfiguration configuration = server.getServerConfiguration(); |
| 64 | + LOG.info("Server configuration that was saved: " + configuration); |
| 65 | + } |
| 66 | + |
| 67 | + @AfterClass |
| 68 | + public static void tearDown() throws Exception { |
| 69 | + // Stop the server |
| 70 | + if (server != null && server.isStarted()) { |
| 71 | + server.stopServer(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Restore the server configuration to the default state after each test. |
| 77 | + * |
| 78 | + * @throws Exception |
| 79 | + */ |
| 80 | + @After |
| 81 | + public void afterTest() throws Exception { |
| 82 | + // Restore the server to the default state. |
| 83 | + server.setMarkToEndOfLog(); |
| 84 | + server.setTraceMarkToEndOfDefaultTrace(); |
| 85 | + server.restoreServerConfiguration(); |
| 86 | + server.waitForConfigUpdateInLogUsingMark(null); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * The test will check the default value of inactivityTimeout by searching the trace file. |
| 91 | + * |
| 92 | + * The default value is 60 seconds will be logged as 60000 miliseconds. |
| 93 | + */ |
| 94 | + //@Test |
| 95 | + public void testInactivityTimeout_default() throws Exception { |
| 96 | + // Validate that inActivityTimeout default is 60s. |
| 97 | + assertNotNull("The default value of inactivityTimeout was not: 60000!", server.waitForStringInTraceUsingMark("inactivityTimeout: 60000")); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * The test will set inactivityTimeout to a value of 120s and validate in the trace file that |
| 102 | + * the correct value is being used. |
| 103 | + * |
| 104 | + * The below configuration will be used to set inactivityTimeout to 120s: |
| 105 | + * <tcpOptions inactivityTimeout="120s"/> |
| 106 | + * |
| 107 | + * @throws Exception |
| 108 | + */ |
| 109 | + //@Test |
| 110 | + public void testInactivityTimeout_nonDefault() throws Exception { |
| 111 | + ServerConfiguration configuration = server.getServerConfiguration(); |
| 112 | + LOG.info("Server configuration that the test started with: " + configuration); |
| 113 | + |
| 114 | + HttpEndpoint httpEndpoint = configuration.getHttpEndpoints().getById("defaultHttpEndpoint"); |
| 115 | + httpEndpoint.getTcpOptions().setInactivityTimeout("120s"); |
| 116 | + |
| 117 | + server.setMarkToEndOfLog(); |
| 118 | + server.setTraceMarkToEndOfDefaultTrace(); |
| 119 | + server.updateServerConfiguration(configuration); |
| 120 | + server.waitForConfigUpdateInLogUsingMark(null); |
| 121 | + |
| 122 | + // Validate that inactivityTimeout is set to 120000 (120s). |
| 123 | + assertNotNull("The configured value of inactivityTimeout was not 120000!", server.waitForStringInTraceUsingMark("inactivityTimeout: 120000")); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * The test will set inactivityTimeout to a value of -1s and validate that an error occurs. |
| 128 | + * |
| 129 | + * The below configuration will be used to set inactivityTimeout to 120s: |
| 130 | + * <tcpOptions inactivityTimeout="-1s"/> |
| 131 | + * |
| 132 | + * @throws Exception |
| 133 | + */ |
| 134 | + @Test |
| 135 | + public void testInactivityTimeout_negative() throws Exception { |
| 136 | + ServerConfiguration configuration = server.getServerConfiguration(); |
| 137 | + LOG.info("Server configuration that the test started with: " + configuration); |
| 138 | + |
| 139 | + HttpEndpoint httpEndpoint = configuration.getHttpEndpoints().getById("defaultHttpEndpoint"); |
| 140 | + httpEndpoint.getTcpOptions().setInactivityTimeout("-1"); |
| 141 | + |
| 142 | + server.setMarkToEndOfLog(); |
| 143 | + server.setTraceMarkToEndOfDefaultTrace(); |
| 144 | + server.updateServerConfiguration(configuration); |
| 145 | + server.waitForConfigUpdateInLogUsingMark(null); |
| 146 | + |
| 147 | + // Validate that inactivityTimeout is set to 120000 (120s). |
| 148 | + //assertNotNull("The configured value of inactivityTimeout was not 120000!", server.waitForStringInTraceUsingMark("inactivityTimeout: 120000")); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * The test will set the inactivitiyTimeout to a value of 10s. |
| 153 | + * |
| 154 | + * A socket will be opened. |
| 155 | + * |
| 156 | + * The test will sleep for 15s which is greater then the inactivityTimeout. |
| 157 | + * |
| 158 | + * The test will then send a request and validate that the response is null since the |
| 159 | + * request should fail because the remote side of the Socket has closed due to the |
| 160 | + * inactivityTimeout. |
| 161 | + * |
| 162 | + * @throws Exception |
| 163 | + */ |
| 164 | + //@Test |
| 165 | + public void testInactivityTimeout() throws Exception { |
| 166 | + String expectedResponse = "HTTP/1.1 408 Request Timeout"; |
| 167 | + |
| 168 | + String address = server.getHostname() + ":" + server.getHttpDefaultPort(); |
| 169 | + |
| 170 | + String request = "GET /" + " HTTP/1.1\r\n" + |
| 171 | + "Host: " + address + "\r\n" + |
| 172 | + "\r\n"; |
| 173 | + |
| 174 | + // Set the inactivityTimeout to 10 seconds to make testing quicker than the default 60 second timeout. |
| 175 | + ServerConfiguration configuration = server.getServerConfiguration(); |
| 176 | + LOG.info("Server configuration that the test started with: " + configuration); |
| 177 | + |
| 178 | + HttpEndpoint httpEndpoint = configuration.getHttpEndpoints().getById("defaultHttpEndpoint"); |
| 179 | + httpEndpoint.getTcpOptions().setInactivityTimeout("10s"); |
| 180 | + |
| 181 | + server.setMarkToEndOfLog(); |
| 182 | + server.setTraceMarkToEndOfDefaultTrace(); |
| 183 | + server.updateServerConfiguration(configuration); |
| 184 | + server.waitForConfigUpdateInLogUsingMark(null); |
| 185 | + |
| 186 | + // Validate that inactivityTimeout is set to 10000 (10s). |
| 187 | + assertNotNull("The configured value of inactivityTimeout was not 10000!", server.waitForStringInTraceUsingMark("inactivityTimeout: 10000")); |
| 188 | + |
| 189 | + // Ensure the TCP Channel has started. |
| 190 | + // CWWKO0219I: TCP Channel defaultHttpEndpoint has been started and is now listening for requests on host * (IPv4) port 8010. |
| 191 | + assertNotNull("The TCP Channel was not started!", server.waitForStringInLogUsingMark("CWWKO0219I")); |
| 192 | + |
| 193 | + LOG.info("Creating a Socket connection."); |
| 194 | + try (Socket socket = new Socket(server.getHostname(), server.getHttpDefaultPort())) { |
| 195 | + |
| 196 | + // Sleep for more than the inactivityTimeout. |
| 197 | + //Thread.sleep(15000); |
| 198 | + // Even without this we get a inactivitiyTimeout |
| 199 | + |
| 200 | + String line; |
| 201 | + BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
| 202 | + OutputStream os = socket.getOutputStream(); |
| 203 | + |
| 204 | + LOG.info("Read from the Socket."); |
| 205 | + boolean expectedResponseFound = false; |
| 206 | + while ((line = reader.readLine()) != null) { |
| 207 | + LOG.info(line); |
| 208 | + if (line.equals(expectedResponse)) { |
| 209 | + expectedResponseFound = true; |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + assertTrue("The expected response: " + expectedResponse + " was not found!", expectedResponseFound); |
| 214 | + |
| 215 | + // Sending the request and reading the response should not work since the remote side of the Socket should have closed due to an inactivitiyTimeout! |
| 216 | + LOG.info("Sending a request."); |
| 217 | + os.write(request.getBytes()); |
| 218 | + |
| 219 | + LOG.info("Read the response."); |
| 220 | + String response = reader.readLine(); |
| 221 | + assertTrue("There should be no response data since the Socket was closed on the remote side due to an inactivityTimeout: " + response, response == null); |
| 222 | + |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + // TODO: Review the tracing of the above test and validate what is happening. |
| 227 | + // Does the test above pass without any wait on the initial socket creation? |
| 228 | + |
| 229 | +} |
0 commit comments