Sleeper { public void sleep() { if (this.duration == 0) { return; } TimeoutAccounter timeoutAccounter = TimeoutAccounter.getAccounter(this.duration); while (!timeoutAccounter.isTimeoutElapsed()) { try { Thread.sleep(timeoutAccounter.getRemainingTimeout()); } catch (InterruptedException e) { logger.debug("Interrupted while sleeping", e); } } } Sleeper(long duration, Logger logger); void sleep();  }
@Test public void test() throws InterruptedException { final Sleeper sleeper = new Sleeper(SLEEP_TIME, Logger.getLogger(Sleeper.class)); final long[] times = { 0, 0 }; Thread sleepyThread = new Thread() { @Override public void run() { times[0] = System.currentTimeMillis(); sleeper.sleep(); times[1] = System.currentTimeMillis(); } }; T t = new T(sleepyThread); Thread thread = new Thread(t); thread.setDaemon(true); thread.start(); sleepyThread.start(); sleepyThread.join(); assertTrue(times[1] - times[0] >= SLEEP_TIME - 100); thread.interrupt(); }