Skip to content

Commit df15a53

Browse files
kimetavjovanov
authored andcommitted
Netty tests dynamic port assignment.
1 parent 3405ea2 commit df15a53

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

tests/src/io.netty/netty-common/4.1.115.Final/src/test/java/netty/NettyTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
import static io.netty.util.NetUtil.LOCALHOST;
7070

7171
public class NettyTests {
72-
private static final int PORT = 8080;
72+
private int port;
7373

7474
@Test
7575
void withSsl() throws Exception {
@@ -149,7 +149,7 @@ private void startClient(EventLoopGroup group, boolean ssl, boolean withAdaptive
149149
if (withAdaptive) {
150150
b.option(ChannelOption.ALLOCATOR, new AdaptiveByteBufAllocator());
151151
}
152-
Channel ch = b.connect("localhost", PORT).sync().channel();
152+
Channel ch = b.connect("localhost", port).sync().channel();
153153
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", Unpooled.EMPTY_BUFFER);
154154
request.headers().set(HttpHeaderNames.HOST, "localhost");
155155
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
@@ -171,7 +171,8 @@ private void startServer(EventLoopGroup bossGroup, EventLoopGroup workerGroup, b
171171
b.option(ChannelOption.ALLOCATOR, new AdaptiveByteBufAllocator())
172172
.childOption(ChannelOption.ALLOCATOR, new AdaptiveByteBufAllocator());
173173
}
174-
b.bind(PORT).sync();
174+
Channel channel = b.bind(0).sync().channel();
175+
this.port = ((NioServerSocketChannel) channel).localAddress().getPort();
175176
}
176177

177178
private static final class HttpClientInitializer extends ChannelInitializer<SocketChannel> {

tests/src/io.netty/netty-common/4.1.80.Final/src/test/java/netty/NettyTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
import static io.netty.util.NetUtil.LOCALHOST;
6868

6969
public class NettyTests {
70-
private static final int PORT = 8080;
70+
private int port = 8080;
7171

7272
@Test
7373
void withSsl() throws Exception {
@@ -139,7 +139,7 @@ private void startClient(EventLoopGroup group, boolean ssl, Consumer<Response> c
139139
}
140140
Bootstrap b = new Bootstrap();
141141
b.group(group).channel(NioSocketChannel.class).handler(new HttpClientInitializer(sslContext, callback));
142-
Channel ch = b.connect("localhost", PORT).sync().channel();
142+
Channel ch = b.connect("localhost", port).sync().channel();
143143
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", Unpooled.EMPTY_BUFFER);
144144
request.headers().set(HttpHeaderNames.HOST, "localhost");
145145
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
@@ -157,7 +157,8 @@ private void startServer(EventLoopGroup bossGroup, EventLoopGroup workerGroup, b
157157
.channel(NioServerSocketChannel.class)
158158
.handler(new LoggingHandler(LogLevel.INFO))
159159
.childHandler(new HttpServerInitializer(sslContext));
160-
b.bind(PORT).sync();
160+
Channel channel = b.bind(0).sync().channel();
161+
this.port = ((NioServerSocketChannel) channel).localAddress().getPort();
161162
}
162163

163164
private static final class HttpClientInitializer extends ChannelInitializer<SocketChannel> {

tests/src/io.netty/netty-transport/4.1.115.Final/src/test/java/netty/NettyTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import org.junit.jupiter.api.Test;
6060

6161
public class NettyTests {
62-
private static final int PORT = 8080;
62+
private int port;
6363

6464
@Test
6565
void withSsl() throws Exception {
@@ -101,7 +101,7 @@ private void startClient(EventLoopGroup group, boolean ssl, Consumer<Response> c
101101
}
102102
Bootstrap b = new Bootstrap();
103103
b.group(group).channel(NioSocketChannel.class).handler(new HttpClientInitializer(sslContext, callback));
104-
Channel ch = b.connect("localhost", PORT).sync().channel();
104+
Channel ch = b.connect("localhost", port).sync().channel();
105105
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", Unpooled.EMPTY_BUFFER);
106106
request.headers().set(HttpHeaderNames.HOST, "localhost");
107107
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
@@ -119,7 +119,8 @@ private void startServer(EventLoopGroup bossGroup, EventLoopGroup workerGroup, b
119119
.channel(NioServerSocketChannel.class)
120120
.handler(new LoggingHandler(LogLevel.INFO))
121121
.childHandler(new HttpServerInitializer(sslContext));
122-
b.bind(PORT).sync();
122+
Channel channel = b.bind(0).sync().channel();
123+
this.port = ((NioServerSocketChannel) channel).localAddress().getPort();
123124
}
124125

125126
private static final class HttpClientInitializer extends ChannelInitializer<SocketChannel> {

tests/src/io.netty/netty-transport/4.1.80.Final/src/test/java/netty/NettyTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import org.junit.jupiter.api.Test;
6060

6161
public class NettyTests {
62-
private static final int PORT = 8080;
62+
private int port;
6363

6464
@Test
6565
void withSsl() throws Exception {
@@ -101,7 +101,7 @@ private void startClient(EventLoopGroup group, boolean ssl, Consumer<Response> c
101101
}
102102
Bootstrap b = new Bootstrap();
103103
b.group(group).channel(NioSocketChannel.class).handler(new HttpClientInitializer(sslContext, callback));
104-
Channel ch = b.connect("localhost", PORT).sync().channel();
104+
Channel ch = b.connect("localhost", port).sync().channel();
105105
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/", Unpooled.EMPTY_BUFFER);
106106
request.headers().set(HttpHeaderNames.HOST, "localhost");
107107
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
@@ -119,7 +119,8 @@ private void startServer(EventLoopGroup bossGroup, EventLoopGroup workerGroup, b
119119
.channel(NioServerSocketChannel.class)
120120
.handler(new LoggingHandler(LogLevel.INFO))
121121
.childHandler(new HttpServerInitializer(sslContext));
122-
b.bind(PORT).sync();
122+
Channel channel = b.bind(0).sync().channel();
123+
this.port = ((NioServerSocketChannel) channel).localAddress().getPort();
123124
}
124125

125126
private static final class HttpClientInitializer extends ChannelInitializer<SocketChannel> {

0 commit comments

Comments
 (0)