@Test
public void testRebuildSelector() throws Exception {

    EventLoopGroup group = new NioEventLoopGroup(1);
    final NioEventLoop loop = (NioEventLoop) group.next();
    try {
        Channel channel = new NioServerSocketChannel();
        loop.register(channel).syncUninterruptibly();
        Selector selector = loop.unwrappedSelector();
        assertSame(selector, ((NioEventLoop) channel.eventLoop()).unwrappedSelector());
        assertTrue(selector.isOpen());
        // Submit to the EventLoop so we are sure its really executed in a non-async manner.
        loop.submit(new Runnable() {
            @Override
            public void run() {
                loop.rebuildSelector();
            }
        }).syncUninterruptibly();
        Selector newSelector = ((NioEventLoop) channel.eventLoop()).unwrappedSelector();
        assertTrue(newSelector.isOpen());
        assertNotSame(selector, newSelector);
        assertFalse(selector.isOpen());
        channel.close().syncUninterruptibly();
    } finally {
        group.shutdownGracefully();
    }
}