ThreadExecutorTest.testInvokeAnyInterrupt1 random failure

kalinshi(施慧) kalinshi at tencent.com
Thu Aug 27 02:51:12 UTC 2020


Hi all,

Checking testInvokeAnyInterrupt1 test in ThreadExecutorTest.java it might have random failure in some executing orders. In most times execution performs in following order

1.     set kernel thread interrupted

2.     submit first task in AbstractExecutorService.doInvokeAny

3.     poll ExecutorCompletionService. completionQueue which is empty in most cases and return null

4.     submit second task in AbstractExecutorService.doInvokeAny loop

5.     poll again and still no task completed

6.     No task to submit and block kernel thread in ExecutorCompletionService.take, because kernel thread is interrupted, it throws InterruptedException

But in real world, task might finished before poll in step3/step5, then kernel thread has no chance to block in ExecutorCompletionService.take and return without InterruptedException.
Suggest task1 and task2 perform sleep long enough to ensure ExecutorCompletionService.poll never returns none null value in this test?

    public void testInvokeAnyInterrupt1() throws Exception {
        try (var executor = Executors.newVirtualThreadExecutor()) {
            Callable<String> task1 = () -> "foo";
            Callable<String> task2 = () -> "bar";
            Thread.currentThread().interrupt();
            try {
                executor.invokeAny(Set.of(task1, task2));
                assertTrue(false);
            } catch (InterruptedException expected) {
                assertFalse(Thread.currentThread().isInterrupted());
            } finally {
                Thread.interrupted(); // clear interrupt
            }
        }
}

Regards
Hui


More information about the loom-dev mailing list