Fix SocketStringEchoTest
This commit is contained in:
parent
7b8024373d
commit
7f96221fe9
@ -16,43 +16,30 @@
|
|||||||
package io.netty.testsuite.transport.socket;
|
package io.netty.testsuite.transport.socket;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
import io.netty.bootstrap.Bootstrap;
|
||||||
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.ServerBootstrap;
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
import io.netty.channel.ChannelFactory;
|
import io.netty.channel.ChannelInboundHandlerContext;
|
||||||
import io.netty.channel.ChannelFuture;
|
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelInitializer;
|
||||||
import io.netty.channel.ChannelStateEvent;
|
import io.netty.channel.socket.SocketChannel;
|
||||||
import io.netty.channel.ExceptionEvent;
|
|
||||||
import io.netty.channel.MessageEvent;
|
|
||||||
import io.netty.channel.SimpleChannelUpstreamHandler;
|
|
||||||
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
|
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
|
||||||
import io.netty.handler.codec.Delimiters;
|
import io.netty.handler.codec.Delimiters;
|
||||||
import io.netty.handler.codec.string.StringDecoder;
|
import io.netty.handler.codec.string.StringDecoder;
|
||||||
import io.netty.handler.codec.string.StringEncoder;
|
import io.netty.handler.codec.string.StringEncoder;
|
||||||
import io.netty.util.CharsetUtil;
|
import io.netty.util.CharsetUtil;
|
||||||
import io.netty.util.SocketAddresses;
|
|
||||||
import io.netty.util.internal.ExecutorUtil;
|
import java.io.IOException;
|
||||||
import org.junit.AfterClass;
|
import java.util.Random;
|
||||||
import org.junit.BeforeClass;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public abstract class AbstractSocketStringEchoTest {
|
public class SocketStringEchoTest extends AbstractSocketTest {
|
||||||
|
|
||||||
static final Random random = new Random();
|
static final Random random = new Random();
|
||||||
static final String[] data = new String[1024];
|
static final String[] data = new String[1024];
|
||||||
|
|
||||||
private static ExecutorService executor;
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
for (int i = 0; i < data.length; i ++) {
|
for (int i = 0; i < data.length; i ++) {
|
||||||
int eLen = random.nextInt(512);
|
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
|
@Test
|
||||||
public void testStringEcho() throws Throwable {
|
public void testStringEcho() throws Throwable {
|
||||||
ServerBootstrap sb = new ServerBootstrap(newServerSocketChannelFactory(executor));
|
run();
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
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) {
|
for (String element : data) {
|
||||||
String delimiter = random.nextBoolean() ? "\r\n" : "\n";
|
String delimiter = random.nextBoolean() ? "\r\n" : "\n";
|
||||||
cc.write(element + delimiter);
|
cc.write(element + delimiter);
|
||||||
@ -141,9 +117,9 @@ public abstract class AbstractSocketStringEchoTest {
|
|||||||
// Ignore.
|
// Ignore.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sh.channel.close().awaitUninterruptibly();
|
sh.channel.close().sync();
|
||||||
ch.channel.close().awaitUninterruptibly();
|
ch.channel.close().sync();
|
||||||
sc.close().awaitUninterruptibly();
|
sc.close().sync();
|
||||||
|
|
||||||
if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
|
if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
|
||||||
throw sh.exception.get();
|
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;
|
volatile Channel channel;
|
||||||
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
|
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
|
||||||
volatile int counter;
|
volatile int counter;
|
||||||
|
|
||||||
EchoHandler() {
|
|
||||||
|
@Override
|
||||||
|
public void channelActive(ChannelInboundHandlerContext<String> ctx)
|
||||||
|
throws Exception {
|
||||||
|
channel = ctx.channel();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
|
public void messageReceived(ChannelInboundHandlerContext<String> ctx,
|
||||||
throws Exception {
|
String msg) throws Exception {
|
||||||
channel = e.channel();
|
assertEquals(data[counter], msg);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
if (channel.parent() != null) {
|
||||||
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
|
|
||||||
throws Exception {
|
|
||||||
String m = (String) e.getMessage();
|
|
||||||
assertEquals(data[counter], m);
|
|
||||||
|
|
||||||
if (channel.getParent() != null) {
|
|
||||||
String delimiter = random.nextBoolean() ? "\r\n" : "\n";
|
String delimiter = random.nextBoolean() ? "\r\n" : "\n";
|
||||||
channel.write(m + delimiter);
|
channel.write(msg + delimiter);
|
||||||
}
|
}
|
||||||
|
|
||||||
counter ++;
|
counter ++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
|
public void exceptionCaught(ChannelInboundHandlerContext<String> ctx,
|
||||||
throws Exception {
|
Throwable cause) throws Exception {
|
||||||
if (exception.compareAndSet(null, e.cause())) {
|
if (exception.compareAndSet(null, cause)) {
|
||||||
e.channel().close();
|
ctx.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user