Fix SocketStringEchoTest
This commit is contained in:
parent
7b8024373d
commit
7f96221fe9
@ -16,43 +16,30 @@
|
||||
package io.netty.testsuite.transport.socket;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import io.netty.bootstrap.ClientBootstrap;
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFactory;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelStateEvent;
|
||||
import io.netty.channel.ExceptionEvent;
|
||||
import io.netty.channel.MessageEvent;
|
||||
import io.netty.channel.SimpleChannelUpstreamHandler;
|
||||
import io.netty.channel.ChannelInboundHandlerContext;
|
||||
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
|
||||
import io.netty.handler.codec.Delimiters;
|
||||
import io.netty.handler.codec.string.StringDecoder;
|
||||
import io.netty.handler.codec.string.StringEncoder;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import io.netty.util.SocketAddresses;
|
||||
import io.netty.util.internal.ExecutorUtil;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public abstract class AbstractSocketStringEchoTest {
|
||||
public class SocketStringEchoTest extends AbstractSocketTest {
|
||||
|
||||
static final Random random = new Random();
|
||||
static final String[] data = new String[1024];
|
||||
|
||||
private static ExecutorService executor;
|
||||
|
||||
static {
|
||||
for (int i = 0; i < data.length; i ++) {
|
||||
int eLen = random.nextInt(512);
|
||||
@ -65,48 +52,37 @@ public abstract class AbstractSocketStringEchoTest {
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
executor = Executors.newCachedThreadPool();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void destroy() {
|
||||
ExecutorUtil.terminate(executor);
|
||||
}
|
||||
|
||||
protected abstract ChannelFactory newServerSocketChannelFactory(Executor executor);
|
||||
protected abstract ChannelFactory newClientSocketChannelFactory(Executor executor);
|
||||
|
||||
@Test
|
||||
public void testStringEcho() throws Throwable {
|
||||
ServerBootstrap sb = new ServerBootstrap(newServerSocketChannelFactory(executor));
|
||||
ClientBootstrap cb = new ClientBootstrap(newClientSocketChannelFactory(executor));
|
||||
|
||||
EchoHandler sh = new EchoHandler();
|
||||
EchoHandler ch = new EchoHandler();
|
||||
|
||||
sb.pipeline().addLast("framer", new DelimiterBasedFrameDecoder(512, Delimiters.lineDelimiter()));
|
||||
sb.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.ISO_8859_1));
|
||||
sb.pipeline().addBefore("decoder", "encoder", new StringEncoder(CharsetUtil.ISO_8859_1));
|
||||
sb.pipeline().addAfter("decoder", "handler", sh);
|
||||
|
||||
cb.pipeline().addLast("framer", new DelimiterBasedFrameDecoder(512, Delimiters.lineDelimiter()));
|
||||
cb.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.ISO_8859_1));
|
||||
cb.pipeline().addBefore("decoder", "encoder", new StringEncoder(CharsetUtil.ISO_8859_1));
|
||||
cb.pipeline().addAfter("decoder", "handler", ch);
|
||||
|
||||
Channel sc = sb.bind(new InetSocketAddress(0));
|
||||
int port = ((InetSocketAddress) sc.getLocalAddress()).getPort();
|
||||
|
||||
ChannelFuture ccf = cb.connect(new InetSocketAddress(SocketAddresses.LOCALHOST, port));
|
||||
boolean success = ccf.awaitUninterruptibly().isSuccess();
|
||||
if (!success) {
|
||||
ccf.cause().printStackTrace();
|
||||
run();
|
||||
}
|
||||
assertTrue(success);
|
||||
|
||||
Channel cc = ccf.channel();
|
||||
public void testStringEcho(ServerBootstrap sb, Bootstrap cb) throws Throwable {
|
||||
final EchoHandler sh = new EchoHandler();
|
||||
final EchoHandler ch = new EchoHandler();
|
||||
|
||||
sb.childHandler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
public void initChannel(SocketChannel sch) throws Exception {
|
||||
sch.pipeline().addLast("framer", new DelimiterBasedFrameDecoder(512, Delimiters.lineDelimiter()));
|
||||
sch.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.ISO_8859_1));
|
||||
sch.pipeline().addBefore("decoder", "encoder", new StringEncoder(CharsetUtil.ISO_8859_1));
|
||||
sch.pipeline().addAfter("decoder", "handler", sh);
|
||||
}
|
||||
});
|
||||
|
||||
cb.handler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
public void initChannel(SocketChannel sch) throws Exception {
|
||||
sch.pipeline().addLast("framer", new DelimiterBasedFrameDecoder(512, Delimiters.lineDelimiter()));
|
||||
sch.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.ISO_8859_1));
|
||||
sch.pipeline().addBefore("decoder", "encoder", new StringEncoder(CharsetUtil.ISO_8859_1));
|
||||
sch.pipeline().addAfter("decoder", "handler", ch);
|
||||
}
|
||||
});
|
||||
|
||||
Channel sc = sb.bind().sync().channel();
|
||||
Channel cc = cb.connect().sync().channel();
|
||||
for (String element : data) {
|
||||
String delimiter = random.nextBoolean() ? "\r\n" : "\n";
|
||||
cc.write(element + delimiter);
|
||||
@ -141,9 +117,9 @@ public abstract class AbstractSocketStringEchoTest {
|
||||
// Ignore.
|
||||
}
|
||||
}
|
||||
sh.channel.close().awaitUninterruptibly();
|
||||
ch.channel.close().awaitUninterruptibly();
|
||||
sc.close().awaitUninterruptibly();
|
||||
sh.channel.close().sync();
|
||||
ch.channel.close().sync();
|
||||
sc.close().sync();
|
||||
|
||||
if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
|
||||
throw sh.exception.get();
|
||||
@ -159,39 +135,36 @@ public abstract class AbstractSocketStringEchoTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static class EchoHandler extends SimpleChannelUpstreamHandler {
|
||||
private static class EchoHandler extends ChannelInboundMessageHandlerAdapter<String> {
|
||||
volatile Channel channel;
|
||||
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
|
||||
volatile int counter;
|
||||
|
||||
EchoHandler() {
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelInboundHandlerContext<String> ctx)
|
||||
throws Exception {
|
||||
channel = ctx.channel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
|
||||
throws Exception {
|
||||
channel = e.channel();
|
||||
}
|
||||
public void messageReceived(ChannelInboundHandlerContext<String> ctx,
|
||||
String msg) throws Exception {
|
||||
assertEquals(data[counter], msg);
|
||||
|
||||
@Override
|
||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
|
||||
throws Exception {
|
||||
String m = (String) e.getMessage();
|
||||
assertEquals(data[counter], m);
|
||||
|
||||
if (channel.getParent() != null) {
|
||||
if (channel.parent() != null) {
|
||||
String delimiter = random.nextBoolean() ? "\r\n" : "\n";
|
||||
channel.write(m + delimiter);
|
||||
channel.write(msg + delimiter);
|
||||
}
|
||||
|
||||
counter ++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
|
||||
throws Exception {
|
||||
if (exception.compareAndSet(null, e.cause())) {
|
||||
e.channel().close();
|
||||
public void exceptionCaught(ChannelInboundHandlerContext<String> ctx,
|
||||
Throwable cause) throws Exception {
|
||||
if (exception.compareAndSet(null, cause)) {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user