Search in sources :

Example 1 with IntSupplier

use of io.netty.util.IntSupplier in project netty by netty.

the class NioEventLoopTest method testRebuildSelectorOnIOException.

@Test
public void testRebuildSelectorOnIOException() {
    SelectStrategyFactory selectStrategyFactory = new SelectStrategyFactory() {

        @Override
        public SelectStrategy newSelectStrategy() {
            return new SelectStrategy() {

                private boolean thrown;

                @Override
                public int calculateStrategy(IntSupplier selectSupplier, boolean hasTasks) throws Exception {
                    if (!thrown) {
                        thrown = true;
                        throw new IOException();
                    }
                    return -1;
                }
            };
        }
    };
    EventLoopGroup group = new NioEventLoopGroup(1, new DefaultThreadFactory("ioPool"), SelectorProvider.provider(), selectStrategyFactory);
    final NioEventLoop loop = (NioEventLoop) group.next();
    try {
        Channel channel = new NioServerSocketChannel();
        Selector selector = loop.unwrappedSelector();
        loop.register(channel).syncUninterruptibly();
        Selector newSelector = ((NioEventLoop) channel.eventLoop()).unwrappedSelector();
        assertTrue(newSelector.isOpen());
        assertNotSame(selector, newSelector);
        assertFalse(selector.isOpen());
        channel.close().syncUninterruptibly();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) DefaultSelectStrategyFactory(io.netty.channel.DefaultSelectStrategyFactory) SelectStrategyFactory(io.netty.channel.SelectStrategyFactory) EventLoopGroup(io.netty.channel.EventLoopGroup) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) IntSupplier(io.netty.util.IntSupplier) SelectStrategy(io.netty.channel.SelectStrategy) ServerSocketChannel(io.netty.channel.socket.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) IOException(java.io.IOException) Selector(java.nio.channels.Selector) AbstractEventLoopTest(io.netty.channel.AbstractEventLoopTest) Test(org.junit.jupiter.api.Test)

Aggregations

AbstractEventLoopTest (io.netty.channel.AbstractEventLoopTest)1 Channel (io.netty.channel.Channel)1 DefaultSelectStrategyFactory (io.netty.channel.DefaultSelectStrategyFactory)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 SelectStrategy (io.netty.channel.SelectStrategy)1 SelectStrategyFactory (io.netty.channel.SelectStrategyFactory)1 ServerSocketChannel (io.netty.channel.socket.ServerSocketChannel)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 IntSupplier (io.netty.util.IntSupplier)1 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)1 IOException (java.io.IOException)1 Selector (java.nio.channels.Selector)1 SocketChannel (java.nio.channels.SocketChannel)1 Test (org.junit.jupiter.api.Test)1