NioEventLoopTest.testChannelsRegistered test failure (#10014)

Motivation:

NioEventLoopTest.testChannelsRegistered sometimes fails due a race which is related to how SelectionKey and Selector is implemented in the JDK. In the current implementation it will "lazy" remove SelectionKeys from the Set which means we may still have these included sometimes when we use size() to get the number of SelectionKeys.

Modifications:

Just retry to read the number of registered Channels if we still see 2

Result:

Fixes https://github.com/netty/netty/issues/9895
This commit is contained in:
Norman Maurer 2020-02-10 20:28:53 +01:00 committed by GitHub
parent 536b83a1a8
commit a6896eae43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -33,7 +33,6 @@ import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.RejectedExecutionHandlers;
import io.netty.util.concurrent.ThreadPerTaskExecutor;
import org.hamcrest.core.IsInstanceOf;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
@ -269,7 +268,7 @@ public class NioEventLoopTest extends AbstractEventLoopTest {
}
}
@Test
@Test(timeout = 3000L)
public void testChannelsRegistered() throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup(1);
final NioEventLoop loop = (NioEventLoop) group.next();
@ -285,7 +284,14 @@ public class NioEventLoopTest extends AbstractEventLoopTest {
assertEquals(2, registeredChannels(loop));
assertTrue(ch1.deregister().syncUninterruptibly().isSuccess());
assertEquals(1, registeredChannels(loop));
int registered;
// As SelectionKeys are removed in a lazy fashion in the JDK implementation we may need to query a few
// times before we see the right number of registered chanels.
while ((registered = registeredChannels(loop)) == 2) {
Thread.sleep(50);
}
assertEquals(1, registered);
} finally {
group.shutdownGracefully();
}