Java 类com.google.common.util.concurrent.AbstractScheduledService.Scheduler 实例源码

项目:guava-mock    文件:AbstractScheduledServiceTest.java   
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));
}
项目:guava-mock    文件:AbstractScheduledServiceTest.java   
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));
}
项目:guava-mock    文件:AbstractScheduledServiceTest.java   
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.");
  }
}
项目:guava-mock    文件:AbstractScheduledServiceTest.java   
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();
}
项目:guava-mock    文件:AbstractScheduledServiceTest.java   
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();
}
项目:guava-mock    文件:AbstractScheduledServiceTest.java   
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();
  }
}
项目:guava-mock    文件:AbstractScheduledServiceTest.java   
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());
}
项目:googles-monorepo-demo    文件:AbstractScheduledServiceTest.java   
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));
}
项目:googles-monorepo-demo    文件:AbstractScheduledServiceTest.java   
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));
}
项目:googles-monorepo-demo    文件:AbstractScheduledServiceTest.java   
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.");
  }
}
项目:googles-monorepo-demo    文件:AbstractScheduledServiceTest.java   
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();
}
项目:googles-monorepo-demo    文件:AbstractScheduledServiceTest.java   
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();
}
项目:googles-monorepo-demo    文件:AbstractScheduledServiceTest.java   
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();
  }
}
项目:googles-monorepo-demo    文件:AbstractScheduledServiceTest.java   
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());
}
项目:miscellaneous    文件:ScheduleConfiguration.java   
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);
}
项目:guava-libraries    文件:AbstractScheduledServiceTest.java   
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));
}
项目:guava-libraries    文件:AbstractScheduledServiceTest.java   
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));
}
项目:guava-libraries    文件:AbstractScheduledServiceTest.java   
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());
}
项目:guava    文件:AbstractScheduledServiceTest.java   
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));
}
项目:guava    文件:AbstractScheduledServiceTest.java   
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();
}
项目:guava    文件:AbstractScheduledServiceTest.java   
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();
}
项目:guava    文件:AbstractScheduledServiceTest.java   
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());
}
项目:guava    文件:AbstractScheduledServiceTest.java   
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));
}
项目:guava    文件:AbstractScheduledServiceTest.java   
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();
}
项目:guava    文件:AbstractScheduledServiceTest.java   
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();
}
项目:guava    文件:AbstractScheduledServiceTest.java   
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());
}
项目:guava-mock    文件:AbstractScheduledServiceTest.java   
@Override protected Scheduler scheduler() {
  return new CustomScheduler() {
    @Override
    protected Schedule getNextSchedule() throws Exception {
      return new Schedule(delay, unit);
    }};
}
项目:guava-mock    文件:AbstractScheduledServiceTest.java   
@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);
    }};
}
项目:googles-monorepo-demo    文件:AbstractScheduledServiceTest.java   
@Override protected Scheduler scheduler() {
  return new CustomScheduler() {
    @Override
    protected Schedule getNextSchedule() throws Exception {
      return new Schedule(delay, unit);
    }};
}
项目:googles-monorepo-demo    文件:AbstractScheduledServiceTest.java   
@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);
    }};
}
项目:Mastering-Mesos    文件:AsyncStatsModule.java   
@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);
}
项目:guava-libraries    文件:AbstractScheduledServiceTest.java   
@Override protected Scheduler scheduler() {
  return new CustomScheduler() {
    @Override
    protected Schedule getNextSchedule() throws Exception {
      return new Schedule(delay, unit);
    }};
}
项目:guava-libraries    文件:AbstractScheduledServiceTest.java   
@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);
    }};
}
项目:guava    文件:AbstractScheduledServiceTest.java   
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));
}
项目:guava    文件:AbstractScheduledServiceTest.java   
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.");
  }
}
项目:guava    文件:AbstractScheduledServiceTest.java   
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();
  }
}
项目:guava    文件:AbstractScheduledServiceTest.java   
@Override
protected Scheduler scheduler() {
  return new CustomScheduler() {
    @Override
    protected Schedule getNextSchedule() throws Exception {
      return new Schedule(delay, unit);
    }
  };
}
项目:guava    文件:AbstractScheduledServiceTest.java   
@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);
    }
  };
}
项目:guava    文件:AbstractScheduledServiceTest.java   
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));
}
项目:guava    文件:AbstractScheduledServiceTest.java   
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.");
  }
}