19
19
20
20
import io .netty .bootstrap .Bootstrap ;
21
21
import io .netty .bootstrap .ServerBootstrap ;
22
+ import io .netty .buffer .AdaptiveByteBufAllocator ;
22
23
import io .netty .buffer .Unpooled ;
23
24
import io .netty .channel .Channel ;
24
25
import io .netty .channel .ChannelFutureListener ;
25
26
import io .netty .channel .ChannelHandlerContext ;
26
27
import io .netty .channel .ChannelInitializer ;
28
+ import io .netty .channel .ChannelOption ;
27
29
import io .netty .channel .ChannelPipeline ;
28
30
import io .netty .channel .EventLoopGroup ;
29
31
import io .netty .channel .SimpleChannelInboundHandler ;
@@ -71,12 +73,17 @@ public class NettyTests {
71
73
72
74
@ Test
73
75
void withSsl () throws Exception {
74
- test (true );
76
+ test (true , false );
75
77
}
76
78
77
79
@ Test
78
80
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 );
80
87
}
81
88
82
89
@ Test
@@ -109,13 +116,13 @@ void testNioServerSocketChannel() {
109
116
}
110
117
}
111
118
112
- private void test (boolean ssl ) throws Exception {
119
+ private void test (boolean ssl , boolean withAdaptive ) throws Exception {
113
120
EventLoopGroup bossGroup = new NioEventLoopGroup (1 );
114
121
EventLoopGroup workerGroup = new NioEventLoopGroup ();
115
122
try {
116
- startServer (bossGroup , workerGroup , ssl );
123
+ startServer (bossGroup , workerGroup , ssl , withAdaptive );
117
124
AtomicReference <Response > response = new AtomicReference <>();
118
- startClient (workerGroup , ssl , response ::set );
125
+ startClient (workerGroup , ssl , withAdaptive , response ::set );
119
126
Awaitility .await ().atMost (Duration .ofSeconds (5 ))
120
127
.untilAtomic (response , CoreMatchers .equalTo (new Response (200 , "HTTP/1.1" , "Hello World" )));
121
128
} finally {
@@ -132,13 +139,16 @@ private InputStream loadCert() {
132
139
return Objects .requireNonNull (NettyTests .class .getResourceAsStream ("/cert.pem" ), "/cert.pem not found" );
133
140
}
134
141
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 {
136
143
SslContext sslContext = null ;
137
144
if (ssl ) {
138
145
sslContext = SslContextBuilder .forClient ().trustManager (InsecureTrustManagerFactory .INSTANCE ).build ();
139
146
}
140
147
Bootstrap b = new Bootstrap ();
141
148
b .group (group ).channel (NioSocketChannel .class ).handler (new HttpClientInitializer (sslContext , callback ));
149
+ if (withAdaptive ) {
150
+ b .option (ChannelOption .ALLOCATOR , new AdaptiveByteBufAllocator ());
151
+ }
142
152
Channel ch = b .connect ("localhost" , PORT ).sync ().channel ();
143
153
HttpRequest request = new DefaultFullHttpRequest (HttpVersion .HTTP_1_1 , HttpMethod .GET , "/" , Unpooled .EMPTY_BUFFER );
144
154
request .headers ().set (HttpHeaderNames .HOST , "localhost" );
@@ -147,7 +157,7 @@ private void startClient(EventLoopGroup group, boolean ssl, Consumer<Response> c
147
157
ch .closeFuture ().sync ();
148
158
}
149
159
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 {
151
161
SslContext sslContext = null ;
152
162
if (ssl ) {
153
163
sslContext = SslContextBuilder .forServer (loadCert (), loadKey (), null ).build ();
@@ -157,6 +167,10 @@ private void startServer(EventLoopGroup bossGroup, EventLoopGroup workerGroup, b
157
167
.channel (NioServerSocketChannel .class )
158
168
.handler (new LoggingHandler (LogLevel .INFO ))
159
169
.childHandler (new HttpServerInitializer (sslContext ));
170
+ if (withAdaptive ) {
171
+ b .option (ChannelOption .ALLOCATOR , new AdaptiveByteBufAllocator ())
172
+ .childOption (ChannelOption .ALLOCATOR , new AdaptiveByteBufAllocator ());
173
+ }
160
174
b .bind (PORT ).sync ();
161
175
}
162
176
0 commit comments