Skip to content

Commit f6c0d8d

Browse files
klueverGoogle Java Core Libraries
authored andcommitted
Minor cleanups to AbstractScheduledServiceTest.
RELNOTES=n/a PiperOrigin-RevId: 418053354
1 parent 5d6bbc5 commit f6c0d8d

File tree

2 files changed

+56
-52
lines changed

2 files changed

+56
-52
lines changed

android/guava-tests/test/com/google/common/util/concurrent/AbstractScheduledServiceTest.java

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static com.google.common.truth.Truth.assertThat;
2020
import static com.google.common.util.concurrent.AbstractScheduledService.Scheduler.newFixedDelaySchedule;
2121
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
22+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
23+
import static java.util.concurrent.TimeUnit.NANOSECONDS;
2224
import static java.util.concurrent.TimeUnit.SECONDS;
2325

2426
import com.google.common.util.concurrent.AbstractScheduledService.Cancellable;
@@ -50,7 +52,7 @@
5052

5153
public class AbstractScheduledServiceTest extends TestCase {
5254

53-
volatile Scheduler configuration = newFixedDelaySchedule(0, 10, TimeUnit.MILLISECONDS);
55+
volatile Scheduler configuration = newFixedDelaySchedule(0, 10, MILLISECONDS);
5456
volatile ScheduledFuture<?> future = null;
5557

5658
volatile boolean atFixedRateCalled = false;
@@ -113,7 +115,7 @@ public void testFailOnExceptionFromStartUp() {
113115
service.startAsync().awaitRunning();
114116
fail();
115117
} catch (IllegalStateException e) {
116-
assertEquals(service.startUpException, e.getCause());
118+
assertThat(e).hasCauseThat().isEqualTo(service.startUpException);
117119
}
118120
assertEquals(0, service.numberOfTimesRunCalled.get());
119121
assertEquals(Service.State.FAILED, service.state());
@@ -156,7 +158,7 @@ public void testFailOnExceptionFromShutDown() throws Exception {
156158
service.awaitTerminated();
157159
fail();
158160
} catch (IllegalStateException e) {
159-
assertEquals(service.shutDownException, e.getCause());
161+
assertThat(e).hasCauseThat().isEqualTo(service.shutDownException);
160162
}
161163
assertEquals(Service.State.FAILED, service.state());
162164
}
@@ -208,7 +210,7 @@ protected ScheduledExecutorService executor() {
208210

209211
@Override
210212
protected Scheduler scheduler() {
211-
return newFixedDelaySchedule(0, 1, TimeUnit.MILLISECONDS);
213+
return newFixedDelaySchedule(0, 1, MILLISECONDS);
212214
}
213215
};
214216

@@ -217,7 +219,7 @@ protected Scheduler scheduler() {
217219
service.awaitRunning();
218220
service.stopAsync();
219221
service.awaitTerminated();
220-
assertTrue(executor.get().awaitTermination(100, TimeUnit.MILLISECONDS));
222+
assertTrue(executor.get().awaitTermination(100, MILLISECONDS));
221223
}
222224

223225
public void testDefaultExecutorIsShutdownWhenServiceFails() throws Exception {
@@ -240,7 +242,7 @@ protected ScheduledExecutorService executor() {
240242

241243
@Override
242244
protected Scheduler scheduler() {
243-
return newFixedDelaySchedule(0, 1, TimeUnit.MILLISECONDS);
245+
return newFixedDelaySchedule(0, 1, MILLISECONDS);
244246
}
245247
};
246248

@@ -250,7 +252,7 @@ protected Scheduler scheduler() {
250252
} catch (IllegalStateException expected) {
251253
}
252254

253-
assertTrue(executor.get().awaitTermination(100, TimeUnit.MILLISECONDS));
255+
assertTrue(executor.get().awaitTermination(100, MILLISECONDS));
254256
}
255257

