From 835a40fa251ccfd6da922d11bd4f74f90e1f4247 Mon Sep 17 00:00:00 2001 From: ursa Date: Thu, 14 Mar 2013 13:43:12 +0400 Subject: [PATCH] Port test for handler's life-cycle processing into master branch. --- .../channel/DefaultChannelPipelineTest.java | 91 ++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java b/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java index 0ccd27de53..d3a6f67d1e 100644 --- a/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java +++ b/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java @@ -25,7 +25,9 @@ import io.netty.channel.local.LocalChannel; import io.netty.channel.local.LocalEventLoopGroup; import org.junit.Test; -import java.net.SocketAddress; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -645,6 +647,39 @@ public class DefaultChannelPipelineTest { } + @Test + public void testLifeCycleAware() { + LocalChannel channel = new LocalChannel(); + LocalEventLoopGroup group = new LocalEventLoopGroup(); + group.register(channel).awaitUninterruptibly(); + final DefaultChannelPipeline pipeline = new DefaultChannelPipeline(channel); + + List handlers = new ArrayList(); + + for (int i = 0; i < 20; i++) { + LifeCycleAwareTestHandler handler = new LifeCycleAwareTestHandler("handler-" + i); + + // Add handler. + pipeline.addFirst(handler.name, handler); + + // Validate handler life-cycle methods called. + handler.validate(true, true, false, false); + + // Store handler into the list. + handlers.add(handler); + } + + // Change the order of remove operations over all handlers in the pipeline. + Collections.shuffle(handlers); + + for (LifeCycleAwareTestHandler handler : handlers) { + assertSame(handler, pipeline.remove(handler.name)); + + // Validate handler life-cycle methods called. + handler.validate(true, true, true, true); + } + } + @Test public void testRemoveAndForwardDuplexMessage() throws Exception { LocalChannel channel = new LocalChannel(); @@ -863,4 +898,58 @@ public class DefaultChannelPipelineTest { ((ChannelOutboundHandler) operationHandler()).freeOutboundBuffer(ctx); } } + + /** Test handler to validate life-cycle aware behavior. */ + private static final class LifeCycleAwareTestHandler extends ChannelHandlerAdapter { + private final String name; + + private boolean beforeAdd; + private boolean afterAdd; + private boolean beforeRemove; + private boolean afterRemove; + + /** + * Constructs life-cycle aware test handler. + * + * @param name Handler name to display in assertion messages. + */ + private LifeCycleAwareTestHandler(String name) { + this.name = name; + } + + public void validate(boolean beforeAdd, boolean afterAdd, boolean beforeRemove, boolean afterRemove) { + assertEquals(name, beforeAdd, this.beforeAdd); + assertEquals(name, afterAdd, this.afterAdd); + assertEquals(name, beforeRemove, this.beforeRemove); + assertEquals(name, afterRemove, this.afterRemove); + } + + @Override + public void beforeAdd(ChannelHandlerContext ctx) { + validate(false, false, false, false); + + beforeAdd = true; + } + + @Override + public void afterAdd(ChannelHandlerContext ctx) { + validate(true, false, false, false); + + afterAdd = true; + } + + @Override + public void beforeRemove(ChannelHandlerContext ctx) { + validate(true, true, false, false); + + beforeRemove = true; + } + + @Override + public void afterRemove(ChannelHandlerContext ctx) { + validate(true, true, true, false); + + afterRemove = true; + } + } }