Skip to content

Commit 7a733b8

Browse files
committed
refactoring
1 parent bb0d14b commit 7a733b8

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

redisson/src/test/java/org/redisson/SimpleDnsServer.java

+20-14
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,35 @@
99

1010
import java.net.InetAddress;
1111
import java.net.InetSocketAddress;
12+
import java.util.concurrent.ThreadLocalRandom;
1213

1314
public class SimpleDnsServer {
1415

1516
private final EventLoopGroup group = new NioEventLoopGroup();
1617
private final Channel channel;
1718
private String ip = "127.0.0.1";
18-
private final int port = 55;
19+
private final int port;
1920

2021
public SimpleDnsServer() throws InterruptedException {
21-
Bootstrap bootstrap = new Bootstrap();
22-
bootstrap.group(group)
23-
.channel(NioDatagramChannel.class)
24-
.handler(new ChannelInitializer<>() {
25-
@Override
26-
protected void initChannel(Channel ch) throws Exception {
27-
ch.pipeline().addLast(new DatagramDnsQueryDecoder());
28-
ch.pipeline().addLast(new DatagramDnsResponseEncoder());
29-
ch.pipeline().addLast(new DnsMessageHandler());
30-
}
31-
});
22+
this(ThreadLocalRandom.current().nextInt(50, 1000));
23+
}
24+
25+
public SimpleDnsServer(int port) throws InterruptedException {
26+
Bootstrap bootstrap = new Bootstrap();
27+
bootstrap.group(group)
28+
.channel(NioDatagramChannel.class)
29+
.handler(new ChannelInitializer<>() {
30+
@Override
31+
protected void initChannel(Channel ch) throws Exception {
32+
ch.pipeline().addLast(new DatagramDnsQueryDecoder());
33+
ch.pipeline().addLast(new DatagramDnsResponseEncoder());
34+
ch.pipeline().addLast(new DnsMessageHandler());
35+
}
36+
});
3237

33-
ChannelFuture future = bootstrap.bind(port).sync();
34-
channel = future.channel();
38+
this.port = port;
39+
ChannelFuture future = bootstrap.bind(port).sync();
40+
channel = future.channel();
3541
}
3642

3743
public InetSocketAddress getAddr() {

0 commit comments

Comments
 (0)