1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.testsuite.transport.socket;
17
18 import io.netty.bootstrap.Bootstrap;
19 import io.netty.buffer.Unpooled;
20 import io.netty.channel.Channel;
21 import io.netty.channel.ChannelFuture;
22 import io.netty.channel.ChannelHandlerContext;
23 import io.netty.channel.ChannelInboundHandlerAdapter;
24 import io.netty.channel.socket.oio.OioDatagramChannel;
25 import io.netty.testsuite.transport.TestsuitePermutation;
26 import io.netty.util.CharsetUtil;
27 import io.netty.util.NetUtil;
28 import io.netty.util.concurrent.ImmediateEventExecutor;
29 import io.netty.util.concurrent.Promise;
30 import io.netty.util.internal.PlatformDependent;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.TestInfo;
33 import org.junit.jupiter.api.Timeout;
34
35 import java.net.PortUnreachableException;
36 import java.util.List;
37 import java.util.concurrent.TimeUnit;
38
39 import static org.junit.jupiter.api.Assertions.assertTrue;
40 import static org.junit.jupiter.api.Assumptions.assumeFalse;
41
42 public class DatagramConnectNotExistsTest extends AbstractClientSocketTest {
43
44 @Override
45 protected List<TestsuitePermutation.BootstrapFactory<Bootstrap>> newFactories() {
46 return SocketTestPermutation.INSTANCE.datagramSocket();
47 }
48
49 @Test
50 @Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
51 public void testConnectNotExists(TestInfo testInfo) throws Throwable {
52 run(testInfo, new Runner<Bootstrap>() {
53 @Override
54 public void run(Bootstrap bootstrap) {
55 testConnectNotExists(bootstrap);
56 }
57 });
58 }
59
60 public void testConnectNotExists(Bootstrap cb) {
61
62
63 assumeFalse(PlatformDependent.isWindows());
64 final Promise<Throwable> promise = ImmediateEventExecutor.INSTANCE.newPromise();
65 cb.handler(new ChannelInboundHandlerAdapter() {
66 @Override
67 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
68 promise.trySuccess(cause);
69 }
70 });
71 ChannelFuture future = cb.connect(NetUtil.LOCALHOST, SocketTestPermutation.BAD_PORT);
72 try {
73 Channel datagramChannel = future.syncUninterruptibly().channel();
74 assertTrue(datagramChannel.isActive());
75 datagramChannel.writeAndFlush(
76 Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII)).syncUninterruptibly();
77 if (!(datagramChannel instanceof OioDatagramChannel)) {
78 assertTrue(promise.syncUninterruptibly().getNow() instanceof PortUnreachableException);
79 }
80 } finally {
81 future.channel().close();
82 }
83 }
84 }