Repository: netty/netty
File: common/src/test/java/io/netty/util/HashedWheelTimerTest.java (Java)

Code before:
                }
            }, 5, TimeUnit.SECONDS);
        }
        Thread.sleep(1000L); // sleep for a second

        for (Timeout timeout : timerUnprocessed.stop()) {
            assertTrue(timeout.isCancelled(), "All unprocessed tasks should be canceled");
        }
    }

    @Test()
    public void cancelWillCallCallback() throws InterruptedException {
        final CountDownLatch latch = new CountDownLatch(1);
        final HashedWheelTimer timer = new HashedWheelTimer();
        final Timeout t1 = timer.newTimeout(new TimerTask() {
            @Override
            public void run(Timeout timeout) {
                fail();
            }

            @Override
            public void cancelled(Timeout timeout) {
                latch.countDown();
            }
        }, 90, TimeUnit.MILLISECONDS);

        assertEquals(1, timer.pendingTimeouts());
        t1.cancel();
        latch.await();

        assertEquals(0, timer.pendingTimeouts());
        timer.stop();
    }

    private static TimerTask createNoOpTimerTask() {
        return new TimerTask() {
            @Override
            public void run(final Timeout timeout) throws Exception {
            }
        };
    }

    private static TimerTask createCountDownLatchTimerTask(final CountDownLatch latch) {
        return new TimerTask() {
            @Override
            public void run(final Timeout timeout) throws Exception {
                latch.countDown();
            }
        };
    }
}

Review comment (suggestion):
I suspect this test could be flaky if you don't extend the timeout to something on the order of 5 seconds or more.

Code after:
                }
            }, 5, TimeUnit.SECONDS);
        }
        Thread.sleep(1000L); // sleep for a second

        for (Timeout timeout : timerUnprocessed.stop()) {
            assertTrue(timeout.isCancelled(), "All unprocessed tasks should be canceled");
        }
    }

    @Test
    @org.junit.jupiter.api.Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
    public void cancelWillCallCallback() throws InterruptedException {
        final CountDownLatch latch = new CountDownLatch(1);
        final HashedWheelTimer timer = new HashedWheelTimer();
        final Timeout t1 = timer.newTimeout(new TimerTask() {
            @Override
            public void run(Timeout timeout) {
                fail();
            }

            @Override
            public void cancelled(Timeout timeout) {
                latch.countDown();
            }
        }, 90, TimeUnit.MILLISECONDS);

        assertEquals(1, timer.pendingTimeouts());
        t1.cancel();
        latch.await();

        assertEquals(0, timer.pendingTimeouts());
        timer.stop();
    }

    private static TimerTask createNoOpTimerTask() {
        return new TimerTask() {
            @Override
            public void run(final Timeout timeout) throws Exception {
            }
        };
    }

    private static TimerTask createCountDownLatchTimerTask(final CountDownLatch latch) {
        return new TimerTask() {
            @Override
            public void run(final Timeout timeout) throws Exception {
                latch.countDown();
            }
        };
    }