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.netty5.testsuite.transport.socket;
17  
18  import io.netty5.bootstrap.Bootstrap;
19  import io.netty5.bootstrap.ServerBootstrap;
20  import io.netty5.channel.Channel;
21  import io.netty5.channel.ChannelHandler;
22  import io.netty5.testsuite.transport.TestsuitePermutation;
23  import io.netty5.util.NetUtil;
24  import org.junit.jupiter.api.Test;
25  import org.junit.jupiter.api.TestInfo;
26  import org.junit.jupiter.api.Timeout;
27  
28  import java.nio.channels.AlreadyConnectedException;
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.concurrent.TimeUnit;
32  
33  import static org.assertj.core.api.Assertions.assertThat;
34  
35  public class SocketMultipleConnectTest extends AbstractSocketTest {
36  
37      @Test
38      @Timeout(value = 30000, unit = TimeUnit.MILLISECONDS)
39      public void testMultipleConnect(TestInfo testInfo) throws Throwable {
40          run(testInfo, this::testMultipleConnect);
41      }
42  
43      public void testMultipleConnect(ServerBootstrap sb, Bootstrap cb) throws Exception {
44          Channel sc = null;
45          Channel cc = null;
46          try {
47              sb.childHandler(new ChannelHandler() { });
48              sc = sb.bind(NetUtil.LOCALHOST, 0).asStage().get();
49  
50              cb.handler(new ChannelHandler() { });
51              cc = cb.register().asStage().get();
52              cc.connect(sc.localAddress()).asStage().sync();
53              Throwable cause = cc.connect(sc.localAddress()).asStage().getCause();
54              assertThat(cause).isInstanceOf(AlreadyConnectedException.class);
55          } finally {
56              if (cc != null) {
57                  cc.close();
58              }
59              if (sc != null) {
60                  sc.close();
61              }
62          }
63      }
64  
65      @Override
66      protected List<TestsuitePermutation.BootstrapComboFactory<ServerBootstrap, Bootstrap>> newFactories() {
67          return new ArrayList<>(SocketTestPermutation.INSTANCE.socketWithFastOpen());
68      }
69  }