Be more lenient when check the number of registered channels (#11194)

Motivation:

Some SingleEventLoop implementations may need some "time" to update the number of registered Channels. We should loop until we see the correct value or timeout if we did not observe the correct value.

Modifications:

- Loop until we see the correct value
- add test timeout

Result:

Fixes https://github.com/netty/netty/issues/11169
This commit is contained in:
Norman Maurer 2021-04-26 11:31:09 +02:00 committed by GitHub
parent c1b922b224
commit 48e2c0d5cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -42,7 +42,7 @@ import io.netty.util.concurrent.Future;
public abstract class AbstractSingleThreadEventLoopTest { public abstract class AbstractSingleThreadEventLoopTest {
@Test @Test(timeout = 5000)
public void testChannelsRegistered() throws Exception { public void testChannelsRegistered() throws Exception {
EventLoopGroup group = newEventLoopGroup(); EventLoopGroup group = newEventLoopGroup();
final SingleThreadEventLoop loop = (SingleThreadEventLoop) group.next(); final SingleThreadEventLoop loop = (SingleThreadEventLoop) group.next();
@ -61,18 +61,25 @@ public abstract class AbstractSingleThreadEventLoopTest {
assertTrue(loop.register(ch1).syncUninterruptibly().isSuccess()); assertTrue(loop.register(ch1).syncUninterruptibly().isSuccess());
assertTrue(loop.register(ch2).syncUninterruptibly().isSuccess()); assertTrue(loop.register(ch2).syncUninterruptibly().isSuccess());
if (channelCountSupported) { if (channelCountSupported) {
assertEquals(2, registeredChannels(loop)); checkNumRegisteredChannels(loop, 2);
} }
assertTrue(ch1.deregister().syncUninterruptibly().isSuccess()); assertTrue(ch1.deregister().syncUninterruptibly().isSuccess());
if (channelCountSupported) { if (channelCountSupported) {
assertEquals(1, registeredChannels(loop)); checkNumRegisteredChannels(loop, 1);
} }
} finally { } finally {
group.shutdownGracefully(); group.shutdownGracefully();
} }
} }
private static void checkNumRegisteredChannels(SingleThreadEventLoop loop, int numChannels) throws Exception {
// We need to loop as some EventLoop implementations may need some time to update the counter correctly.
while (registeredChannels(loop) != numChannels) {
Thread.sleep(50);
}
}
// Only reliable if run from event loop // Only reliable if run from event loop
private static int registeredChannels(final SingleThreadEventLoop loop) throws Exception { private static int registeredChannels(final SingleThreadEventLoop loop) throws Exception {
return loop.submit(new Callable<Integer>() { return loop.submit(new Callable<Integer>() {