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.
Prototype support for a UDP API. Simple extension of existing testing…
… framework to support UDP.
- Loading branch information
Showing
23 changed files
with
1,084 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2022, 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. | ||
--> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.helidon.webserver.tests</groupId> | ||
<artifactId>helidon-webserver-tests-project</artifactId> | ||
<version>4.2.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>helidon-webserver-tests-udp</artifactId> | ||
<name>Helidon WebServer Tests UDP</name> | ||
<description>WebServer UDP tests</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.helidon.webserver</groupId> | ||
<artifactId>helidon-webserver</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>jakarta.json</groupId> | ||
<artifactId>jakarta.json-api</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.http.media</groupId> | ||
<artifactId>helidon-http-media-jsonp</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.webserver.testing.junit5</groupId> | ||
<artifactId>helidon-webserver-testing-junit5</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-all</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
50 changes: 50 additions & 0 deletions
50
webserver/tests/udp/src/test/java/io/helidon/webserver/tests/udp/UdpBaseTest.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,50 @@ | ||
/* | ||
* 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.tests.udp; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.DatagramChannel; | ||
|
||
import static java.lang.System.Logger.Level.INFO; | ||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
class UdpBaseTest { | ||
private static final System.Logger LOGGER = System.getLogger(UdpBaseTest.class.getName()); | ||
|
||
void echoMessage(String msg, InetSocketAddress address) throws IOException { | ||
try (DatagramChannel channel = DatagramChannel.open()) { | ||
echoMessageOnChannel(msg, channel, address); | ||
} | ||
} | ||
|
||
void echoMessageOnChannel(String msg, DatagramChannel channel, InetSocketAddress address) throws IOException { | ||
channel.send(ByteBuffer.wrap(msg.getBytes(UTF_8)), address); | ||
LOGGER.log(INFO, "Client SND: " + msg); | ||
byte[] bytes = new byte[msg.length()]; | ||
ByteBuffer buffer = ByteBuffer.wrap(bytes); | ||
InetSocketAddress remote = (InetSocketAddress) channel.receive(buffer); | ||
String rcv = new String(bytes, UTF_8); | ||
LOGGER.log(INFO, "Client RCV: " + rcv); | ||
assertThat(rcv, is(msg)); | ||
assertThat(remote.getHostName(), is("localhost")); | ||
assertThat(remote.getPort(), is(address.getPort())); | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
webserver/tests/udp/src/test/java/io/helidon/webserver/tests/udp/UdpPortsTest.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,120 @@ | ||
/* | ||
* 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.tests.udp; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.nio.channels.DatagramChannel; | ||
|
||
import io.helidon.webserver.WebServer; | ||
import io.helidon.webserver.WebServerConfig; | ||
import io.helidon.webserver.testing.junit5.ServerTest; | ||
import io.helidon.webserver.testing.junit5.SetUpServer; | ||
import io.helidon.webserver.udp.UdpEndpoint; | ||
import io.helidon.webserver.udp.UdpMessage; | ||
|
||
import jakarta.json.JsonObject; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static java.lang.System.Logger.Level.INFO; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
/** | ||
* Tests multiple UDP ports with different endpoint services and media types. | ||
*/ | ||
@ServerTest | ||
class UdpPortsTest extends UdpBaseTest { | ||
private static final System.Logger LOGGER = System.getLogger(UdpPortsTest.class.getName()); | ||
|
||
private final InetSocketAddress address; | ||
private final InetSocketAddress addressJson; | ||
|
||
public UdpPortsTest(WebServer webServer) { | ||
this.address = new InetSocketAddress("localhost", webServer.port("text")); | ||
this.addressJson = new InetSocketAddress("localhost", webServer.port("json")); | ||
} | ||
|
||
@SetUpServer | ||
static void setupServer(WebServerConfig.Builder builder) { | ||
builder.putSocket("text", | ||
lc -> lc.host("localhost") | ||
.port(0) | ||
.udp(true) | ||
.udpEndpoint(new EchoService())); | ||
builder.putSocket("json", | ||
lc -> lc.host("localhost") | ||
.port(0) | ||
.udp(true) | ||
.udpEndpoint(new EchoServiceJson())); | ||
} | ||
|
||
@Test | ||
void testEndpoint() throws Exception { | ||
echoMessage("hello", address); | ||
echoMessage("how are you?", address); | ||
echoMessage("good bye", address); | ||
} | ||
|
||
@Test | ||
void testJsonEndpoint() throws Exception { | ||
echoMessage("{\"msg\":\"hello\"}", addressJson); | ||
} | ||
|
||
@Test | ||
void testEndpointConnected() throws Exception { | ||
try (DatagramChannel channel = DatagramChannel.open()) { | ||
channel.connect(address); | ||
assertThat(channel.isConnected(), is(true)); | ||
echoMessageOnChannel("hello", channel, address); | ||
echoMessageOnChannel("how are you?", channel, address); | ||
echoMessageOnChannel("good bye", channel, address); | ||
channel.disconnect(); | ||
assertThat(channel.isConnected(), is(false)); | ||
} | ||
} | ||
|
||
static class EchoService implements UdpEndpoint { | ||
|
||
@Override | ||
public void onMessage(UdpMessage message) { | ||
try { | ||
String str = message.as(String.class); | ||
LOGGER.log(INFO, "Server RCV: " + str); | ||
message.udpClient().sendMessage(str); | ||
LOGGER.log(INFO, "Server SND: " + str); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
|
||
static class EchoServiceJson implements UdpEndpoint { | ||
|
||
@Override | ||
public void onMessage(UdpMessage message) { | ||
try { | ||
JsonObject json = message.as(JsonObject.class); | ||
LOGGER.log(INFO, "Server RCV: " + json); | ||
message.udpClient().sendMessage(json); | ||
LOGGER.log(INFO, "Server SND: " + json); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.