Skip to content

Commit

Permalink
impr
Browse files Browse the repository at this point in the history
  • Loading branch information
szysas committed Aug 28, 2024
1 parent 37fb763 commit fa1c562
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.mbed.coap.packet.CoapResponse.ok;
import static com.mbed.coap.packet.Opaque.of;
import static com.mbed.coap.utils.Networks.localhost;
import static java.lang.Thread.currentThread;
import static java.time.Duration.ofSeconds;
import static java.util.concurrent.CompletableFuture.supplyAsync;
import static org.awaitility.Awaitility.await;
Expand Down Expand Up @@ -100,9 +101,8 @@ void afterAll() {
void multi_thread_server() throws Exception {
Set<String> usedThreads = new HashSet<>();
CoapServerBuilder serverBuilder = CoapServer.builder()
.executor(eventLoopGroup)
.route(RouterService.builder()
.get("/currentThread", req -> supplyAsync(() -> ok(Thread.currentThread().getName()).build(), eventLoopGroup))
.get("/currentThread", req -> supplyAsync(() -> ok(currentThread().getName()).build(), eventLoopGroup))
);
MultiCoapServer server = MultiCoapServer.create(serverBuilder, serverBootstrap).start();

Expand Down
1 change: 1 addition & 0 deletions coap-netty/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies {

testImplementation(testFixtures(project(":coap-core")))
testImplementation("ch.qos.logback:logback-classic:1.3.14")
testImplementation("io.netty:netty-transport-native-epoll:4.1.112.Final:linux-x86_64")

jmhImplementation("io.netty:netty-all:4.1.112.Final")
jmh("org.openjdk.jmh:jmh-core:1.37")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap)
* SPDX-License-Identifier: Apache-2.0
* 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 org.opencoap.coap.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.EpollDatagramChannel;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.unix.UnixChannelOption;
import io.netty.util.concurrent.DefaultThreadFactory;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;

@Nested
@EnabledOnOs(OS.LINUX)
class LinuxMultiChannelNettyTest extends MultiChannelNettyTest {

@Override
protected Bootstrap createBootstrap(int port) {
EventLoopGroup eventLoopGroup = new EpollEventLoopGroup(3, new DefaultThreadFactory("udp", true));

return new Bootstrap()
.group(eventLoopGroup)
.option(UnixChannelOption.SO_REUSEPORT, true)
.localAddress(port)
.channel(EpollDatagramChannel.class)
.handler(new ChannelInitializer<DatagramChannel>() {
@Override
protected void initChannel(DatagramChannel ch) throws Exception {
// do nothing
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,36 @@
import io.netty.channel.unix.UnixChannelOption;
import io.netty.util.concurrent.DefaultThreadFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class MultiChannelNettyTest {

private final EventLoopGroup eventLoopGroup = new NioEventLoopGroup(3, new DefaultThreadFactory("udp", true));

private final Bootstrap bootstrap = createBootstrap(0);

@Test
void test() throws Exception {
CoapServerBuilder builder = CoapServer.builder()
.route(RouterService.builder()
.get("/test", __ -> ok("OK").toFuture())
);
MultiCoapServer coapServer = MultiCoapServer.create(builder, bootstrap).start();

MultiCoapServer coapServer = MultiCoapServer.create(builder, createBootstrap(0));

coapServer.start();
for (int i = 0; i < 3; i++) {
CoapClient client = CoapServer.builder()
.transport(new NettyCoapTransport(bootstrap, EMPTY_RESOLVER))
.buildClient(localhost(coapServer.getLocalSocketAddress().getPort()));

CoapClient client = CoapServer.builder()
.transport(new NettyCoapTransport(createBootstrap(0), EMPTY_RESOLVER))
.buildClient(localhost(coapServer.getLocalSocketAddress().getPort()));

assertTrue(client.ping().get());
assertEquals(ok("OK"), client.sendSync(get("/test")));
assertTrue(client.ping().get());
assertEquals(ok("OK"), client.sendSync(get("/test")));
}

coapServer.stop();
}

private Bootstrap createBootstrap(int port) {
protected Bootstrap createBootstrap(int port) {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup(3, new DefaultThreadFactory("udp", true));

return new Bootstrap()
.group(eventLoopGroup)
.option(UnixChannelOption.SO_REUSEPORT, true)
Expand Down

0 comments on commit fa1c562

Please sign in to comment.