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