Skip to content

Commit

Permalink
Add test that we handle thread.interrupt() in NioEventLoop (netty#7917)
Browse files Browse the repository at this point in the history
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().
  • Loading branch information
normanmaurer authored May 9, 2018
1 parent f01a590 commit cbe9ed8
Showing 1 changed file with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.Test;

import java.nio.channels.Selector;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.*;
Expand All @@ -42,7 +43,7 @@ protected Class<? extends ServerSocketChannel> newChannel() {
}

@Test
public void testRebuildSelector() throws Exception {
public void testRebuildSelector() {
EventLoopGroup group = new NioEventLoopGroup(1);
final NioEventLoop loop = (NioEventLoop) group.next();
try {
Expand Down Expand Up @@ -107,4 +108,47 @@ public void run() {
assertTrue(future.cancel(true));
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();
}
}
}

0 comments on commit cbe9ed8

Please sign in to comment.