a simple client server in junit

spaghetti zhebincong at 163.com
Fri Dec 4 22:52:13 EST 2009


i write two class, one is a junit testcase, the other one called ServerHandler is a implementation of SimpleChannelHandler,in the setUp method of the test case, i start a server, in a test method (testHandler), i connect to the server. and send a message, the code as following:
the test case:
<b>
    protected void setUp() throws Exception {
        ServerBootstrap bootstrap = new ServerBootstrap(
                new NioServerSocketChannelFactory(
                Executors.newCachedThreadPool(),
                Executors.newCachedThreadPool()));
        ChannelExceptionHandler handler = new ChannelExceptionHandler();
        ServerHandler serverHandler = new ServerHandler();
        ChannelPipeline pipeline = bootstrap.getPipeline();
        pipeline.addLast("serverHandler", serverHandler);
        bootstrap.bind(new InetSocketAddress(8080));
    }
     public void testHandler() {
        ChannelFactory factory =
                new NioClientSocketChannelFactory(
                Executors.newCachedThreadPool(),
                Executors.newCachedThreadPool());
        ClientBootstrap cb = new ClientBootstrap(factory);
        ChannelFuture future = cb.connect(new InetSocketAddress("localhost", 8080));
        Channel channel = future.awaitUninterruptibly().getChannel();
        if (future.isSuccess()) {
            System.out.println("begin write.................");
            future = channel.write("the message");
            System.out.println(future.isSuccess());
            System.out.println(future.isDone());
            System.out.println(future.getCause());
        }
    }
</b>
the ServerHandler:
<b>
        @Override
        public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
            System.out.println("connected........");
        }
        @Override
        public void messageReceived(ChannelHandlerContext ctx,
                MessageEvent e)
                throws Exception {
            DefaultExceptionEvent event =
                    new DefaultExceptionEvent(
                    ctx.getChannel(), new IllegalArgumentException("illegal argument"));
            System.out.println(e.getMessage() + "=======");
        }
    }
</b>
after runing, i see following print in the log:
"connected........" and "begin write...........", it verify that the server connection is successful and the client can write to the channel.
but following, there are two "false", it means the isSuccess and isDone are failed.
the getCause is null. so i could not see the message i sent to be printed as i hope. who can help me?
thanks

-- 
View this message in context: http://n2.nabble.com/a-simple-client-server-in-junit-tp4116390p4116390.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list