256258
public void testSchedulerOnlyCalledOnce() throws Exception {
@@ -277,7 +279,7 @@ public void testTimeout() {
277279
new AbstractScheduledService() {
278280
@Override
279281
protected Scheduler scheduler() {
280-
return Scheduler.newFixedDelaySchedule(0, 1, TimeUnit.NANOSECONDS);
282+
return Scheduler.newFixedDelaySchedule(0, 1, NANOSECONDS);
281283
}
282284

283285
@Override
@@ -294,7 +296,7 @@ protected String serviceName() {
294296
}
295297
};
296298
try {
297-
service.startAsync().awaitRunning(1, TimeUnit.MILLISECONDS);
299+
service.startAsync().awaitRunning(1, MILLISECONDS);
298300
fail("Expected timeout");
299301
} catch (TimeoutException e) {
300302
assertThat(e)
@@ -366,9 +368,9 @@ protected Scheduler scheduler() {
366368
public static class SchedulerTest extends TestCase {
367369
// These constants are arbitrary and just used to make sure that the correct method is called
368370
// with the correct parameters.
369-
private static final int initialDelay = 10;
370-
private static final int delay = 20;
371-
private static final TimeUnit unit = TimeUnit.MILLISECONDS;
371+
private static final int INITIAL_DELAY = 10;
372+
private static final int DELAY = 20;
373+
private static final TimeUnit UNIT = MILLISECONDS;
372374

373375
// Unique runnable object used for comparison.
374376
final Runnable testRunnable =
@@ -382,22 +384,22 @@ private void assertSingleCallWithCorrectParameters(
382384
Runnable command, long initialDelay, long delay, TimeUnit unit) {
383385
assertFalse(called); // only called once.
384386
called = true;
385-
assertEquals(SchedulerTest.initialDelay, initialDelay);
386-
assertEquals(SchedulerTest.delay, delay);
387-
assertEquals(SchedulerTest.unit, unit);
387+
assertEquals(INITIAL_DELAY, initialDelay);
388+
assertEquals(DELAY, delay);
389+
assertEquals(UNIT, unit);
388390
assertEquals(testRunnable, command);
389391
}
390392

391393
public void testFixedRateSchedule() {
392-
Scheduler schedule = Scheduler.newFixedRateSchedule(initialDelay, delay, unit);
394+
Scheduler schedule = Scheduler.newFixedRateSchedule(INITIAL_DELAY, DELAY, UNIT);
393395
Cancellable unused =
394396
schedule.schedule(
395397
null,
396398
new ScheduledThreadPoolExecutor(1) {
397399
@Override
398400
public ScheduledFuture<?> scheduleAtFixedRate(
399401
Runnable command, long initialDelay, long period, TimeUnit unit) {
400-
assertSingleCallWithCorrectParameters(command, initialDelay, delay, unit);
402+
assertSingleCallWithCorrectParameters(command, initialDelay, period, unit);
401403
return new ThrowingScheduledFuture<>();
402404
}
403405
},
@@ -406,7 +408,7 @@ public ScheduledFuture<?> scheduleAtFixedRate(
406408
}
407409

408410
public void testFixedDelaySchedule() {
409-
Scheduler schedule = newFixedDelaySchedule(initialDelay, delay, unit);
411+
Scheduler schedule = newFixedDelaySchedule(INITIAL_DELAY, DELAY, UNIT);
410412
Cancellable unused =
411413
schedule.schedule(
412414
null,
@@ -487,13 +489,13 @@ protected Schedule getNextSchedule() throws Exception {
487489
service.awaitTerminated();
488490
}
489491

490-
private class TestCustomScheduler extends AbstractScheduledService.CustomScheduler {
492+
private static class TestCustomScheduler extends AbstractScheduledService.CustomScheduler {
491493
public AtomicInteger scheduleCounter = new AtomicInteger(0);
492494

493495
@Override
494496
protected Schedule getNextSchedule() throws Exception {
495497
scheduleCounter.incrementAndGet();
496-
return new Schedule(0, TimeUnit.SECONDS);
498+
return new Schedule(0, SECONDS);
497499
}
498500
}
499501

@@ -538,7 +540,7 @@ public void testCustomSchedulerServiceStop() throws Exception {
538540
service.secondBarrier.await();
539541
service.awaitTerminated();
540542
// Sleep for a while just to ensure that our task wasn't called again.
541-
Thread.sleep(unit.toMillis(3 * delay));
543+
Thread.sleep(UNIT.toMillis(3 * DELAY));
542544
assertEquals(1, service.numIterations.get());
543545
}
544546

@@ -562,7 +564,7 @@ protected Schedule getNextSchedule() throws Exception {
562564
Thread.yield();
563565
throw new RuntimeException("boom");
564566
}
565-
return new Schedule(0, TimeUnit.NANOSECONDS);
567+
return new Schedule(0, NANOSECONDS);
566568
}
567569
};
568570
}
@@ -584,7 +586,7 @@ protected Scheduler scheduler() {
584586
protected Schedule getNextSchedule() throws Exception {
585587
// Explicitly yield to increase the probability of a pathological scheduling.
586588
Thread.yield();
587-
return new Schedule(0, TimeUnit.SECONDS);
589+
return new Schedule(0, SECONDS);
588590
}
589591
};
590592
}
@@ -627,7 +629,7 @@ protected Scheduler scheduler() {
627629
return new CustomScheduler() {
628630
@Override
629631
protected Schedule getNextSchedule() throws Exception {
630-
return new Schedule(delay, unit);
632+
return new Schedule(DELAY, UNIT);
631633
}
632634
};
633635
}
@@ -644,7 +646,7 @@ public void testCustomSchedulerFailure() throws Exception {
644646
}
645647
Thread.sleep(1000);
646648
try {
647-
service.stopAsync().awaitTerminated(100, TimeUnit.SECONDS);
649+
service.stopAsync().awaitTerminated(100, SECONDS);
648650
fail();
649651
} catch (IllegalStateException e) {
650652
assertEquals(State.FAILED, service.state());
@@ -677,7 +679,7 @@ protected Schedule getNextSchedule() throws Exception {
677679
if (numIterations.get() > 2) {
678680
throw new IllegalStateException("Failed");
679681
}
680-
return new Schedule(delay, unit);
682+
return new Schedule(DELAY, UNIT);
681683
}
682684
};
683685
}

0 commit comments

Comments
 (0)