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.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          // Currently not works on windows
62          // See https://github.com/netty/netty/issues/11285
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  }