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