|
18 | 18 | import java.util.concurrent.atomic.AtomicBoolean; |
19 | 19 | import java.util.concurrent.atomic.AtomicInteger; |
20 | 20 | import java.util.concurrent.atomic.AtomicReference; |
| 21 | +import java.util.stream.Stream; |
21 | 22 |
|
22 | 23 | import io.netty.channel.Channel; |
23 | 24 | import io.netty.channel.ChannelInboundHandlerAdapter; |
|
28 | 29 | import io.netty.channel.IoHandlerFactory; |
29 | 30 | import io.netty.channel.IoRegistration; |
30 | 31 | import io.netty.channel.local.LocalIoHandler; |
| 32 | +import io.netty.channel.epoll.Epoll; |
| 33 | +import io.netty.channel.epoll.EpollIoHandler; |
| 34 | +import io.netty.channel.epoll.EpollServerSocketChannel; |
31 | 35 |
|
32 | 36 | import org.junit.jupiter.api.Test; |
| 37 | +import org.junit.jupiter.params.ParameterizedTest; |
| 38 | +import org.junit.jupiter.params.provider.MethodSource; |
33 | 39 |
|
34 | 40 | import io.netty.bootstrap.ServerBootstrap; |
35 | 41 | import io.netty.channel.nio.NioIoHandler; |
|
50 | 56 |
|
51 | 57 | public class VirtualMultithreadIoEventLoopGroupTest { |
52 | 58 |
|
53 | | - @Test |
54 | | - void processHttpRequestWithVirtualThreadOnManualNettyEventLoop() throws InterruptedException { |
55 | | - var group = new VirtualMultithreadIoEventLoopGroup(1, NioIoHandler.newFactory()); |
| 59 | + // Transport enumeration to drive tests across available Netty transports. |
| 60 | + private enum Transport { |
| 61 | + NIO, EPOLL, LOCAL; |
| 62 | + |
| 63 | + boolean isLocal() { |
| 64 | + return this == LOCAL; |
| 65 | + } |
| 66 | + |
| 67 | + boolean isAvailable() { |
| 68 | + switch (this) { |
| 69 | + case NIO : |
| 70 | + return true; |
| 71 | + case EPOLL : |
| 72 | + return Epoll.isAvailable(); |
| 73 | + case LOCAL : |
| 74 | + return true; |
| 75 | + default : |
| 76 | + return false; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + IoHandlerFactory handlerFactory() { |
| 81 | + switch (this) { |
| 82 | + case NIO : |
| 83 | + return NioIoHandler.newFactory(); |
| 84 | + case EPOLL : |
| 85 | + return EpollIoHandler.newFactory(); |
| 86 | + case LOCAL : |
| 87 | + return LocalIoHandler.newFactory(); |
| 88 | + default : |
| 89 | + throw new IllegalStateException(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + Class<? extends io.netty.channel.ServerChannel> serverChannelClass() { |
| 94 | + switch (this) { |
| 95 | + case NIO : |
| 96 | + return NioServerSocketChannel.class; |
| 97 | + case EPOLL : |
| 98 | + return EpollServerSocketChannel.class; |
| 99 | + case LOCAL : |
| 100 | + throw new IllegalStateException( |
| 101 | + "LOCAL transport does not provide a ServerChannel class for real networking"); |
| 102 | + default : |
| 103 | + throw new IllegalStateException(); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static Stream<Transport> transportsForNetworking() { |
| 109 | + return Stream.of(Transport.values()).filter(t -> !t.isLocal() && t.isAvailable()); |
| 110 | + } |
| 111 | + |
| 112 | + private static Stream<Transport> transportsAllowLocal() { |
| 113 | + return Stream.of(Transport.values()).filter(Transport::isAvailable); |
| 114 | + } |
| 115 | + |
| 116 | + @ParameterizedTest |
| 117 | + @MethodSource("transportsForNetworking") |
| 118 | + void processHttpRequestWithVirtualThreadOnManualNettyEventLoop(Transport transport) throws InterruptedException { |
| 119 | + assumeTrue(transport.isAvailable()); |
| 120 | + // avoid LOCAL for real networking tests |
| 121 | + assumeTrue(!transport.isLocal()); |
| 122 | + var group = new VirtualMultithreadIoEventLoopGroup(1, transport.handlerFactory()); |
56 | 123 | // create a simple http request server |
57 | 124 | InetSocketAddress inetAddress = new InetSocketAddress(8080); |
58 | 125 | CountDownLatch sendResponse = new CountDownLatch(1); |
59 | | - var bootstrap = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class) |
| 126 | + var bootstrap = new ServerBootstrap().group(group).channel(transport.serverChannelClass()) |
60 | 127 | .childHandler(new ChannelInitializer<SocketChannel>() { |
61 | 128 |
|
62 | 129 | @Override |
@@ -114,16 +181,19 @@ void virtualEventExecutorGroupCorrectlySetEventExecutor() throws ExecutionExcept |
114 | 181 | group.shutdownGracefully(); |
115 | 182 | } |
116 | 183 |
|
117 | | - @Test |
118 | | - void busyYieldMakeEveryoneToProgress() throws InterruptedException { |
119 | | - var group = new VirtualMultithreadIoEventLoopGroup(1, NioIoHandler.newFactory()); |
| 184 | + @ParameterizedTest |
| 185 | + @MethodSource("transportsForNetworking") |
| 186 | + void busyYieldMakeEveryoneToProgress(Transport transport) throws InterruptedException { |
| 187 | + assumeTrue(transport.isAvailable()); |
| 188 | + assumeTrue(!transport.isLocal()); |
| 189 | + var group = new VirtualMultithreadIoEventLoopGroup(1, transport.handlerFactory()); |
120 | 190 | // create a simple http request server |
121 | 191 | InetSocketAddress inetAddress = new InetSocketAddress(8080); |
122 | 192 | CountDownLatch sendResponse = new CountDownLatch(1); |
123 | 193 | AtomicBoolean secondVThreadHasDone = new AtomicBoolean(false); |
124 | 194 | AtomicInteger yields = new AtomicInteger(); |
125 | 195 | CyclicBarrier bothDone = new CyclicBarrier(2); |
126 | | - var bootstrap = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class) |
| 196 | + var bootstrap = new ServerBootstrap().group(group).channel(transport.serverChannelClass()) |
127 | 197 | .childHandler(new ChannelInitializer<SocketChannel>() { |
128 | 198 |
|
129 | 199 | @Override |
|
0 commit comments