View Javadoc
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.List;
43  
44  public class SocketTestPermutation {
45  
46      static final String BAD_HOST = SystemPropertyUtil.get("io.netty.testsuite.badHost", "198.51.100.254");
47      static final int BAD_PORT = SystemPropertyUtil.getInt("io.netty.testsuite.badPort", 65535);
48      static final boolean INCLUDE_OIO = SystemPropertyUtil.getBoolean("io.netty.testsuite.includeOio", false);
49  
50      // See /etc/services
51      public static final int UNASSIGNED_PORT = 4;
52  
53      static {
54          InternalLogger logger = InternalLoggerFactory.getInstance(SocketConnectionAttemptTest.class);
55          logger.debug("-Dio.netty.testsuite.badHost: {}", BAD_HOST);
56          logger.debug("-Dio.netty.testsuite.badPort: {}", BAD_PORT);
57      }
58  
59      static final SocketTestPermutation INSTANCE = new SocketTestPermutation();
60  
61      protected static final int NUM_THREADS = 4;
62  
63      protected static final int OIO_SO_TIMEOUT = 10;  // Use short timeout for faster runs.
64  
65      protected final EventLoopGroup NIO_GROUP = new NioEventLoopGroup(
66              NUM_THREADS, new DefaultThreadFactory("testsuite-nio", true));
67      protected final EventLoopGroup OIO_GROUP =
68              new OioEventLoopGroup(Integer.MAX_VALUE, new DefaultThreadFactory("testsuite-oio", true));
69  
70      protected <A extends AbstractBootstrap<?, ?>, B extends AbstractBootstrap<?, ?>>
71  
72      List<BootstrapComboFactory<A, B>> combo(List<BootstrapFactory<A>> sbfs, List<BootstrapFactory<B>> cbfs) {
73  
74          List<BootstrapComboFactory<A, B>> list = new ArrayList<BootstrapComboFactory<A, B>>();
75  
76          // Populate the combinations
77          for (BootstrapFactory<A> sbf: sbfs) {
78              for (BootstrapFactory<B> cbf: cbfs) {
79                  final BootstrapFactory<A> sbf0 = sbf;
80                  final BootstrapFactory<B> cbf0 = cbf;
81                  list.add(new BootstrapComboFactory<A, B>() {
82                      @Override
83                      public A newServerInstance() {
84                          return sbf0.newInstance();
85                      }
86  
87                      @Override
88                      public B newClientInstance() {
89                          return cbf0.newInstance();
90                      }
91                  });
92              }
93          }
94  
95          return list;
96      }
97  
98      public List<BootstrapComboFactory<ServerBootstrap, Bootstrap>> socket() {
99          // Make the list of ServerBootstrap factories.
100         List<BootstrapFactory<ServerBootstrap>> sbfs = serverSocket();
101 
102         // Make the list of Bootstrap factories.
103         List<BootstrapFactory<Bootstrap>> cbfs = clientSocket();
104 
105         // Populate the combinations
106         List<BootstrapComboFactory<ServerBootstrap, Bootstrap>> list = combo(sbfs, cbfs);
107 
108         if (INCLUDE_OIO) {
109             // Remove the OIO-OIO case which often leads to a dead lock by its nature.
110             list.remove(list.size() - 1);
111         }
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         if (INCLUDE_OIO) {
127             // Remove the OIO-OIO case which often leads to a dead lock by its nature.
128             list.remove(list.size() - 1);
129         }
130 
131         return list;
132     }
133 
134     public List<BootstrapComboFactory<Bootstrap, Bootstrap>> datagram(final InternetProtocolFamily family) {
135         // Make the list of Bootstrap factories.
136         List<BootstrapFactory<Bootstrap>> bfs = new ArrayList<BootstrapFactory<Bootstrap>>();
137 
138         bfs.add(new BootstrapFactory<Bootstrap>() {
139             @Override
140             public Bootstrap newInstance() {
141                 return new Bootstrap().group(NIO_GROUP).channelFactory(new ChannelFactory<Channel>() {
142                     @Override
143                     public Channel newChannel() {
144                         return new NioDatagramChannel(family);
145                     }
146 
147                     @Override
148                     public String toString() {
149                         return NioDatagramChannel.class.getSimpleName() + ".class";
150                     }
151                 });
152             }
153         });
154         if (INCLUDE_OIO) {
155             bfs.add(new BootstrapFactory<Bootstrap>() {
156                 @Override
157                 public Bootstrap newInstance() {
158                     return new Bootstrap().group(OIO_GROUP).channel(OioDatagramChannel.class)
159                             .option(ChannelOption.SO_TIMEOUT, OIO_SO_TIMEOUT);
160                 }
161             });
162         }
163 
164         // Populare the combinations.
165         return combo(bfs, bfs);
166     }
167 
168     public List<BootstrapFactory<ServerBootstrap>> serverSocket() {
169         List<BootstrapFactory<ServerBootstrap>> factories = new ArrayList<BootstrapFactory<ServerBootstrap>>();
170         factories.add(new BootstrapFactory<ServerBootstrap>() {
171             @Override
172             public ServerBootstrap newInstance() {
173                 return new ServerBootstrap().group(NIO_GROUP)
174                         .channel(NioServerSocketChannel.class);
175             }
176         });
177         if (INCLUDE_OIO) {
178             factories.add(new BootstrapFactory<ServerBootstrap>() {
179                 @Override
180                 public ServerBootstrap newInstance() {
181                     return new ServerBootstrap().group(OIO_GROUP)
182                             .channel(OioServerSocketChannel.class)
183                             .option(ChannelOption.SO_TIMEOUT, OIO_SO_TIMEOUT);
184                 }
185             });
186         }
187 
188         return factories;
189     }
190 
191     public List<BootstrapFactory<Bootstrap>> clientSocket() {
192         List<BootstrapFactory<Bootstrap>> factories = new ArrayList<BootstrapFactory<Bootstrap>>();
193         factories.add(new BootstrapFactory<Bootstrap>() {
194             @Override
195             public Bootstrap newInstance() {
196                 return new Bootstrap().group(NIO_GROUP).channel(NioSocketChannel.class);
197             }
198         });
199         if (INCLUDE_OIO) {
200             factories.add(new BootstrapFactory<Bootstrap>() {
201                 @Override
202                 public Bootstrap newInstance() {
203                     return new Bootstrap().group(OIO_GROUP).channel(OioSocketChannel.class)
204                             .option(ChannelOption.SO_TIMEOUT, OIO_SO_TIMEOUT);
205                 }
206             });
207         }
208         return factories;
209     }
210 
211     public List<BootstrapFactory<Bootstrap>> clientSocketWithFastOpen() {
212         return clientSocket();
213     }
214 
215     public List<BootstrapFactory<Bootstrap>> datagramSocket() {
216         List<BootstrapFactory<Bootstrap>> factories = new ArrayList<BootstrapFactory<Bootstrap>>();
217         factories.add(new BootstrapFactory<Bootstrap>() {
218             @Override
219             public Bootstrap newInstance() {
220                 return new Bootstrap().group(NIO_GROUP).channel(NioDatagramChannel.class);
221             }
222         });
223         if (INCLUDE_OIO) {
224             factories.add(new BootstrapFactory<Bootstrap>() {
225                 @Override
226                 public Bootstrap newInstance() {
227                     return new Bootstrap().group(OIO_GROUP).channel(OioDatagramChannel.class)
228                             .option(ChannelOption.SO_TIMEOUT, OIO_SO_TIMEOUT);
229                 }
230             });
231         }
232          return factories;
233     }
234 }