View Javadoc
1   /*
2    * Copyright 2017 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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          // Currently, not works on windows
56          // See https://github.com/netty/netty/issues/11285
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  }