View Javadoc
1   /*
2    * Copyright 2019 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;
17  
18  import io.netty5.bootstrap.ServerBootstrap;
19  import io.netty5.channel.Channel;
20  import io.netty5.channel.ChannelHandler;
21  import io.netty5.channel.EventLoop;
22  import io.netty5.channel.EventLoopGroup;
23  import io.netty5.channel.IoHandlerFactory;
24  import io.netty5.channel.MultithreadEventLoopGroup;
25  import io.netty5.channel.ServerChannel;
26  import io.netty5.channel.local.LocalAddress;
27  import io.netty5.channel.local.LocalServerChannel;
28  import io.netty5.util.concurrent.EventExecutor;
29  import io.netty5.util.concurrent.Future;
30  import org.junit.jupiter.api.Test;
31  import org.junit.jupiter.api.Timeout;
32  
33  import java.util.concurrent.CountDownLatch;
34  import java.util.concurrent.RejectedExecutionException;
35  import java.util.concurrent.TimeUnit;
36  
37  import static org.junit.jupiter.api.Assertions.assertFalse;
38  import static org.junit.jupiter.api.Assertions.assertTrue;
39  import static org.junit.jupiter.api.Assertions.fail;
40  
41  public abstract class AbstractSingleThreadEventLoopTest {
42  
43      @Test
44      public void shutdownBeforeStart() throws Exception {
45          EventLoopGroup group = new MultithreadEventLoopGroup(newIoHandlerFactory());
46          assertFalse(group.awaitTermination(2, TimeUnit.MILLISECONDS));
47          group.shutdownGracefully(0, 0, TimeUnit.MILLISECONDS);
48          assertTrue(group.awaitTermination(200, TimeUnit.MILLISECONDS));
49      }
50  
51      @Test
52      public void shutdownGracefullyZeroQuietBeforeStart() throws Exception {
53          EventLoopGroup group =  new MultithreadEventLoopGroup(newIoHandlerFactory());
54          assertTrue(group.shutdownGracefully(0L, 2L, TimeUnit.SECONDS).asStage().await(200, TimeUnit.MILLISECONDS));
55      }
56  
57      // Copied from AbstractEventLoopTest
58      @Test
59      @Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
60      public void testShutdownGracefullyNoQuietPeriod() throws Exception {
61          EventLoopGroup loop = new MultithreadEventLoopGroup(newIoHandlerFactory());
62          ServerBootstrap b = new ServerBootstrap();
63          b.group(loop)
64                  .channel(serverChannelClass())
65                  .childHandler(new ChannelHandler() { });
66  
67          // Not close the Channel to ensure the EventLoop is still shutdown in time.
68          Future<Channel> cf = serverChannelClass() == LocalServerChannel.class
69                  ? b.bind(new LocalAddress(getClass())) : b.bind(0);
70          cf.asStage().sync();
71  
72          Future<?> f = loop.shutdownGracefully(0, 1, TimeUnit.MINUTES);
73          assertTrue(loop.awaitTermination(600, TimeUnit.MILLISECONDS));
74          assertTrue(f.asStage().sync().isSuccess());
75          assertTrue(loop.isShutdown());
76          assertTrue(loop.isTerminated());
77      }
78  
79      @Test
80      public void shutdownGracefullyBeforeStart() throws Exception {
81          EventLoopGroup group = new MultithreadEventLoopGroup(newIoHandlerFactory());
82          assertTrue(group.shutdownGracefully(200L, 1000L, TimeUnit.MILLISECONDS).asStage()
83                          .await(500, TimeUnit.MILLISECONDS));
84      }
85  
86      @Test
87      public void gracefulShutdownAfterStart() throws Exception {
88          EventLoop loop = new MultithreadEventLoopGroup(newIoHandlerFactory()).next();
89          final CountDownLatch latch = new CountDownLatch(1);
90          loop.execute(latch::countDown);
91  
92          // Wait for the event loop thread to start.
93          latch.await();
94  
95          // Request the event loop thread to stop.
96          loop.shutdownGracefully(200L, 3000L, TimeUnit.MILLISECONDS);
97  
98          // Wait until the event loop is terminated.
99          assertTrue(loop.awaitTermination(500L, TimeUnit.MILLISECONDS));
100 
101         assertRejection(loop);
102     }
103 
104     private static final Runnable NOOP = () -> { };
105 
106     private static void assertRejection(EventExecutor loop) {
107         try {
108             loop.execute(NOOP);
109             fail("A task must be rejected after shutdown() is called.");
110         } catch (RejectedExecutionException e) {
111             // Expected
112         }
113     }
114 
115     @Test
116     public void testDoesNotSupportChannelType() {
117         EventLoopGroup group = new MultithreadEventLoopGroup(newIoHandlerFactory());
118         try {
119             assertFalse(group.isCompatible(Channel.class));
120         } finally {
121             group.shutdownGracefully();
122         }
123     }
124 
125     @Test
126     public void testDoesSupportChannelType() {
127         EventLoopGroup group = new MultithreadEventLoopGroup(newIoHandlerFactory());
128         try {
129             assertTrue(group.isCompatible(serverChannelClass()));
130         } finally {
131             group.shutdownGracefully();
132         }
133     }
134 
135     protected abstract IoHandlerFactory newIoHandlerFactory();
136     protected abstract Class<? extends ServerChannel> serverChannelClass();
137 }