Skip to content

Commit

Permalink
Port test for handler's life-cycle processing into master branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
ursaj authored and Norman Maurer committed Mar 14, 2013
1 parent 289d474 commit 835a40f
Showing 1 changed file with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
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;
Expand Down Expand Up @@ -645,6 +647,39 @@ public void run() {

}

@Test
public void testLifeCycleAware() {
LocalChannel channel = new LocalChannel();
LocalEventLoopGroup group = new LocalEventLoopGroup();
group.register(channel).awaitUninterruptibly();
final DefaultChannelPipeline pipeline = new DefaultChannelPipeline(channel);

List<LifeCycleAwareTestHandler> handlers = new ArrayList<LifeCycleAwareTestHandler>();

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();
Expand Down Expand Up @@ -863,4 +898,58 @@ public void freeOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
((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;
}
}
}

0 comments on commit 835a40f

Please sign in to comment.