Here's a canonical example of an "expected timeout" test that seems natural
and readable to me.
timeoutMillis() returns 12ms, adjusted by timeout factor.  Ldap might need
a bigger value.


    /**
     * timed await times out if not counted down before timeout
     */
    public void testAwaitTimeout() throws InterruptedException {
        final CountDownLatch l = new CountDownLatch(1);
        Thread t = newStartedThread(new CheckedRunnable() {
            public void realRun() throws InterruptedException {
                assertEquals(1, l.getCount());

                long startTime = System.nanoTime();
                assertFalse(l.await(timeoutMillis(), MILLISECONDS));
                assertTrue(millisElapsedSince(startTime) >=
timeoutMillis());

                assertEquals(1, l.getCount());
            }});

        awaitTermination(t);
        assertEquals(1, l.getCount());
    }

Reply via email to