1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }