Skip to content

Commit

Permalink
Move tests to DatagramUnicastInetTest because the JDK DatagramPacket …
Browse files Browse the repository at this point in the history
…does not support domain sockets
  • Loading branch information
chrisvest committed Jul 27, 2022
1 parent 770555d commit a443211
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 37 deletions.
Expand Up @@ -25,12 +25,15 @@
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.internal.EmptyArrays;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicReference;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -58,6 +61,38 @@ private static void testBindWithPortOnly(Bootstrap cb) throws Throwable {
}
}

@Test
public void testReceiveEmptyDatagrams(TestInfo testInfo) throws Throwable {
run(testInfo, new Runner<Bootstrap, Bootstrap>() {
@Override
public void run(Bootstrap bootstrap, Bootstrap bootstrap2) throws Throwable {
testReceiveEmptyDatagrams(bootstrap, bootstrap2);
}
});
}

public void testReceiveEmptyDatagrams(Bootstrap sb, Bootstrap cb) throws Throwable {
final Semaphore semaphore = new Semaphore(0);
Channel server = sb.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new SimpleChannelInboundHandler<DatagramPacket>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
semaphore.release();
}
});
}
}).bind(newSocketAddress()).sync().channel();

SocketAddress address = server.localAddress();
DatagramSocket client = new DatagramSocket();
for (int i = 0; i < 100; i++) {
client.send(new java.net.DatagramPacket(EmptyArrays.EMPTY_BYTES, 0, address));
semaphore.acquire();
}
}

@Override
protected boolean isConnected(Channel channel) {
return ((DatagramChannel) channel).isConnected();
Expand Down
Expand Up @@ -22,17 +22,13 @@
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.NetUtil;
import io.netty.util.internal.EmptyArrays;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

import java.net.DatagramSocket;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
Expand All @@ -42,7 +38,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -193,38 +188,6 @@ public void testSimpleSendWithConnect(Bootstrap sb, Bootstrap cb) throws Throwab
testSimpleSendWithConnect(sb, cb, Unpooled.directBuffer().writeBytes(BYTES), BYTES, 4);
}

@Test
public void testReceiveEmptyDatagrams(TestInfo testInfo) throws Throwable {
run(testInfo, new Runner<Bootstrap, Bootstrap>() {
@Override
public void run(Bootstrap bootstrap, Bootstrap bootstrap2) throws Throwable {
testReceiveEmptyDatagrams(bootstrap, bootstrap2);
}
});
}

public void testReceiveEmptyDatagrams(Bootstrap sb, Bootstrap cb) throws Throwable {
final Semaphore semaphore = new Semaphore(0);
Channel server = sb.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new SimpleChannelInboundHandler<DatagramPacket>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
semaphore.release();
}
});
}
}).bind(newSocketAddress()).sync().channel();

SocketAddress address = server.localAddress();
DatagramSocket client = new DatagramSocket();
for (int i = 0; i < 100; i++) {
client.send(new java.net.DatagramPacket(EmptyArrays.EMPTY_BYTES, 0, address));
semaphore.acquire();
}
}

@SuppressWarnings("deprecation")
private void testSimpleSend0(Bootstrap sb, Bootstrap cb, ByteBuf buf, boolean bindClient,
final byte[] bytes, int count, WrapType wrapType)
Expand Down

0 comments on commit a443211

Please sign in to comment.