forked from helidon-io/helidon
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds ability to close idle connections after a certain time using con…
…neciton-idle-timeout. Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
- Loading branch information
Showing
6 changed files
with
188 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
webserver/webserver/src/test/java/io/helidon/webserver/ConnectionIdleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed 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 io.helidon.webserver; | ||
|
||
import java.net.SocketException; | ||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.logging.Logger; | ||
|
||
import io.helidon.common.http.Http; | ||
import io.helidon.webserver.utils.SocketHttpClient; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.CoreMatchers.containsString; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
/** | ||
* Tests support for idle connection timeouts. | ||
*/ | ||
public class ConnectionIdleTest { | ||
private static final Logger LOGGER = Logger.getLogger(ConnectionIdleTest.class.getName()); | ||
private static final Duration TIMEOUT = Duration.ofSeconds(10); | ||
|
||
private static final int IDLE_TIMEOUT = 1000; | ||
|
||
private static WebServer webServer; | ||
|
||
@BeforeAll | ||
public static void startServer() throws Exception { | ||
startServer(0); | ||
} | ||
|
||
@AfterAll | ||
public static void close() throws Exception { | ||
if (webServer != null) { | ||
webServer.shutdown().await(TIMEOUT); | ||
} | ||
} | ||
|
||
/** | ||
* Start the Web Server | ||
* | ||
* @param port the port on which to start the server | ||
*/ | ||
private static void startServer(int port) { | ||
webServer = WebServer.builder() | ||
.host("localhost") | ||
.port(port) | ||
.connectionIdleTimeout(IDLE_TIMEOUT / 1000) // in seconds | ||
.routing(Routing.builder().get("/hello", (req, res) -> res.send("Hello World!"))) | ||
.build() | ||
.start() | ||
.await(TIMEOUT); | ||
|
||
LOGGER.info("Started server at: https://localhost:" + webServer.port()); | ||
} | ||
|
||
@Test | ||
public void testIdleConnectionClosed() throws Exception { | ||
try (SocketHttpClient client = new SocketHttpClient(webServer)) { | ||
// initial request with keep-alive to open connection | ||
client.request(Http.Method.GET, | ||
"/hello", | ||
null, | ||
List.of("Connection: keep-alive")); | ||
String res = client.receive(); | ||
assertThat(res, containsString("Hello World!")); | ||
|
||
// second request while connection is active | ||
client.request(Http.Method.GET, | ||
"/hello", | ||
null); | ||
res = client.receive(); | ||
assertThat(res, containsString("Hello World!")); | ||
|
||
// wait for connection to time out due to inactivity | ||
Thread.sleep(2 * IDLE_TIMEOUT); | ||
|
||
// try again and either get nothing or an exception | ||
try { | ||
client.request(Http.Method.GET, | ||
"/hello", | ||
null); | ||
res = client.receive(); | ||
assertThat(res, is("")); | ||
} catch (SocketException e) { | ||
// falls through as possible outcome | ||
} | ||
} | ||
} | ||
} |