Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@
public class AbstractScheduledServiceTest extends TestCase {

volatile Scheduler configuration = newFixedDelaySchedule(0, 10, MILLISECONDS);
volatile @Nullable ScheduledFuture<?> future = null;
volatile @Nullable ScheduledFuture<?> future;

volatile boolean atFixedRateCalled = false;
volatile boolean withFixedDelayCalled = false;
volatile boolean scheduleCalled = false;
volatile boolean atFixedRateCalled;
volatile boolean withFixedDelayCalled;
volatile boolean scheduleCalled;

final ScheduledExecutorService executor =
new ScheduledThreadPoolExecutor(10) {
Expand All @@ -83,7 +83,7 @@ public void testServiceStartStop() throws Exception {
assertTrue(future.isCancelled());
}

private class NullService extends AbstractScheduledService {
private final class NullService extends AbstractScheduledService {
@Override
protected void runOneIteration() throws Exception {}

Expand All @@ -98,6 +98,18 @@ protected ScheduledExecutorService executor() {
}
}

private static final class NullAbstractService extends AbstractService {
@Override
protected void doStart() {
notifyStarted();
}

@Override
protected void doStop() {
notifyStopped();
}
}

public void testFailOnExceptionFromRun() throws Exception {
TestService service = new TestService();
service.runException = new Exception();
Expand Down Expand Up @@ -295,18 +307,18 @@ protected String serviceName() {
.isEqualTo("Timed out waiting for Foo [STARTING] to reach the RUNNING state.");
}

private class TestService extends AbstractScheduledService {
private final class TestService extends AbstractScheduledService {
final CyclicBarrier runFirstBarrier = new CyclicBarrier(2);
final CyclicBarrier runSecondBarrier = new CyclicBarrier(2);

volatile boolean startUpCalled = false;
volatile boolean shutDownCalled = false;
volatile boolean startUpCalled;
volatile boolean shutDownCalled;
final AtomicInteger numberOfTimesRunCalled = new AtomicInteger(0);
final AtomicInteger numberOfTimesExecutorCalled = new AtomicInteger(0);
final AtomicInteger numberOfTimesSchedulerCalled = new AtomicInteger(0);
volatile @Nullable Exception runException = null;
volatile @Nullable Exception startUpException = null;
volatile @Nullable Exception shutDownException = null;
volatile @Nullable Exception runException;
volatile @Nullable Exception startUpException;
volatile @Nullable Exception shutDownException;

@Override
protected void runOneIteration() throws Exception {
Expand Down Expand Up @@ -371,7 +383,7 @@ protected Scheduler scheduler() {
public void run() {}
};

boolean called = false;
boolean called;

private void assertSingleCallWithCorrectParameters(
Runnable command, long initialDelay, long delay, TimeUnit unit) {
Expand All @@ -387,7 +399,7 @@ public void testFixedRateSchedule() {
Scheduler schedule = Scheduler.newFixedRateSchedule(INITIAL_DELAY, DELAY, UNIT);
Cancellable unused =
schedule.schedule(
null,
new NullAbstractService(),
new ScheduledThreadPoolExecutor(1) {
@Override
public ScheduledFuture<?> scheduleAtFixedRate(
Expand All @@ -404,7 +416,7 @@ public void testFixedDelaySchedule() {
Scheduler schedule = newFixedDelaySchedule(INITIAL_DELAY, DELAY, UNIT);
Cancellable unused =
schedule.schedule(
null,
new NullAbstractService(),
new ScheduledThreadPoolExecutor(10) {
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(
Expand Down Expand Up @@ -472,8 +484,8 @@ protected Schedule getNextSchedule() throws Exception {
service.awaitTerminated();
}

private static class TestCustomScheduler extends AbstractScheduledService.CustomScheduler {
private final AtomicInteger scheduleCounter = new AtomicInteger(0);
private static final class TestCustomScheduler extends AbstractScheduledService.CustomScheduler {
final AtomicInteger scheduleCounter = new AtomicInteger(0);

@Override
protected Schedule getNextSchedule() throws Exception {
Expand All @@ -498,7 +510,8 @@ public void testCustomSchedule_startStop() throws Exception {
}
};
TestCustomScheduler scheduler = new TestCustomScheduler();
Cancellable future = scheduler.schedule(null, newScheduledThreadPool(10), task);
Cancellable future =
scheduler.schedule(new NullAbstractService(), newScheduledThreadPool(10), task);
firstBarrier.await();
assertEquals(1, scheduler.scheduleCounter.get());
secondBarrier.await();
Expand Down Expand Up @@ -627,7 +640,7 @@ public void testCustomSchedulerFailure() throws Exception {
assertThat(service.state()).isEqualTo(State.FAILED);
}

private static class TestFailingCustomScheduledService extends AbstractScheduledService {
private static final class TestFailingCustomScheduledService extends AbstractScheduledService {
final AtomicInteger numIterations = new AtomicInteger(0);
final CyclicBarrier firstBarrier = new CyclicBarrier(2);
final CyclicBarrier secondBarrier = new CyclicBarrier(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public static Scheduler newFixedDelaySchedule(long initialDelay, long delay, Tim
checkArgument(delay > 0, "delay must be > 0, found %s", delay);
return new Scheduler() {
@Override
public Cancellable schedule(
Cancellable schedule(
AbstractService service, ScheduledExecutorService executor, Runnable task) {
return new FutureAsCancellable(
executor.scheduleWithFixedDelay(task, initialDelay, delay, unit));
Expand Down Expand Up @@ -185,7 +185,7 @@ public static Scheduler newFixedRateSchedule(long initialDelay, long period, Tim
checkArgument(period > 0, "period must be > 0, found %s", period);
return new Scheduler() {
@Override
public Cancellable schedule(
Cancellable schedule(
AbstractService service, ScheduledExecutorService executor, Runnable task) {
return new FutureAsCancellable(
executor.scheduleAtFixedRate(task, initialDelay, period, unit));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@
public class AbstractScheduledServiceTest extends TestCase {

volatile Scheduler configuration = newFixedDelaySchedule(0, 10, MILLISECONDS);
volatile @Nullable ScheduledFuture<?> future = null;
volatile @Nullable ScheduledFuture<?> future;

volatile boolean atFixedRateCalled = false;
volatile boolean withFixedDelayCalled = false;
volatile boolean scheduleCalled = false;
volatile boolean atFixedRateCalled;
volatile boolean withFixedDelayCalled;
volatile boolean scheduleCalled;

final ScheduledExecutorService executor =
new ScheduledThreadPoolExecutor(10) {
Expand All @@ -83,7 +83,7 @@ public void testServiceStartStop() throws Exception {
assertTrue(future.isCancelled());
}

private class NullService extends AbstractScheduledService {
private final class NullService extends AbstractScheduledService {
@Override
protected void runOneIteration() throws Exception {}

Expand All @@ -98,6 +98,18 @@ protected ScheduledExecutorService executor() {
}
}

private static final class NullAbstractService extends AbstractService {
@Override
protected void doStart() {
notifyStarted();
}

@Override
protected void doStop() {
notifyStopped();
}
}

public void testFailOnExceptionFromRun() throws Exception {
TestService service = new TestService();
service.runException = new Exception();
Expand Down Expand Up @@ -295,18 +307,18 @@ protected String serviceName() {
.isEqualTo("Timed out waiting for Foo [STARTING] to reach the RUNNING state.");
}

private class TestService extends AbstractScheduledService {
private final class TestService extends AbstractScheduledService {
final CyclicBarrier runFirstBarrier = new CyclicBarrier(2);
final CyclicBarrier runSecondBarrier = new CyclicBarrier(2);

volatile boolean startUpCalled = false;
volatile boolean shutDownCalled = false;
volatile boolean startUpCalled;
volatile boolean shutDownCalled;
final AtomicInteger numberOfTimesRunCalled = new AtomicInteger(0);
final AtomicInteger numberOfTimesExecutorCalled = new AtomicInteger(0);
final AtomicInteger numberOfTimesSchedulerCalled = new AtomicInteger(0);
volatile @Nullable Exception runException = null;
volatile @Nullable Exception startUpException = null;
volatile @Nullable Exception shutDownException = null;
volatile @Nullable Exception runException;
volatile @Nullable Exception startUpException;
volatile @Nullable Exception shutDownException;

@Override
protected void runOneIteration() throws Exception {
Expand Down Expand Up @@ -371,7 +383,7 @@ protected Scheduler scheduler() {
public void run() {}
};

boolean called = false;
boolean called;

private void assertSingleCallWithCorrectParameters(
Runnable command, long initialDelay, long delay, TimeUnit unit) {
Expand All @@ -387,7 +399,7 @@ public void testFixedRateSchedule() {
Scheduler schedule = Scheduler.newFixedRateSchedule(INITIAL_DELAY, DELAY, UNIT);
Cancellable unused =
schedule.schedule(
null,
new NullAbstractService(),
new ScheduledThreadPoolExecutor(1) {
@Override
public ScheduledFuture<?> scheduleAtFixedRate(
Expand All @@ -404,7 +416,7 @@ public void testFixedDelaySchedule() {
Scheduler schedule = newFixedDelaySchedule(INITIAL_DELAY, DELAY, UNIT);
Cancellable unused =
schedule.schedule(
null,
new NullAbstractService(),
new ScheduledThreadPoolExecutor(10) {
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(
Expand Down Expand Up @@ -472,8 +484,8 @@ protected Schedule getNextSchedule() throws Exception {
service.awaitTerminated();
}

private static class TestCustomScheduler extends AbstractScheduledService.CustomScheduler {
private final AtomicInteger scheduleCounter = new AtomicInteger(0);
private static final class TestCustomScheduler extends AbstractScheduledService.CustomScheduler {
final AtomicInteger scheduleCounter = new AtomicInteger(0);

@Override
protected Schedule getNextSchedule() throws Exception {
Expand All @@ -498,7 +510,8 @@ public void testCustomSchedule_startStop() throws Exception {
}
};
TestCustomScheduler scheduler = new TestCustomScheduler();
Cancellable future = scheduler.schedule(null, newScheduledThreadPool(10), task);
Cancellable future =
scheduler.schedule(new NullAbstractService(), newScheduledThreadPool(10), task);
firstBarrier.await();
assertEquals(1, scheduler.scheduleCounter.get());
secondBarrier.await();
Expand Down Expand Up @@ -627,7 +640,7 @@ public void testCustomSchedulerFailure() throws Exception {
assertThat(service.state()).isEqualTo(State.FAILED);
}

private static class TestFailingCustomScheduledService extends AbstractScheduledService {
private static final class TestFailingCustomScheduledService extends AbstractScheduledService {
final AtomicInteger numIterations = new AtomicInteger(0);
final CyclicBarrier firstBarrier = new CyclicBarrier(2);
final CyclicBarrier secondBarrier = new CyclicBarrier(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public static Scheduler newFixedDelaySchedule(long initialDelay, long delay, Tim
checkArgument(delay > 0, "delay must be > 0, found %s", delay);
return new Scheduler() {
@Override
public Cancellable schedule(
Cancellable schedule(
AbstractService service, ScheduledExecutorService executor, Runnable task) {
return new FutureAsCancellable(
executor.scheduleWithFixedDelay(task, initialDelay, delay, unit));
Expand Down Expand Up @@ -183,7 +183,7 @@ public static Scheduler newFixedRateSchedule(long initialDelay, long period, Tim
checkArgument(period > 0, "period must be > 0, found %s", period);
return new Scheduler() {
@Override
public Cancellable schedule(
Cancellable schedule(
AbstractService service, ScheduledExecutorService executor, Runnable task) {
return new FutureAsCancellable(
executor.scheduleAtFixedRate(task, initialDelay, period, unit));
Expand Down
Loading