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