2015-12-30 19 views
1

我想对使用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

回答

1

我能够解决之前,我可以发布问题的问题(我昨天写了这个问题,但我没有将它张贴直到今天......我找到了答案)。

所以我终于可以用Citrus Framework和Spring Integration来解决这个问题。

在与一位同事阅读后,我能够使用Spring Integration TCP适配器作为Citrus端点的通道(来自java.nio的SocketChannel)。

Spring Integration Reference - IP

在这里,您可以看到柑橘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> 

在我的柑橘试验我能够发送邮件,因为我需要:

import org.junit.AfterClass; 
import org.junit.BeforeClass; 
import org.junit.Test; 

import com.consol.citrus.TestCaseMetaInfo.Status; 
import com.consol.citrus.annotations.CitrusTest; 
import com.consol.citrus.dsl.junit.JUnit4CitrusTestDesigner; 

public class MyFirstTest extends JUnit4CitrusTestDesigner { 

    private static final int PORT = 9123; 
    private static SocketServer socketServer; 
    private static Thread socketThread; 

    @BeforeClass 
    public static void setUp() throws Exception { 
     socketServer = new SocketServer(PORT); 
     socketThread = new Thread(socketServer); 
     socketThread.start(); 
    } 

    @AfterClass 
    public static void tearDown() throws Exception { 
     socketServer.shutdown(); 
     socketThread.join(); 
    } 

    @Test 
    @CitrusTest(name = "sendSpringIntegrationMessageTest") 
    public void sendSpringIntegrationMessageTest() { 
     status(Status.DRAFT); 
     send("citrusServiceEndpoint").payload("Message 1"); 
     send("citrusServiceEndpoint").payload("Message 2"); 
    } 

} 

我希望这可以帮助任何可能与我有同样问题的人。

2

您也可以使用Citrus Apache Camel集成。您需要citrus-camel模块和camel-netty(http://camel.apache.org/netty.html)或camel-netty4(http://camel.apache.org/netty4.html)依赖项。然后就可以直接使用骆驼的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"); 
} 

这是利用,其中在测试运行时被创建的端点配置在柑橘动态端点。所以在这里实际上不需要额外的配置!

+0

使用这种方法,在检查收到的消息时,我们将如何确定正确的端口号? 例如:我如何知道以下语句中$ PORT的值? (“camel:netty4:tcp:// localhost:$ PORT”).payload(“Message 1”); – mdewit

相关问题