|
| 1 | +package org.jooby.internal.netty; |
| 2 | + |
| 3 | +import static org.easymock.EasyMock.expect; |
| 4 | +import static org.easymock.EasyMock.isA; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.util.concurrent.ThreadFactory; |
| 8 | + |
| 9 | +import org.jooby.spi.HttpHandler; |
| 10 | +import org.jooby.spi.Server; |
| 11 | +import org.jooby.test.MockUnit; |
| 12 | +import org.jooby.test.MockUnit.Block; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.runner.RunWith; |
| 15 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 16 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 17 | + |
| 18 | +import com.typesafe.config.Config; |
| 19 | +import com.typesafe.config.ConfigFactory; |
| 20 | +import com.typesafe.config.ConfigValueFactory; |
| 21 | + |
| 22 | +import io.netty.bootstrap.ServerBootstrap; |
| 23 | +import io.netty.channel.Channel; |
| 24 | +import io.netty.channel.ChannelFuture; |
| 25 | +import io.netty.channel.ChannelOption; |
| 26 | +import io.netty.channel.EventLoopGroup; |
| 27 | +import io.netty.channel.ServerChannel; |
| 28 | +import io.netty.channel.epoll.Epoll; |
| 29 | +import io.netty.channel.epoll.EpollEventLoopGroup; |
| 30 | +import io.netty.channel.nio.NioEventLoopGroup; |
| 31 | +import io.netty.channel.socket.nio.NioServerSocketChannel; |
| 32 | +import io.netty.handler.logging.LogLevel; |
| 33 | +import io.netty.handler.logging.LoggingHandler; |
| 34 | +import io.netty.handler.ssl.SslContextBuilder; |
| 35 | +import io.netty.util.concurrent.DefaultEventExecutorGroup; |
| 36 | +import io.netty.util.concurrent.DefaultThreadFactory; |
| 37 | +import io.netty.util.concurrent.EventExecutorGroup; |
| 38 | +import io.netty.util.concurrent.Future; |
| 39 | + |
| 40 | +@RunWith(PowerMockRunner.class) |
| 41 | +@PrepareForTest({NettyServer.class, NioEventLoopGroup.class, DefaultThreadFactory.class, |
| 42 | + DefaultEventExecutorGroup.class, ServerBootstrap.class, SslContextBuilder.class, File.class, |
| 43 | + Epoll.class, EpollEventLoopGroup.class, NettySslContext.class }) |
| 44 | +public class Issue581 { |
| 45 | + |
| 46 | + Config config = ConfigFactory.empty() |
| 47 | + .withValue("server.http2.enabled", ConfigValueFactory.fromAnyRef(false)) |
| 48 | + .withValue("netty.threads.Boss", ConfigValueFactory.fromAnyRef(1)) |
| 49 | + .withValue("netty.threads.Worker", ConfigValueFactory.fromAnyRef(0)) |
| 50 | + .withValue("netty.threads.Max", ConfigValueFactory.fromAnyRef(2)) |
| 51 | + .withValue("netty.threads.Name", ConfigValueFactory.fromAnyRef("netty task")) |
| 52 | + .withValue("netty.options.SO_BACKLOG", ConfigValueFactory.fromAnyRef(1024)) |
| 53 | + .withValue("netty.worker.options.SO_REUSEADDR", ConfigValueFactory.fromAnyRef(true)) |
| 54 | + .withValue("netty.http.MaxContentLength", ConfigValueFactory.fromAnyRef("200k")) |
| 55 | + .withValue("netty.http.MaxInitialLineLength", ConfigValueFactory.fromAnyRef("4k")) |
| 56 | + .withValue("netty.http.MaxHeaderSize", ConfigValueFactory.fromAnyRef("8k")) |
| 57 | + .withValue("netty.http.MaxChunkSize", ConfigValueFactory.fromAnyRef("8k")) |
| 58 | + .withValue("netty.http.IdleTimeout", ConfigValueFactory.fromAnyRef("30s")) |
| 59 | + .withValue("netty.options.CONNECT_TIMEOUT_MILLIS", ConfigValueFactory.fromAnyRef(1000)) |
| 60 | + .withValue("application.port", ConfigValueFactory.fromAnyRef(6789)) |
| 61 | + .withValue("application.host", ConfigValueFactory.fromAnyRef("0.0.0.0")); |
| 62 | + |
| 63 | + @SuppressWarnings({"rawtypes", "unchecked" }) |
| 64 | + private MockUnit.Block parentEventLoop = unit -> { |
| 65 | + NioEventLoopGroup eventLoop = unit.constructor(NioEventLoopGroup.class) |
| 66 | + .args(int.class, ThreadFactory.class) |
| 67 | + .build(1, unit.get(ThreadFactory.class)); |
| 68 | + unit.registerMock(EventLoopGroup.class, eventLoop); |
| 69 | + unit.registerMock(NioEventLoopGroup.class, eventLoop); |
| 70 | + |
| 71 | + Future future = unit.mock(Future.class); |
| 72 | + expect(eventLoop.shutdownGracefully()).andReturn(future); |
| 73 | + expect(eventLoop.isShutdown()).andReturn(true); |
| 74 | + }; |
| 75 | + |
| 76 | + private Block taskThreadFactory = unit -> { |
| 77 | + DefaultThreadFactory factory = unit.constructor(DefaultThreadFactory.class) |
| 78 | + .args(String.class) |
| 79 | + .build("netty task"); |
| 80 | + unit.registerMock(DefaultThreadFactory.class, factory); |
| 81 | + }; |
| 82 | + |
| 83 | + private Block taskExecutor = unit -> { |
| 84 | + DefaultEventExecutorGroup executor = unit.constructor(DefaultEventExecutorGroup.class) |
| 85 | + .args(int.class, ThreadFactory.class) |
| 86 | + .build(2, unit.get(DefaultThreadFactory.class)); |
| 87 | + unit.registerMock(EventExecutorGroup.class, executor); |
| 88 | + }; |
| 89 | + |
| 90 | + private Block channel = unit -> { |
| 91 | + ChannelFuture cfuture = unit.mock(ChannelFuture.class); |
| 92 | + expect(cfuture.sync()).andReturn(cfuture); |
| 93 | + |
| 94 | + Channel channel = unit.mock(Channel.class); |
| 95 | + expect(channel.closeFuture()).andReturn(cfuture); |
| 96 | + unit.registerMock(Channel.class, channel); |
| 97 | + }; |
| 98 | + |
| 99 | + private Block noepoll = unit -> { |
| 100 | + unit.mockStatic(Epoll.class); |
| 101 | + expect(Epoll.isAvailable()).andReturn(false).times(1, 2); |
| 102 | + }; |
| 103 | + |
| 104 | + @SuppressWarnings({"rawtypes", "unchecked" }) |
| 105 | + @Test |
| 106 | + public void shouldShutdownExecutor() throws Exception { |
| 107 | + new MockUnit(HttpHandler.class) |
| 108 | + .expect(parentThreadFactory("nio-boss")) |
| 109 | + .expect(noepoll) |
| 110 | + .expect(parentEventLoop) |
| 111 | + .expect(taskThreadFactory) |
| 112 | + .expect(taskExecutor) |
| 113 | + .expect(channel) |
| 114 | + .expect(bootstrap(6789)) |
| 115 | + .expect(unit -> { |
| 116 | + EventExecutorGroup executor = unit.get(EventExecutorGroup.class); |
| 117 | + Future shutdownFuture = unit.mock(Future.class); |
| 118 | + expect(executor.shutdownGracefully()).andReturn(shutdownFuture); |
| 119 | + }) |
| 120 | + .run(unit -> { |
| 121 | + NettyServer server = new NettyServer(unit.get(HttpHandler.class), config); |
| 122 | + try { |
| 123 | + server.start(); |
| 124 | + server.join(); |
| 125 | + } finally { |
| 126 | + server.stop(); |
| 127 | + } |
| 128 | + }); |
| 129 | + } |
| 130 | + |
| 131 | + private Block parentThreadFactory(final String name) { |
| 132 | + return unit -> { |
| 133 | + DefaultThreadFactory factory = unit.constructor(DefaultThreadFactory.class) |
| 134 | + .args(String.class, int.class) |
| 135 | + .build(name, false); |
| 136 | + unit.registerMock(ThreadFactory.class, factory); |
| 137 | + }; |
| 138 | + } |
| 139 | + |
| 140 | + private Block bootstrap(final int port) { |
| 141 | + return bootstrap(port, NioEventLoopGroup.class, NioServerSocketChannel.class); |
| 142 | + } |
| 143 | + |
| 144 | + private Block bootstrap(final int port, final Class<? extends EventLoopGroup> eventLoop, |
| 145 | + final Class<? extends ServerChannel> channelClass) { |
| 146 | + return unit -> { |
| 147 | + ServerBootstrap bootstrap = unit.mockConstructor(ServerBootstrap.class); |
| 148 | + |
| 149 | + expect(bootstrap.group( |
| 150 | + unit.get(EventLoopGroup.class), unit.get(eventLoop))) |
| 151 | + .andReturn(bootstrap); |
| 152 | + |
| 153 | + LoggingHandler handler = unit.constructor(LoggingHandler.class) |
| 154 | + .args(Class.class, LogLevel.class) |
| 155 | + .build(Server.class, LogLevel.DEBUG); |
| 156 | + |
| 157 | + expect(bootstrap.channel(channelClass)).andReturn(bootstrap); |
| 158 | + expect(bootstrap.handler(handler)).andReturn(bootstrap); |
| 159 | + expect(bootstrap.childHandler(isA(NettyPipeline.class))).andReturn(bootstrap); |
| 160 | + |
| 161 | + expect(bootstrap.option(ChannelOption.SO_BACKLOG, 1024)).andReturn(bootstrap); |
| 162 | + expect(bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000)).andReturn(bootstrap); |
| 163 | + expect(bootstrap.childOption(ChannelOption.SO_REUSEADDR, true)).andReturn(bootstrap); |
| 164 | + |
| 165 | + ChannelFuture future = unit.mock(ChannelFuture.class); |
| 166 | + expect(future.sync()).andReturn(future); |
| 167 | + expect(future.channel()).andReturn(unit.get(Channel.class)); |
| 168 | + |
| 169 | + expect(bootstrap.bind("0.0.0.0", port)).andReturn(future); |
| 170 | + |
| 171 | + unit.registerMock(ServerBootstrap.class, bootstrap); |
| 172 | + }; |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments