diff --git a/transport/src/test/java/io/netty/channel/AbstractChannelTest.java b/transport/src/test/java/io/netty/channel/AbstractChannelTest.java index 70fa0ac397..3a69155c1d 100644 --- a/transport/src/test/java/io/netty/channel/AbstractChannelTest.java +++ b/transport/src/test/java/io/netty/channel/AbstractChannelTest.java @@ -15,20 +15,14 @@ */ package io.netty.channel; -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.capture; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.createNiceMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.expectLastCall; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; - import java.net.SocketAddress; import org.easymock.Capture; +import org.easymock.IAnswer; import org.junit.Test; +import static org.easymock.EasyMock.*; + public class AbstractChannelTest { @Test @@ -56,43 +50,56 @@ public class AbstractChannelTest { @Test public void ensureSubsequentRegistrationDoesNotFireActive() throws Throwable { - EventLoop eventLoop = createNiceMock(EventLoop.class); - // This allows us to have a single-threaded test - expect(eventLoop.inEventLoop()).andReturn(true).anyTimes(); + final EventLoop eventLoop = createNiceMock(EventLoop.class); + // This allows us to have a single-threaded test + expect(eventLoop.inEventLoop()).andReturn(true).anyTimes(); + eventLoop.execute(anyObject(Runnable.class)); + expectLastCall().andAnswer(new IAnswer() { + @Override + public Object answer() throws Throwable { + ((Runnable) getCurrentArguments()[0]).run(); + return null; + } + }).once(); - TestChannel channel = new TestChannel(); - ChannelInboundHandler handler = createMock(ChannelInboundHandler.class); - handler.handlerAdded(anyObject(ChannelHandlerContext.class)); expectLastCall(); - Capture throwable = catchHandlerExceptions(handler); - handler.channelRegistered(anyObject(ChannelHandlerContext.class)); - expectLastCall().times(2); // Should register twice - handler.channelActive(anyObject(ChannelHandlerContext.class)); - expectLastCall().once(); // Should only fire active once - replay(handler, eventLoop); - channel.pipeline().addLast(handler); + final TestChannel channel = new TestChannel(); + ChannelInboundHandler handler = createMock(ChannelInboundHandler.class); + handler.handlerAdded(anyObject(ChannelHandlerContext.class)); expectLastCall(); + Capture throwable = catchHandlerExceptions(handler); + handler.channelRegistered(anyObject(ChannelHandlerContext.class)); + expectLastCall().times(2); // Should register twice + handler.channelActive(anyObject(ChannelHandlerContext.class)); + expectLastCall().once(); // Should only fire active once - registerChannel(eventLoop, channel); - channel.unsafe().deregister(new DefaultChannelPromise(channel)); - registerChannel(eventLoop, channel); + handler.channelUnregistered(anyObject(ChannelHandlerContext.class)); + expectLastCall().once(); // Should register twice - checkForHandlerException(throwable); - verify(handler); - } + replay(handler, eventLoop); + channel.pipeline().addLast(handler); - private void registerChannel(EventLoop eventLoop, Channel channel) throws Exception { + registerChannel(eventLoop, channel); + channel.unsafe().deregister(new DefaultChannelPromise(channel)); + + registerChannel(eventLoop, channel); + + checkForHandlerException(throwable); + verify(handler); + } + + private static void registerChannel(EventLoop eventLoop, Channel channel) throws Exception { DefaultChannelPromise future = new DefaultChannelPromise(channel); channel.unsafe().register(eventLoop, future); future.sync(); // Cause any exceptions to be thrown } - private Capture catchHandlerExceptions(ChannelInboundHandler handler) throws Exception { + private static Capture catchHandlerExceptions(ChannelInboundHandler handler) throws Exception { Capture throwable = new Capture(); handler.exceptionCaught(anyObject(ChannelHandlerContext.class), capture(throwable)); expectLastCall().anyTimes(); return throwable; } - private void checkForHandlerException(Capture throwable) throws Throwable { + private static void checkForHandlerException(Capture throwable) throws Throwable { if (throwable.hasCaptured()) { throw throwable.getValue(); }