Java 如何在Citrus中测试套接字服务器?
我想对用Netty构建的套接字服务器进行一些单元测试 套接字服务器具有以下简单代码:Java 如何在Citrus中测试套接字服务器?,java,sockets,junit,netty,citrus-framework,Java,Sockets,Junit,Netty,Citrus Framework,我想对用Netty构建的套接字服务器进行一些单元测试 套接字服务器具有以下简单代码: 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; im
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();
}
}
导入io.netty.bootstrap.ServerBootstrap;
导入io.netty.channel.ChannelFuture;
导入io.netty.channel.ChannelInitializer;
导入io.netty.channel.ChannelOption;
导入io.netty.channel.EventLoopGroup;
导入io.netty.channel.nio.NioEventLoopGroup;
导入io.netty.channel.socket.SocketChannel;
导入io.netty.channel.socket.nio.NioServerSocketChannel;
公共类SocketServer实现可运行{
专用int端口;
private EventLoopGroup bossGroup;
私有EventLoopGroup workerGroup;
私人渠道未来;
私有服务器引导引导;
公共SocketServer(int端口){
this.port=端口;
this.bossGroup=new NioEventLoopGroup();
this.workerGroup=new NioEventLoopGroup();
}
公共int getPort(){
返回端口;
}
@凌驾
公开募捐{
试一试{
bootstrap=newserverbootstrap();
独自创立
.group(bossGroup、workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(新的通道初始值设定项(){
@凌驾
公共频道(SocketChannel ch)
抛出异常{
ch.管道()
.addLast(新的ReceiveMessageServerHandler())
.addLast(新的ParseMessageServerHandler());
}
}).option(ChannelOption.SO_BACKLOG,128)。childOption(ChannelOption.SO_KEEPALIVE,true);
//绑定并开始接受传入连接。
channelFuture=bootstrap.bind(port.sync();
//等待服务器套接字关闭
channelFuture.channel().closeFuture().sync();
}捕捉(中断异常e){
e、 printStackTrace();
}最后{
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public void shutdown()引发InterruptedException{
channelFuture.channel().close();
}
}
在MessageHandler上,我首先接收由“\n”分隔的文本消息。我非常需要一个telnet客户端
我想测试我是否可以向服务器发送不同的消息,以及是否在一个时间范围内收到某些预期的响应
我尝试使用Citrus框架,但没有得到任何结果,因为它没有提供适当的纯文本协议(我尝试了Rest、Soap等,但它们对我没有好处)。我在参考文献2.4中找不到答案
在我发布问题之前,我已经解决了这个问题(我昨天写了这个问题,但直到今天我才发布……我找到了答案) 所以我最终可以通过柑橘框架和Spring集成来解决这个问题 在与一位同事阅读了本文之后,我能够使用Spring集成TCP适配器作为Citrus端点的通道(来自java.nio的SocketChannel) 在这里,您可以看到我使用的citrus-context.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:citrus="http://www.citrusframework.org/schema/config"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-ip="http://www.springframework.org/schema/integration/ip"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.citrusframework.org/schema/config http://www.citrusframework.org/schema/config/citrus-config.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd">
<citrus:channel-endpoint id="citrusServiceEndpoint"
channel-name="input" />
<int-ip:tcp-connection-factory id="client"
type="client" host="localhost" port="9123" single-use="true"
so-timeout="10000" using-nio="true" />
<int:channel id="input" />
<int-ip:tcp-outbound-channel-adapter
id="outboundClient" channel="input" connection-factory="client" />
</beans>
我希望这能帮助那些可能和我有同样问题的人。您也可以使用Citrus Apache Camel集成来解决这个问题。您需要camel模块和camel-netty()或camel-netty4()依赖项。然后,您可以直接使用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中的动态端点,其中端点配置是在测试运行时创建的。因此,实际上这里不需要额外的配置 使用此方法,我们将如何在检查接收到的消息时确定正确的端口号?我如何知道下面语句中$PORT的值?接收(“骆驼:净额4:tcp://localhost:$PORT”)。有效载荷(“消息1”);
@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");
}