我想对使用 Netty 构建的套接字服务器进行一些单元测试。
Socket Server 有以下简单的代码:
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class SocketServer implements Runnable {
private int port;
private EventLoopGroup bossGroup;
private EventLoopGroup workerGroup;
private ChannelFuture channelFuture;
private ServerBootstrap bootstrap;
public SocketServer(int port) {
this.port = port;
this.bossGroup = new NioEventLoopGroup();
this.workerGroup = new NioEventLoopGroup();
}
public int getPort() {
return port;
}
@Override
public void run() {
try {
bootstrap = new ServerBootstrap();
bootstrap
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch)
throws Exception {
ch.pipeline()
.addLast(new ReceiveMessageServerHandler())
.addLast(new ParseMessageServerHandler());
}
}).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
// Bind and start to accept incoming connections.
channelFuture = bootstrap.bind(port).sync();
// Wait until the server socket is closed
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public void shutdown() throws InterruptedException {
channelFuture.channel().close();
}
}
在 MessageHandlers 上,我首先会收到由 '\n' 分隔的文本消息。我非常需要一个 telnet 客户端。
我想测试我是否可以向服务器发送不同的消息,并且我是否会在某个时间范围内收到某些预期的响应。
我尝试使用 Citrus Framework,但无法获得任何结果,因为它没有提供适当的纯文本协议(我尝试过 Rest、Soap 等,但它们对我没有好处)。我在 Citrus Reference 2.4 中找不到答案。