查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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.nio.NioEventLoopGroup;
24  import io.netty.testsuite.transport.TestsuitePermutation;
25  import io.netty.util.NetUtil;
26  import org.junit.jupiter.api.Test;
27  import org.junit.jupiter.api.TestInfo;
28  import org.junit.jupiter.api.Timeout;
29  
30  import java.nio.channels.AlreadyConnectedException;
31  import java.util.ArrayList;
32  import java.util.List;
33  import java.util.concurrent.TimeUnit;
34  
35  import static org.junit.jupiter.api.Assertions.assertTrue;
36  
37  public class SocketMultipleConnectTest extends AbstractSocketTest {
38  
39      @Test
40      @Timeout(value = 30000, unit = TimeUnit.MILLISECONDS)
41      public void testMultipleConnect(TestInfo testInfo) throws Throwable {
42          run(testInfo, new Runner<ServerBootstrap, Bootstrap>() {
43              @Override
44              public void run(ServerBootstrap serverBootstrap, Bootstrap bootstrap) throws Throwable {
45                  testMultipleConnect(serverBootstrap, bootstrap);
46              }
47          });
48      }
49  
50      public void testMultipleConnect(ServerBootstrap sb, Bootstrap cb) throws Exception {
51          Channel sc = null;
52          Channel cc = null;
53          try {
54              sb.childHandler(new ChannelInboundHandlerAdapter());
55              sc = sb.bind(NetUtil.LOCALHOST, 0).syncUninterruptibly().channel();
56  
57              cb.handler(new ChannelInboundHandlerAdapter());
58              cc = cb.register().syncUninterruptibly().channel();
59              cc.connect(sc.localAddress()).syncUninterruptibly();
60              ChannelFuture connectFuture2 = cc.connect(sc.localAddress()).await();
61              assertTrue(connectFuture2.cause() instanceof AlreadyConnectedException);
62          } finally {
63              if (cc != null) {
64                  cc.close();
65              }
66              if (sc != null) {
67                  sc.close();
68              }
69          }
70      }
71  
72      @Override
73      protected List<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>> newFactories() {
74          List<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>> factories
75                  = new ArrayList<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>>();
76          for (TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap> comboFactory
77                  : SocketTestPermutation.INSTANCE.socketWithFastOpen()) {
78              if (comboFactory.newClientInstance().config().group() instanceof NioEventLoopGroup) {
79                  factories.add(comboFactory);
80              }
81          }
82          return factories;
83      }
84  }