View Javadoc
1   /*
2    * Copyright 2016 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.bootstrap.ServerBootstrap;
20  import io.netty.channel.Channel;
21  import io.netty.channel.ChannelFuture;
22  import io.netty.channel.ChannelInboundHandlerAdapter;
23  import io.netty.channel.EventLoopGroup;
24  import io.netty.channel.IoEventLoopGroup;
25  import io.netty.channel.nio.NioIoHandler;
26  import io.netty.testsuite.transport.TestsuitePermutation;
27  import io.netty.util.NetUtil;
28  import org.junit.jupiter.api.Test;
29  import org.junit.jupiter.api.TestInfo;
30  import org.junit.jupiter.api.Timeout;
31  
32  import java.nio.channels.AlreadyConnectedException;
33  import java.util.ArrayList;
34  import java.util.List;
35  import java.util.concurrent.TimeUnit;
36  
37  import static org.junit.jupiter.api.Assertions.assertTrue;
38  
39  public class SocketMultipleConnectTest extends AbstractSocketTest {
40  
41      @Test
42      @Timeout(value = 30000, unit = TimeUnit.MILLISECONDS)
43      public void testMultipleConnect(TestInfo testInfo) throws Throwable {
44          run(testInfo, new Runner<ServerBootstrap, Bootstrap>() {
45              @Override
46              public void run(ServerBootstrap serverBootstrap, Bootstrap bootstrap) throws Throwable {
47                  testMultipleConnect(serverBootstrap, bootstrap);
48              }
49          });
50      }
51  
52      public void testMultipleConnect(ServerBootstrap sb, Bootstrap cb) throws Exception {
53          Channel sc = null;
54          Channel cc = null;
55          try {
56              sb.childHandler(new ChannelInboundHandlerAdapter());
57              sc = sb.bind(NetUtil.LOCALHOST, 0).syncUninterruptibly().channel();
58  
59              cb.handler(new ChannelInboundHandlerAdapter());
60              cc = cb.register().syncUninterruptibly().channel();
61              cc.connect(sc.localAddress()).syncUninterruptibly();
62              ChannelFuture connectFuture2 = cc.connect(sc.localAddress()).await();
63              assertTrue(connectFuture2.cause() instanceof AlreadyConnectedException);
64          } finally {
65              if (cc != null) {
66                  cc.close();
67              }
68              if (sc != null) {
69                  sc.close();
70              }
71          }
72      }
73  
74      @Override
75      protected List<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>> newFactories() {
76          List<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>> factories
77                  = new ArrayList<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>>();
78          for (TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap> comboFactory
79                  : SocketTestPermutation.INSTANCE.socketWithFastOpen()) {
80              EventLoopGroup group = comboFactory.newClientInstance().config().group();
81              if (group instanceof IoEventLoopGroup && ((IoEventLoopGroup) group).isIoType(NioIoHandler.class)) {
82                  factories.add(comboFactory);
83              }
84          }
85          return factories;
86      }
87  }