我想对使用Netty构建的套接字服务器进行一些单元测试。如何在Citrus中测试套接字服务器?
套接字服务器配有简单的下面的代码:
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();
}
}
在我第一次收到由“\ n”分隔短信MessageHandlers。我非常需要一个telnet客户端。
我想测试一下,我可以发送不同的消息到服务器,并在一定的时间内收到某些预期的响应。我试过使用Citrus Framework,但是我没有得到任何结果,因为它没有提供正确的纯文本协议(我试过Rest,Soap等,但它们对我没有好处)。我在Citrus Reference 2.4中找不到答案。
Citrus 2.4 Reference - HTML Version
使用这种方法,在检查收到的消息时,我们将如何确定正确的端口号? 例如:我如何知道以下语句中$ PORT的值? (“camel:netty4:tcp:// localhost:$ PORT”).payload(“Message 1”); – mdewit