查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 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.AbstractBootstrap;
19  import io.netty.bootstrap.Bootstrap;
20  import io.netty.bootstrap.ServerBootstrap;
21  import io.netty.channel.Channel;
22  import io.netty.channel.ChannelFactory;
23  import io.netty.channel.ChannelOption;
24  import io.netty.channel.EventLoopGroup;
25  import io.netty.channel.nio.NioEventLoopGroup;
26  import io.netty.channel.oio.OioEventLoopGroup;
27  import io.netty.channel.socket.InternetProtocolFamily;
28  import io.netty.channel.socket.nio.NioDatagramChannel;
29  import io.netty.channel.socket.nio.NioServerSocketChannel;
30  import io.netty.channel.socket.nio.NioSocketChannel;
31  import io.netty.channel.socket.oio.OioDatagramChannel;
32  import io.netty.channel.socket.oio.OioServerSocketChannel;
33  import io.netty.channel.socket.oio.OioSocketChannel;
34  import io.netty.testsuite.transport.TestsuitePermutation.BootstrapComboFactory;
35  import io.netty.testsuite.transport.TestsuitePermutation.BootstrapFactory;
36  import io.netty.util.concurrent.DefaultThreadFactory;
37  import io.netty.util.internal.SystemPropertyUtil;
38  import io.netty.util.internal.logging.InternalLogger;
39  import io.netty.util.internal.logging.InternalLoggerFactory;
40  
41  import java.util.ArrayList;
42  import java.util.Arrays;
43  import java.util.List;
44  
45  public class SocketTestPermutation {
46  
47      static final String BAD_HOST = SystemPropertyUtil.get("io.netty.testsuite.badHost", "198.51.100.254");
48      static final int BAD_PORT = SystemPropertyUtil.getInt("io.netty.testsuite.badPort", 65535);
49  
50      static {
51          InternalLogger logger = InternalLoggerFactory.getInstance(SocketConnectionAttemptTest.class);
52          logger.debug("-Dio.netty.testsuite.badHost: {}", BAD_HOST);
53          logger.debug("-Dio.netty.testsuite.badPort: {}", BAD_PORT);
54      }
55  
56      static final SocketTestPermutation INSTANCE = new SocketTestPermutation();
57  
58      protected static final int BOSSES = 2;
59      protected static final int WORKERS = 3;
60  
61      protected static final int OIO_SO_TIMEOUT = 10;  // Use short timeout for faster runs.
62  
63      protected final EventLoopGroup nioBossGroup =
64              new NioEventLoopGroup(BOSSES, new DefaultThreadFactory("testsuite-nio-boss", true));
65      protected final EventLoopGroup nioWorkerGroup =
66              new NioEventLoopGroup(WORKERS, new DefaultThreadFactory("testsuite-nio-worker", true));
67      protected final EventLoopGroup oioBossGroup =
68              new OioEventLoopGroup(Integer.MAX_VALUE, new DefaultThreadFactory("testsuite-oio-boss", true));
69      protected final EventLoopGroup oioWorkerGroup =
70              new OioEventLoopGroup(Integer.MAX_VALUE, new DefaultThreadFactory("testsuite-oio-worker", true));
71  
72      protected <A extends AbstractBootstrap<?, ?>, B extends AbstractBootstrap<?, ?>>
73  
74      List<BootstrapComboFactory<A, B>> combo(List<BootstrapFactory<A>> sbfs, List<BootstrapFactory<B>> cbfs) {
75  
76          List<BootstrapComboFactory<A, B>> list = new ArrayList<BootstrapComboFactory<A, B>>();
77  
78          // Populate the combinations
79          for (BootstrapFactory<A> sbf: sbfs) {
80              for (BootstrapFactory<B> cbf: cbfs) {
81                  final BootstrapFactory<A> sbf0 = sbf;
82                  final BootstrapFactory<B> cbf0 = cbf;
83                  list.add(new BootstrapComboFactory<A, B>() {
84                      @Override
85                      public A newServerInstance() {
86                          return sbf0.newInstance();
87                      }
88  
89                      @Override
90                      public B newClientInstance() {
91                          return cbf0.newInstance();
92                      }
93                  });
94              }
95          }
96  
97          return list;
98      }
99  
100     public List<BootstrapComboFactory<ServerBootstrap, Bootstrap>> socket() {
101         // Make the list of ServerBootstrap factories.
102         List<BootstrapFactory<ServerBootstrap>> sbfs = serverSocket();
103 
104         // Make the list of Bootstrap factories.
105         List<BootstrapFactory<Bootstrap>> cbfs = clientSocket();
106 
107         // Populate the combinations
108         List<BootstrapComboFactory<ServerBootstrap, Bootstrap>> list = combo(sbfs, cbfs);
109 
110         // Remove the OIO-OIO case which often leads to a dead lock by its nature.
111         list.remove(list.size() - 1);
112 
113         return list;
114     }
115 
116     public List<BootstrapComboFactory<ServerBootstrap, Bootstrap>> socketWithFastOpen() {
117         // Make the list of ServerBootstrap factories.
118         List<BootstrapFactory<ServerBootstrap>> sbfs = serverSocket();
119 
120         // Make the list of Bootstrap factories.
121         List<BootstrapFactory<Bootstrap>> cbfs = clientSocketWithFastOpen();
122 
123         // Populate the combinations
124         List<BootstrapComboFactory<ServerBootstrap, Bootstrap>> list = combo(sbfs, cbfs);
125 
126         // Remove the OIO-OIO case which often leads to a dead lock by its nature.
127         list.remove(list.size() - 1);
128 
129         return list;
130     }
131 
132     public List<BootstrapComboFactory<Bootstrap, Bootstrap>> datagram(final InternetProtocolFamily family) {
133         // Make the list of Bootstrap factories.
134         List<BootstrapFactory<Bootstrap>> bfs = Arrays.asList(
135                 new BootstrapFactory<Bootstrap>() {
136                     @Override
137                     public Bootstrap newInstance() {
138                         return new Bootstrap().group(nioWorkerGroup).channelFactory(new ChannelFactory<Channel>() {
139                             @Override
140                             public Channel newChannel() {
141                                 return new NioDatagramChannel(family);
142                             }
143 
144                             @Override
145                             public String toString() {
146                                 return NioDatagramChannel.class.getSimpleName() + ".class";
147                             }
148                         });
149                     }
150                 },
151                 new BootstrapFactory<Bootstrap>() {
152                     @Override
153                     public Bootstrap newInstance() {
154                         return new Bootstrap().group(oioWorkerGroup).channel(OioDatagramChannel.class)
155                                 .option(ChannelOption.SO_TIMEOUT, OIO_SO_TIMEOUT);
156                     }
157                 }
158         );
159 
160         // Populare the combinations.
161         return combo(bfs, bfs);
162     }
163 
164     public List<BootstrapFactory<ServerBootstrap>> serverSocket() {
165         return Arrays.asList(
166                 new BootstrapFactory<ServerBootstrap>() {
167                     @Override
168                     public ServerBootstrap newInstance() {
169                         return new ServerBootstrap().group(nioBossGroup, nioWorkerGroup)
170                                 .channel(NioServerSocketChannel.class);
171                     }
172                 },
173                 new BootstrapFactory<ServerBootstrap>() {
174                     @Override
175                     public ServerBootstrap newInstance() {
176                         return new ServerBootstrap().group(oioBossGroup, oioWorkerGroup)
177                                 .channel(OioServerSocketChannel.class)
178                                 .option(ChannelOption.SO_TIMEOUT, OIO_SO_TIMEOUT);
179                     }
180                 }
181         );
182     }
183 
184     public List<BootstrapFactory<Bootstrap>> clientSocket() {
185         return Arrays.asList(
186                 new BootstrapFactory<Bootstrap>() {
187                     @Override
188                     public Bootstrap newInstance() {
189                         return new Bootstrap().group(nioWorkerGroup).channel(NioSocketChannel.class);
190                     }
191                 },
192                 new BootstrapFactory<Bootstrap>() {
193                     @Override
194                     public Bootstrap newInstance() {
195                         return new Bootstrap().group(oioWorkerGroup).channel(OioSocketChannel.class)
196                                 .option(ChannelOption.SO_TIMEOUT, OIO_SO_TIMEOUT);
197                     }
198                 }
199         );
200     }
201 
202     public List<BootstrapFactory<Bootstrap>> clientSocketWithFastOpen() {
203         return clientSocket();
204     }
205 
206     public List<BootstrapFactory<Bootstrap>> datagramSocket() {
207         return Arrays.asList(
208                 new BootstrapFactory<Bootstrap>() {
209                     @Override
210                     public Bootstrap newInstance() {
211                         return new Bootstrap().group(nioWorkerGroup).channel(NioDatagramChannel.class);
212                     }
213                 },
214                 new BootstrapFactory<Bootstrap>() {
215                     @Override
216                     public Bootstrap newInstance() {
217                         return new Bootstrap().group(oioWorkerGroup).channel(OioDatagramChannel.class)
218                                 .option(ChannelOption.SO_TIMEOUT, OIO_SO_TIMEOUT);
219                     }
220                 }
221         );
222     }
223 }