19
19
import static com .google .common .truth .Truth .assertThat ;
20
20
import static com .google .common .util .concurrent .AbstractScheduledService .Scheduler .newFixedDelaySchedule ;
21
21
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 ;
22
24
import static java .util .concurrent .TimeUnit .SECONDS ;
23
25
24
26
import com .google .common .util .concurrent .AbstractScheduledService .Cancellable ;
50
52
51
53
public class AbstractScheduledServiceTest extends TestCase {
52
54
53
- volatile Scheduler configuration = newFixedDelaySchedule (0 , 10 , TimeUnit . MILLISECONDS );
55
+ volatile Scheduler configuration = newFixedDelaySchedule (0 , 10 , MILLISECONDS );
54
56
volatile ScheduledFuture <?> future = null ;
55
57
56
58
volatile boolean atFixedRateCalled = false ;
@@ -113,7 +115,7 @@ public void testFailOnExceptionFromStartUp() {
113
115
service .startAsync ().awaitRunning ();
114
116
fail ();
115
117
} catch (IllegalStateException e ) {
116
- assertEquals ( service . startUpException , e . getCause () );
118
+ assertThat ( e ). hasCauseThat (). isEqualTo ( service . startUpException );
117
119
}
118
120
assertEquals (0 , service .numberOfTimesRunCalled .get ());
119
121
assertEquals (Service .State .FAILED , service .state ());
@@ -156,7 +158,7 @@ public void testFailOnExceptionFromShutDown() throws Exception {
156
158
service .awaitTerminated ();
157
159
fail ();
158
160
} catch (IllegalStateException e ) {
159
- assertEquals ( service . shutDownException , e . getCause () );
161
+ assertThat ( e ). hasCauseThat (). isEqualTo ( service . shutDownException );
160
162
}
161
163
assertEquals (Service .State .FAILED , service .state ());
162
164
}
@@ -208,7 +210,7 @@ protected ScheduledExecutorService executor() {
208
210
209
211
@ Override
210
212
protected Scheduler scheduler () {
211
- return newFixedDelaySchedule (0 , 1 , TimeUnit . MILLISECONDS );
213
+ return newFixedDelaySchedule (0 , 1 , MILLISECONDS );
212
214
}
213
215
};
214
216
@@ -217,7 +219,7 @@ protected Scheduler scheduler() {
217
219
service .awaitRunning ();
218
220
service .stopAsync ();
219
221
service .awaitTerminated ();
220
- assertTrue (executor .get ().awaitTermination (100 , TimeUnit . MILLISECONDS ));
222
+ assertTrue (executor .get ().awaitTermination (100 , MILLISECONDS ));
221
223
}
222
224
223
225
public void testDefaultExecutorIsShutdownWhenServiceFails () throws Exception {
@@ -240,7 +242,7 @@ protected ScheduledExecutorService executor() {
240
242
241
243
@ Override
242
244
protected Scheduler scheduler () {
243
- return newFixedDelaySchedule (0 , 1 , TimeUnit . MILLISECONDS );
245
+ return newFixedDelaySchedule (0 , 1 , MILLISECONDS );
244
246
}
245
247
};
246
248
@@ -250,7 +252,7 @@ protected Scheduler scheduler() {
250
252
} catch (IllegalStateException expected ) {
251
253
}
252
254
253
- assertTrue (executor .get ().awaitTermination (100 , TimeUnit . MILLISECONDS ));
255
+ assertTrue (executor .get ().awaitTermination (100 , MILLISECONDS ));
254
256
}
255
257
256
258
public void testSchedulerOnlyCalledOnce () throws Exception {
@@ -277,7 +279,7 @@ public void testTimeout() {
277
279
new AbstractScheduledService () {
278
280
@ Override
279
281
protected Scheduler scheduler () {
280
- return Scheduler .newFixedDelaySchedule (0 , 1 , TimeUnit . NANOSECONDS );
282
+ return Scheduler .newFixedDelaySchedule (0 , 1 , NANOSECONDS );
281
283
}
282
284
283
285
@ Override
@@ -294,7 +296,7 @@ protected String serviceName() {
294
296
}
295
297
};
296
298
try {
297
- service .startAsync ().awaitRunning (1 , TimeUnit . MILLISECONDS );
299
+ service .startAsync ().awaitRunning (1 , MILLISECONDS );
298
300
fail ("Expected timeout" );
299
301
} catch (TimeoutException e ) {
300
302
assertThat (e )
@@ -366,9 +368,9 @@ protected Scheduler scheduler() {
366
368
public static class SchedulerTest extends TestCase {
367
369
// These constants are arbitrary and just used to make sure that the correct method is called
368
370
// 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 ;
372
374
373
375
// Unique runnable object used for comparison.
374
376
final Runnable testRunnable =
@@ -382,22 +384,22 @@ private void assertSingleCallWithCorrectParameters(
382
384
Runnable command , long initialDelay , long delay , TimeUnit unit ) {
383
385
assertFalse (called ); // only called once.
384
386
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 );
388
390
assertEquals (testRunnable , command );
389
391
}
390
392
391
393
public void testFixedRateSchedule () {
392
- Scheduler schedule = Scheduler .newFixedRateSchedule (initialDelay , delay , unit );
394
+ Scheduler schedule = Scheduler .newFixedRateSchedule (INITIAL_DELAY , DELAY , UNIT );
393
395
Cancellable unused =
394
396
schedule .schedule (
395
397
null ,
396
398
new ScheduledThreadPoolExecutor (1 ) {
397
399
@ Override
398
400
public ScheduledFuture <?> scheduleAtFixedRate (
399
401
Runnable command , long initialDelay , long period , TimeUnit unit ) {
400
- assertSingleCallWithCorrectParameters (command , initialDelay , delay , unit );
402
+ assertSingleCallWithCorrectParameters (command , initialDelay , period , unit );
401
403
return new ThrowingScheduledFuture <>();
402
404
}
403
405
},
@@ -406,7 +408,7 @@ public ScheduledFuture<?> scheduleAtFixedRate(
406
408
}
407
409
408
410
public void testFixedDelaySchedule () {
409
- Scheduler schedule = newFixedDelaySchedule (initialDelay , delay , unit );
411
+ Scheduler schedule = newFixedDelaySchedule (INITIAL_DELAY , DELAY , UNIT );
410
412
Cancellable unused =
411
413
schedule .schedule (
412
414
null ,
@@ -487,13 +489,13 @@ protected Schedule getNextSchedule() throws Exception {
487
489
service .awaitTerminated ();
488
490
}
489
491
490
- private class TestCustomScheduler extends AbstractScheduledService .CustomScheduler {
492
+ private static class TestCustomScheduler extends AbstractScheduledService .CustomScheduler {
491
493
public AtomicInteger scheduleCounter = new AtomicInteger (0 );
492
494
493
495
@ Override
494
496
protected Schedule getNextSchedule () throws Exception {
495
497
scheduleCounter .incrementAndGet ();
496
- return new Schedule (0 , TimeUnit . SECONDS );
498
+ return new Schedule (0 , SECONDS );
497
499
}
498
500
}
499
501
@@ -538,7 +540,7 @@ public void testCustomSchedulerServiceStop() throws Exception {
538
540
service .secondBarrier .await ();
539
541
service .awaitTerminated ();
540
542
// 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 ));
542
544
assertEquals (1 , service .numIterations .get ());
543
545
}
544
546
@@ -562,7 +564,7 @@ protected Schedule getNextSchedule() throws Exception {
562
564
Thread .yield ();
563
565
throw new RuntimeException ("boom" );
564
566
}
565
- return new Schedule (0 , TimeUnit . NANOSECONDS );
567
+ return new Schedule (0 , NANOSECONDS );
566
568
}
567
569
};
568
570
}
@@ -584,7 +586,7 @@ protected Scheduler scheduler() {
584
586
protected Schedule getNextSchedule () throws Exception {
585
587
// Explicitly yield to increase the probability of a pathological scheduling.
586
588
Thread .yield ();
587
- return new Schedule (0 , TimeUnit . SECONDS );
589
+ return new Schedule (0 , SECONDS );
588
590
}
589
591
};
590
592
}
@@ -627,7 +629,7 @@ protected Scheduler scheduler() {
627
629
return new CustomScheduler () {
628
630
@ Override
629
631
protected Schedule getNextSchedule () throws Exception {
630
- return new Schedule (delay , unit );
632
+ return new Schedule (DELAY , UNIT );
631
633
}
632
634
};
633
635
}
@@ -644,7 +646,7 @@ public void testCustomSchedulerFailure() throws Exception {
644
646
}
645
647
Thread .sleep (1000 );
646
648
try {
647
- service .stopAsync ().awaitTerminated (100 , TimeUnit . SECONDS );
649
+ service .stopAsync ().awaitTerminated (100 , SECONDS );
648
650
fail ();
649
651
} catch (IllegalStateException e ) {
650
652
assertEquals (State .FAILED , service .state ());
@@ -677,7 +679,7 @@ protected Schedule getNextSchedule() throws Exception {
677
679
if (numIterations .get () > 2 ) {
678
680
throw new IllegalStateException ("Failed" );
679
681
}
680
- return new Schedule (delay , unit );
682
+ return new Schedule (DELAY , UNIT );
681
683
}
682
684
};
683
685
}
0 commit comments