作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想对使用 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,但我无法获得任何结果,因为它没有提供适当的纯文本协议(protocol)(我尝试过 Rest、Soap 等,但它们对我没有好处)。我在 Citrus Reference 2.4 中找不到答案。
最佳答案
您还可以为此使用 Citrus Apache Camel 集成。您需要 citrus-camel 模块和 camel-netty(http://camel.apache.org/netty.html)或 camel-netty4(http://camel.apache.org/netty4.html)依赖项。然后就可以直接使用Camel Netty组件发送消息了:
@Test
@CitrusTest
public void sendNettyMessageTest() {
status(Status.DRAFT);
//sends data to server
send("camel:netty4:tcp://localhost:9123").payload("Message 1");
send("camel:netty4:tcp://localhost:9123").payload("Message 2");
}
这是在 Citrus 中使用动态端点,其中端点配置是在测试运行时创建的。所以这里几乎不需要额外的配置!
关于java - 如何在 Citrus 中测试套接字服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34535159/
我是一名优秀的程序员,十分优秀!