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 a288854 commit 8f9f2ce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
package org.opencoap.coap.netty;

import static com.mbed.coap.utils.Validations.require;
import static java.util.stream.Collectors.toList;
import static org.opencoap.coap.netty.CoapCodec.EMPTY_RESOLVER;
import com.mbed.coap.server.CoapServer;
import com.mbed.coap.server.CoapServerBuilder;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.EventLoopGroup;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class MultiCoapServer {
Expand All @@ -47,7 +46,7 @@ public static MultiCoapServer create(CoapServerBuilder builder, Bootstrap bootst
.executor(executor)
.build()
)
.collect(Collectors.toList());
.collect(toList());

return new MultiCoapServer(servers);
}
Expand All @@ -65,7 +64,7 @@ public void stop() {
}
}

public InetSocketAddress getLocalSocketAddress() {
return servers.get(0).getLocalSocketAddress();
public List<Integer> getLocalPorts() {
return servers.stream().map(server -> server.getLocalSocketAddress().getPort()).collect(toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
class LinuxMultiChannelNettyTest extends MultiChannelNettyTest {

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

return new Bootstrap()
.group(eventLoopGroup)
.option(UnixChannelOption.SO_REUSEPORT, true)
.localAddress(port)
.localAddress(65001) // bind multiple times on single port
.channel(EpollDatagramChannel.class)
.handler(new ChannelInitializer<DatagramChannel>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import static com.mbed.coap.packet.CoapRequest.get;
import static com.mbed.coap.packet.CoapResponse.ok;
import static com.mbed.coap.transport.udp.DatagramSocketTransport.udp;
import static com.mbed.coap.utils.Assertions.assertEquals;
import static com.mbed.coap.utils.Networks.localhost;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opencoap.coap.netty.CoapCodec.EMPTY_RESOLVER;
import com.mbed.coap.client.CoapClient;
import com.mbed.coap.server.CoapServer;
import com.mbed.coap.server.CoapServerBuilder;
Expand All @@ -31,15 +31,14 @@
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.nio.NioDatagramChannel;
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 Bootstrap bootstrap = createBootstrap(0);
private static final int THREADS = 3;
private final Bootstrap bootstrap = createBootstrap();

@Test
void test() throws Exception {
Expand All @@ -49,10 +48,11 @@ void test() throws Exception {
);
MultiCoapServer coapServer = MultiCoapServer.create(builder, bootstrap).start();

for (int i = 0; i < 3; i++) {
// verify that all channels are working
for (int i = 0; i < THREADS; i++) {
CoapClient client = CoapServer.builder()
.transport(new NettyCoapTransport(bootstrap, EMPTY_RESOLVER))
.buildClient(localhost(coapServer.getLocalSocketAddress().getPort()));
.transport(udp())
.buildClient(localhost(coapServer.getLocalPorts().get(i)));

assertTrue(client.ping().get());
assertEquals(ok("OK"), client.sendSync(get("/test")));
Expand All @@ -61,13 +61,11 @@ void test() throws Exception {
coapServer.stop();
}

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

protected Bootstrap createBootstrap() {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup(THREADS, new DefaultThreadFactory("udp", true));
return new Bootstrap()
.group(eventLoopGroup)
.option(UnixChannelOption.SO_REUSEPORT, true)
.localAddress(port)
.localAddress(0) // this will case server binding to multiple ports
.channel(NioDatagramChannel.class)
.handler(new ChannelInitializer<DatagramChannel>() {
@Override
Expand Down

0 comments on commit 8f9f2ce

Please sign in to comment.