Skip to content

Commit

Permalink
Prototype support for a UDP API. Simple extension of existing testing…
Browse files Browse the repository at this point in the history
… framework to support UDP.
  • Loading branch information
spericas committed Nov 20, 2024
1 parent ba8bdd1 commit a26d4cf
Show file tree
Hide file tree
Showing 23 changed files with 1,084 additions and 5 deletions.
4 changes: 4 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,10 @@
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-concurrency-limits</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-udp</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webserver.testing.junit5</groupId>
<artifactId>helidon-webserver-testing-junit5</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,11 @@
<artifactId>helidon-webserver-concurrency-limits</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-udp</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.webserver.testing.junit5</groupId>
<artifactId>helidon-webserver-testing-junit5</artifactId>
Expand Down
1 change: 1 addition & 0 deletions webserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<module>webserver</module>
<module>websocket</module>
<module>concurrency-limits</module>
<module>udp</module>
</modules>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* 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.
Expand Down Expand Up @@ -84,7 +84,8 @@ public void beforeAll(ExtensionContext context) {

WebServerConfig.Builder builder = WebServer.builder()
.config(GlobalConfig.config().get("server"))
.host("localhost");
.host("localhost")
.udp(testAnnot.udp());

extensions.forEach(it -> it.beforeAll(context));
extensions.forEach(it -> it.updateServerBuilder(builder));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* 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.
Expand Down Expand Up @@ -37,4 +37,11 @@
@ExtendWith(HelidonServerJunitExtension.class)
@Inherited
public @interface ServerTest {

/**
* Port is of type UDP.
*
* @return UDP port
*/
boolean udp() default false;
}
1 change: 1 addition & 0 deletions webserver/tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<module>upgrade</module>
<module>webserver</module>
<module>websocket</module>
<module>udp</module>
</modules>

<properties>
Expand Down
64 changes: 64 additions & 0 deletions webserver/tests/udp/pom.xml
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>
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()));
}
}
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);
}
}
}
}
Loading

0 comments on commit a26d4cf

Please sign in to comment.