Add test that we handle thread.interrupt() in NioEventLoop (#7917)

Motivation:

We added some code to guard against thread.interrupt() in NioEventLoop but did not added a test.

Modifications:

Add testcase.

Result:

Verify that we correctly handle interrupt().
This commit is contained in:
Norman Maurer 2018-05-09 08:57:53 +02:00 committed by GitHub
parent f01a590154
commit cbe9ed8cc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,6 +25,7 @@ import io.netty.util.concurrent.Future;
import org.junit.Test; import org.junit.Test;
import java.nio.channels.Selector; import java.nio.channels.Selector;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -42,7 +43,7 @@ public class NioEventLoopTest extends AbstractEventLoopTest {
} }
@Test @Test
public void testRebuildSelector() throws Exception { public void testRebuildSelector() {
EventLoopGroup group = new NioEventLoopGroup(1); EventLoopGroup group = new NioEventLoopGroup(1);
final NioEventLoop loop = (NioEventLoop) group.next(); final NioEventLoop loop = (NioEventLoop) group.next();
try { try {
@ -107,4 +108,47 @@ public class NioEventLoopTest extends AbstractEventLoopTest {
assertTrue(future.cancel(true)); assertTrue(future.cancel(true));
group.shutdownGracefully(); group.shutdownGracefully();
} }
@Test
public void testInterruptEventLoopThread() throws Exception {
EventLoopGroup group = new NioEventLoopGroup(1);
final NioEventLoop loop = (NioEventLoop) group.next();
try {
Selector selector = loop.unwrappedSelector();
assertTrue(selector.isOpen());
loop.submit(new Runnable() {
@Override
public void run() {
// Interrupt the thread which should not end-up in a busy spin and
// so the selector should not have been rebuild.
Thread.currentThread().interrupt();
}
}).syncUninterruptibly();
assertTrue(selector.isOpen());
final CountDownLatch latch = new CountDownLatch(2);
loop.submit(new Runnable() {
@Override
public void run() {
latch.countDown();
}
}).syncUninterruptibly();
loop.schedule(new Runnable() {
@Override
public void run() {
latch.countDown();
}
}, 2, TimeUnit.SECONDS).syncUninterruptibly();
latch.await();
assertSame(selector, loop.unwrappedSelector());
assertTrue(selector.isOpen());
} finally {
group.shutdownGracefully();
}
}
} }