@Test public void testDecreaseWorkFactor() { executor.setWorkFactor(0.5); executor.setPendingNanos(5000); executor.setWorkFactor(0.3); assertEquals(5000, executor.getPendingNanos()); }
@Test public void testNegativePendingNanos() throws InterruptedException { executor.setWorkFactor(0.5); executor.setPendingNanos(-1000); assertEquals(-1000, executor.getPendingNanos()); // Note: we do not expect the delay time to be used because work time + pending delay is // negative. setWorkTime(500); executor.execute(NO_OP); // Sleep should not be called with negative pending nanos Mockito.verify(executor, Mockito.never()).sleep(Mockito.anyLong()); assertEquals(-1000 + 500, executor.getPendingNanos()); }
@Test public void testNegativePendingNanosGoesPositive() throws InterruptedException { executor.setWorkFactor(0.5); long startPendingNanos = -1000; executor.setPendingNanos(startPendingNanos); assertEquals(-1000, executor.getPendingNanos()); setWorkTime(1250); setActualSleepTime(1250 + startPendingNanos); executor.execute(NO_OP); verifySleepTime(1250 + startPendingNanos); assertEquals(0, executor.getPendingNanos()); }
@Test public void testOverflowOfSleepNanos() throws InterruptedException { executor.setWorkFactor(0.5); executor.setPendingNanos(Long.MAX_VALUE); assertEquals(Long.MAX_VALUE, executor.getPendingNanos()); // At a 50% work factor we'd expect work and sleep to match. As they don't, the function will // try to increment the pending sleep nanos, which could (but should not) result in overflow. setWorkTime(5000); setActualSleepTime(Long.MAX_VALUE); executor.execute(NO_OP); // Expect sleep nanos to be clamped to the maximum long value verifySleepTime(Long.MAX_VALUE); }