查看本类的 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.ServerBootstrap;
19  import io.netty.channel.Channel;
20  import io.netty.channel.ChannelHandler;
21  import io.netty.channel.ChannelHandlerContext;
22  import io.netty.channel.ChannelInboundHandlerAdapter;
23  import io.netty.channel.ChannelOption;
24  import io.netty.util.internal.SocketUtils;
25  import org.junit.jupiter.api.Disabled;
26  import org.junit.jupiter.api.Test;
27  import org.junit.jupiter.api.TestInfo;
28  
29  import java.net.Socket;
30  import java.util.ArrayList;
31  import java.util.List;
32  import java.util.concurrent.CountDownLatch;
33  
34  import static org.junit.jupiter.api.Assertions.assertTrue;
35  
36  public class ServerSocketSuspendTest extends AbstractServerSocketTest {
37  
38      private static final int NUM_CHANNELS = 10;
39      private static final long TIMEOUT = 3000000000L;
40  
41      @Test
42      @Disabled("Need to investigate why it fails on osx")
43      public void testSuspendAndResumeAccept(TestInfo testInfo) throws Throwable {
44          run(testInfo, new Runner<ServerBootstrap>() {
45              @Override
46              public void run(ServerBootstrap serverBootstrap) throws Throwable {
47                  testSuspendAndResumeAccept(serverBootstrap);
48              }
49          });
50      }
51  
52      public void testSuspendAndResumeAccept(ServerBootstrap sb) throws Throwable {
53          AcceptedChannelCounter counter = new AcceptedChannelCounter(NUM_CHANNELS);
54  
55          sb.option(ChannelOption.SO_BACKLOG, 1);
56          sb.option(ChannelOption.AUTO_READ, false);
57          sb.childHandler(counter);
58  
59          Channel sc = sb.bind().sync().channel();
60  
61          List<Socket> sockets = new ArrayList<Socket>();
62  
63          try {
64              long startTime = System.nanoTime();
65              for (int i = 0; i < NUM_CHANNELS; i ++) {
66                  Socket s = new Socket();
67                  SocketUtils.connect(s, sc.localAddress(), 10000);
68                  sockets.add(s);
69              }
70  
71              sc.config().setAutoRead(true);
72  
73              counter.latch.await();
74  
75              long endTime = System.nanoTime();
76              assertTrue(endTime - startTime > TIMEOUT);
77          } finally {
78              for (Socket s: sockets) {
79                  s.close();
80              }
81          }
82  
83          Thread.sleep(TIMEOUT / 1000000);
84  
85          try {
86              long startTime = System.nanoTime();
87              for (int i = 0; i < NUM_CHANNELS; i ++) {
88                  Socket s = new Socket();
89                  s.connect(sc.localAddress(), 10000);
90                  sockets.add(s);
91              }
92              long endTime = System.nanoTime();
93  
94              assertTrue(endTime - startTime < TIMEOUT);
95          } finally {
96              for (Socket s: sockets) {
97                  s.close();
98              }
99          }
100     }
101 
102     @ChannelHandler.Sharable
103     private static final class AcceptedChannelCounter extends ChannelInboundHandlerAdapter {
104 
105         final CountDownLatch latch;
106 
107         AcceptedChannelCounter(int nChannels) {
108             latch = new CountDownLatch(nChannels);
109         }
110 
111         @Override
112         public void channelActive(ChannelHandlerContext ctx) throws Exception {
113             latch.countDown();
114         }
115     }
116 }