1919
2020import io .netty .bootstrap .Bootstrap ;
2121import io .netty .bootstrap .ServerBootstrap ;
22+ import io .netty .buffer .AdaptiveByteBufAllocator ;
2223import io .netty .buffer .Unpooled ;
2324import io .netty .channel .Channel ;
2425import io .netty .channel .ChannelFutureListener ;
2526import io .netty .channel .ChannelHandlerContext ;
2627import io .netty .channel .ChannelInitializer ;
28+ import io .netty .channel .ChannelOption ;
2729import io .netty .channel .ChannelPipeline ;
2830import io .netty .channel .EventLoopGroup ;
2931import io .netty .channel .SimpleChannelInboundHandler ;
@@ -71,12 +73,17 @@ public class NettyTests {
7173
7274 @ Test
7375 void withSsl () throws Exception {
74- test (true );
76+ test (true , false );
7577 }
7678
7779 @ Test
7880 public void noSsl () throws Exception {
79- test (false );
81+ test (false , false );
82+ }
83+
84+ @ Test
85+ public void withAdaptive () throws Exception {
86+ test (true , true );
8087 }
8188
8289 @ Test
@@ -109,13 +116,13 @@ void testNioServerSocketChannel() {
109116 }
110117 }
111118
112- private void test (boolean ssl ) throws Exception {
119+ private void test (boolean ssl , boolean withAdaptive ) throws Exception {
113120 EventLoopGroup bossGroup = new NioEventLoopGroup (1 );
114121 EventLoopGroup workerGroup = new NioEventLoopGroup ();
115122 try {
116- startServer (bossGroup , workerGroup , ssl );
123+ startServer (bossGroup , workerGroup , ssl , withAdaptive );
117124 AtomicReference <Response > response = new AtomicReference <>();
118- startClient (workerGroup , ssl , response ::set );
125+ startClient (workerGroup , ssl , withAdaptive , response ::set );
119126 Awaitility .await ().atMost (Duration .ofSeconds (5 ))
120127 .untilAtomic (response , CoreMatchers .equalTo (new Response (200 , "HTTP/1.1" , "Hello World" )));
121128 } finally {
@@ -132,13 +139,16 @@ private InputStream loadCert() {
132139 return Objects .requireNonNull (NettyTests .class .getResourceAsStream ("/cert.pem" ), "/cert.pem not found" );
133140 }
134141
135- private void startClient (EventLoopGroup group , boolean ssl , Consumer <Response > callback ) throws InterruptedException , SSLException {
142+ private void startClient (EventLoopGroup group , boolean ssl , boolean withAdaptive , Consumer <Response > callback ) throws InterruptedException , SSLException {
136143 SslContext sslContext = null ;
137144 if (ssl ) {
138145 sslContext = SslContextBuilder .forClient ().trustManager (InsecureTrustManagerFactory .INSTANCE ).build ();
139146 }
140147 Bootstrap b = new Bootstrap ();
141148 b .group (group ).channel (NioSocketChannel .class ).handler (new HttpClientInitializer (sslContext , callback ));
149+ if (withAdaptive ) {
150+ b .option (ChannelOption .ALLOCATOR , new AdaptiveByteBufAllocator ());
151+ }
142152 Channel ch = b .connect ("localhost" , PORT ).sync ().channel ();
143153 HttpRequest request = new DefaultFullHttpRequest (HttpVersion .HTTP_1_1 , HttpMethod .GET , "/" , Unpooled .EMPTY_BUFFER );
144154 request .headers ().set (HttpHeaderNames .HOST , "localhost" );
@@ -147,7 +157,7 @@ private void startClient(EventLoopGroup group, boolean ssl, Consumer<Response> c
147157 ch .closeFuture ().sync ();
148158 }
149159
150- private void startServer (EventLoopGroup bossGroup , EventLoopGroup workerGroup , boolean ssl ) throws InterruptedException , SSLException {
160+ private void startServer (EventLoopGroup bossGroup , EventLoopGroup workerGroup , boolean ssl , boolean withAdaptive ) throws InterruptedException , SSLException {
151161 SslContext sslContext = null ;
152162 if (ssl ) {
153163 sslContext = SslContextBuilder .forServer (loadCert (), loadKey (), null ).build ();
@@ -157,6 +167,10 @@ private void startServer(EventLoopGroup bossGroup, EventLoopGroup workerGroup, b
157167 .channel (NioServerSocketChannel .class )
158168 .handler (new LoggingHandler (LogLevel .INFO ))
159169 .childHandler (new HttpServerInitializer (sslContext ));
170+ if (withAdaptive ) {
171+ b .option (ChannelOption .ALLOCATOR , new AdaptiveByteBufAllocator ())
172+ .childOption (ChannelOption .ALLOCATOR , new AdaptiveByteBufAllocator ());
173+ }
160174 b .bind (PORT ).sync ();
161175 }
162176
0 commit comments