public void testDefaultExecutorIsShutdownWhenServiceIsStopped() throws Exception { final AtomicReference<ScheduledExecutorService> executor = Atomics.newReference(); AbstractScheduledService service = new AbstractScheduledService() { @Override protected void runOneIteration() throws Exception {} @Override protected ScheduledExecutorService executor() { executor.set(super.executor()); return executor.get(); } @Override protected Scheduler scheduler() { return newFixedDelaySchedule(0, 1, TimeUnit.MILLISECONDS); } }; service.startAsync(); assertFalse(service.executor().isShutdown()); service.awaitRunning(); service.stopAsync(); service.awaitTerminated(); assertTrue(executor.get().awaitTermination(100, TimeUnit.MILLISECONDS)); }
public void testDefaultExecutorIsShutdownWhenServiceFails() throws Exception { final AtomicReference<ScheduledExecutorService> executor = Atomics.newReference(); AbstractScheduledService service = new AbstractScheduledService() { @Override protected void startUp() throws Exception { throw new Exception("Failed"); } @Override protected void runOneIteration() throws Exception {} @Override protected ScheduledExecutorService executor() { executor.set(super.executor()); return executor.get(); } @Override protected Scheduler scheduler() { return newFixedDelaySchedule(0, 1, TimeUnit.MILLISECONDS); } }; try { service.startAsync().awaitRunning(); fail("Expected service to fail during startup"); } catch (IllegalStateException expected) {} assertTrue(executor.get().awaitTermination(100, TimeUnit.MILLISECONDS)); }
public void testTimeout() { // Create a service whose executor will never run its commands Service service = new AbstractScheduledService() { @Override protected Scheduler scheduler() { return Scheduler.newFixedDelaySchedule(0, 1, TimeUnit.NANOSECONDS); } @Override protected ScheduledExecutorService executor() { return TestingExecutors.noOpScheduledExecutor(); } @Override protected void runOneIteration() throws Exception {} @Override protected String serviceName() { return "Foo"; } }; try { service.startAsync().awaitRunning(1, TimeUnit.MILLISECONDS); fail("Expected timeout"); } catch (TimeoutException e) { assertThat(e).hasMessage("Timed out waiting for Foo [STARTING] to reach the RUNNING state."); } }
public void testFixedDelayScheduleFarFuturePotentiallyOverflowingScheduleIsNeverReached() throws Exception { TestAbstractScheduledCustomService service = new TestAbstractScheduledCustomService() { @Override protected Scheduler scheduler() { return newFixedDelaySchedule(Long.MAX_VALUE, Long.MAX_VALUE, SECONDS); } }; service.startAsync().awaitRunning(); try { service.firstBarrier.await(5, SECONDS); fail(); } catch (TimeoutException expected) { } assertEquals(0, service.numIterations.get()); service.stopAsync(); service.awaitTerminated(); }
public void testCustomSchedulerFarFuturePotentiallyOverflowingScheduleIsNeverReached() throws Exception { TestAbstractScheduledCustomService service = new TestAbstractScheduledCustomService() { @Override protected Scheduler scheduler() { return new AbstractScheduledService.CustomScheduler() { @Override protected Schedule getNextSchedule() throws Exception { return new Schedule(Long.MAX_VALUE, SECONDS); } }; } }; service.startAsync().awaitRunning(); try { service.firstBarrier.await(5, SECONDS); fail(); } catch (TimeoutException expected) { } assertEquals(0, service.numIterations.get()); service.stopAsync(); service.awaitTerminated(); }
public void testCustomScheduler_deadlock() throws InterruptedException, BrokenBarrierException { final CyclicBarrier inGetNextSchedule = new CyclicBarrier(2); // This will flakily deadlock, so run it multiple times to increase the flake likelihood for (int i = 0; i < 1000; i++) { Service service = new AbstractScheduledService() { @Override protected void runOneIteration() {} @Override protected Scheduler scheduler() { return new CustomScheduler() { @Override protected Schedule getNextSchedule() throws Exception { if (state() != State.STARTING) { inGetNextSchedule.await(); Thread.yield(); throw new RuntimeException("boom"); } return new Schedule(0, TimeUnit.NANOSECONDS); } }; } }; service.startAsync().awaitRunning(); inGetNextSchedule.await(); service.stopAsync(); } }
public void testBig() throws Exception { TestAbstractScheduledCustomService service = new TestAbstractScheduledCustomService() { @Override protected Scheduler scheduler() { return new AbstractScheduledService.CustomScheduler() { @Override protected Schedule getNextSchedule() throws Exception { // Explicitly yield to increase the probability of a pathological scheduling. Thread.yield(); return new Schedule(0, TimeUnit.SECONDS); } }; } }; service.useBarriers = false; service.startAsync().awaitRunning(); Thread.sleep(50); service.useBarriers = true; service.firstBarrier.await(); int numIterations = service.numIterations.get(); service.stopAsync(); service.secondBarrier.await(); service.awaitTerminated(); assertEquals(numIterations, service.numIterations.get()); }
public Scheduler newScheduler() { long initialDelay = initialDelay().isPresent() ? initialDelay().get().toMillis() : 0; if (delay().isPresent()) { return Scheduler.newFixedDelaySchedule( initialDelay, delay().get().toMillis(), TimeUnit.MILLISECONDS); } return Scheduler.newFixedRateSchedule( initialDelay, rate().get().toMillis(), TimeUnit.MILLISECONDS); }
public void testDefaultExecutorIsShutdownWhenServiceIsStopped() throws Exception { final AtomicReference<ScheduledExecutorService> executor = Atomics.newReference(); AbstractScheduledService service = new AbstractScheduledService() { @Override protected void runOneIteration() throws Exception {} @Override protected ScheduledExecutorService executor() { executor.set(super.executor()); return executor.get(); } @Override protected Scheduler scheduler() { return Scheduler.newFixedDelaySchedule(0, 1, TimeUnit.MILLISECONDS); } }; service.startAsync(); assertFalse(service.executor().isShutdown()); service.awaitRunning(); service.stopAsync(); service.awaitTerminated(); assertTrue(executor.get().awaitTermination(100, TimeUnit.MILLISECONDS)); }
public void testDefaultExecutorIsShutdownWhenServiceFails() throws Exception { final AtomicReference<ScheduledExecutorService> executor = Atomics.newReference(); AbstractScheduledService service = new AbstractScheduledService() { @Override protected void startUp() throws Exception { throw new Exception("Failed"); } @Override protected void runOneIteration() throws Exception {} @Override protected ScheduledExecutorService executor() { executor.set(super.executor()); return executor.get(); } @Override protected Scheduler scheduler() { return Scheduler.newFixedDelaySchedule(0, 1, TimeUnit.MILLISECONDS); } }; try { service.startAsync().awaitRunning(); fail("Expected service to fail during startup"); } catch (IllegalStateException expected) {} assertTrue(executor.get().awaitTermination(100, TimeUnit.MILLISECONDS)); }
@Override protected Scheduler scheduler() { return new CustomScheduler() { @Override protected Schedule getNextSchedule() throws Exception { return new Schedule(delay, unit); }}; }
@Override protected Scheduler scheduler() { return new CustomScheduler() { @Override protected Schedule getNextSchedule() throws Exception { if (numIterations.get() > 2) { throw new IllegalStateException("Failed"); } return new Schedule(delay, unit); }}; }
@Override protected void configure() { bind(TaskStatCalculator.class).in(Singleton.class); bind(CachedCounters.class).in(Singleton.class); bind(MachineResourceProvider.class).to(OfferAdapter.class); bind(SlotSizeCounter.class).in(Singleton.class); install(new PrivateModule() { @Override protected void configure() { bind(TaskStatUpdaterService.class).in(Singleton.class); bind(Scheduler.class).toInstance( Scheduler.newFixedRateSchedule( TASK_STAT_INTERVAL.get().getValue(), TASK_STAT_INTERVAL.get().getValue(), TASK_STAT_INTERVAL.get().getUnit().getTimeUnit())); expose(TaskStatUpdaterService.class); } }); SchedulerServicesModule.addSchedulerActiveServiceBinding(binder()) .to(TaskStatUpdaterService.class); install(new PrivateModule() { @Override protected void configure() { bind(SlotSizeCounterService.class).in(Singleton.class); bind(Scheduler.class).toInstance( Scheduler.newFixedRateSchedule( SLOT_STAT_INTERVAL.get().getValue(), SLOT_STAT_INTERVAL.get().getValue(), SLOT_STAT_INTERVAL.get().getUnit().getTimeUnit())); expose(SlotSizeCounterService.class); } }); SchedulerServicesModule.addSchedulerActiveServiceBinding(binder()) .to(SlotSizeCounterService.class); }
public void testDefaultExecutorIsShutdownWhenServiceFails() throws Exception { final AtomicReference<ScheduledExecutorService> executor = Atomics.newReference(); AbstractScheduledService service = new AbstractScheduledService() { @Override protected void startUp() throws Exception { throw new Exception("Failed"); } @Override protected void runOneIteration() throws Exception {} @Override protected ScheduledExecutorService executor() { executor.set(super.executor()); return executor.get(); } @Override protected Scheduler scheduler() { return newFixedDelaySchedule(0, 1, TimeUnit.MILLISECONDS); } }; try { service.startAsync().awaitRunning(); fail("Expected service to fail during startup"); } catch (IllegalStateException expected) { } assertTrue(executor.get().awaitTermination(100, TimeUnit.MILLISECONDS)); }
@Override protected Scheduler scheduler() { return new CustomScheduler() { @Override protected Schedule getNextSchedule() throws Exception { return new Schedule(delay, unit); } }; }
@Override protected Scheduler scheduler() { return new CustomScheduler() { @Override protected Schedule getNextSchedule() throws Exception { if (numIterations.get() > 2) { throw new IllegalStateException("Failed"); } return new Schedule(delay, unit); } }; }