[#803] Make sure the right EventExecutor is used after re-register a Channel to another EventLoop

This commit is contained in:
Norman Maurer 2012-12-13 10:38:44 +01:00
parent 0c5fd38eb6
commit 9d42acbc2a
4 changed files with 145 additions and 4 deletions

View File

@ -56,7 +56,9 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
final int flags;
final AtomicBoolean readable = new AtomicBoolean(true);
EventExecutor executor; // not thread-safe but OK because it never changes once set.
// Will be set to null if no child executor should be used, otherwise it will be set to the
// child executor.
final EventExecutor executor;
private MessageBuf<Object> inMsgBuf;
private ByteBuf inByteBuf;
@ -261,8 +263,6 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
pipeline.childExecutors.put(group, childExecutor);
}
executor = childExecutor;
} else if (channel.isRegistered()) {
executor = channel.eventLoop();
} else {
executor = null;
}
@ -442,7 +442,7 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
@Override
public EventExecutor executor() {
if (executor == null) {
return executor = channel.eventLoop();
return channel.eventLoop();
} else {
return executor;
}

View File

@ -0,0 +1,74 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import org.junit.Test;
import java.net.InetSocketAddress;
import static org.junit.Assert.*;
public abstract class AbstractEventLoopTest {
/**
* Test for https://github.com/netty/netty/issues/803
*/
@Test
public void testReregister() {
EventLoopGroup group = newEventLoopGroup();
EventLoopGroup group2 = newEventLoopGroup();
final EventExecutorGroup eventExecutorGroup = new DefaultEventExecutorGroup(2);
ServerBootstrap bootstrap = new ServerBootstrap();
ChannelFuture future = bootstrap.channel(newChannel()).group(group)
.localAddress(new InetSocketAddress(0)).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
}
}).handler(new ChannelInitializer<ServerSocketChannel>() {
@Override
public void initChannel(ServerSocketChannel ch) throws Exception {
ch.pipeline().addLast(new TestChannelHandler());
ch.pipeline().addLast(eventExecutorGroup, new TestChannelHandler2());
}
})
.bind().awaitUninterruptibly();
EventExecutor executor = future.channel().pipeline().context(TestChannelHandler2.class).executor();
EventExecutor executor1 = future.channel().pipeline().context(TestChannelHandler.class).executor();
future.channel().deregister().awaitUninterruptibly();
Channel channel = group2.register(future.channel()).awaitUninterruptibly().channel();
EventExecutor executorNew = channel.pipeline().context(TestChannelHandler.class).executor();
assertNotSame(executor1, executorNew);
assertSame(executor, future.channel().pipeline().context(TestChannelHandler2.class).executor());
}
private static final class TestChannelHandler extends ChannelHandlerAdapter {
}
private static final class TestChannelHandler2 extends ChannelHandlerAdapter {
}
protected abstract EventLoopGroup newEventLoopGroup();
protected abstract Class<? extends ServerSocketChannel> newChannel();
}

View File

@ -0,0 +1,32 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel.socket.aio;
import io.netty.channel.AbstractEventLoopTest;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
public class AioEventLoopTest extends AbstractEventLoopTest {
@Override
protected EventLoopGroup newEventLoopGroup() {
return new AioEventLoopGroup();
}
@Override
protected Class<? extends ServerSocketChannel> newChannel() {
return AioServerSocketChannel.class;
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel.socket.nio;
import io.netty.channel.AbstractEventLoopTest;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class NioEventLoopTest extends AbstractEventLoopTest {
@Override
protected EventLoopGroup newEventLoopGroup() {
return new NioEventLoopGroup();
}
@Override
protected Class<? extends ServerSocketChannel> newChannel() {
return NioServerSocketChannel.class;
}
}