@Before public void setUp() { clock = Mockito.mock(HighResolutionClock.class); executor = Mockito.spy(new ThrottlingExecutor(MAX_NANOS, clock)); }
@Test public void testInitialState() { ThrottlingExecutor throttler = new ThrottlingExecutor(MAX_NANOS); assertEquals(0, throttler.getPendingNanos()); assertEquals(1.0, throttler.getWorkFactor()); }
@Test(expected = IllegalArgumentException.class) public void testLessThan0PercentWorkRate() { new ThrottlingExecutor(MAX_NANOS).setWorkFactor(-0.1); }
@Test public void testInitialState() { ThrottlingExecutor throttler = new ThrottlingExecutor(MAX_NANOS); assertEquals(0, throttler.getPendingNanos()); assertEquals(1.0, throttler.getWorkFactor()); }
@Test(expected = IllegalArgumentException.class) public void testLessThan0PercentWorkRate() { new ThrottlingExecutor(MAX_NANOS).setWorkFactor(-0.1); }
@Test(expected = IllegalArgumentException.class) public void testGreaterThan100PercentWorkRate() { new ThrottlingExecutor(MAX_NANOS).setWorkFactor(1.1); }
@Test(expected = IllegalArgumentException.class) public void testGreaterThan100PercentWorkRate() { new ThrottlingExecutor(MAX_NANOS).setWorkFactor(1.1); }
@Test public void testClampDelayMillis() throws InterruptedException { final long maxDelayMillis = 10; final long maxDelayNanos = TimeUnit.MILLISECONDS.toNanos(maxDelayMillis); executor = Mockito.spy(new ThrottlingExecutor(maxDelayMillis, clock)); executor.setWorkFactor(0.5); // Note work time exceeds maxDelayMillis setWorkTime(TimeUnit.MILLISECONDS.toNanos(100)); setActualSleepTime(maxDelayNanos); executor.execute(NO_OP); verifySleepTime(maxDelayNanos); assertEquals(0L, executor.getPendingNanos()); }