27
27
28
28
import com .google .common .util .concurrent .testing .TestingExecutors ;
29
29
import io .grpc .SynchronizationContext .ScheduledHandle ;
30
+ import java .time .Duration ;
30
31
import java .util .concurrent .BlockingQueue ;
31
32
import java .util .concurrent .CountDownLatch ;
32
33
import java .util .concurrent .LinkedBlockingQueue ;
@@ -72,7 +73,7 @@ public void uncaughtException(Thread t, Throwable e) {
72
73
73
74
@ Mock
74
75
private Runnable task3 ;
75
-
76
+
76
77
@ After public void tearDown () {
77
78
assertThat (uncaughtErrors ).isEmpty ();
78
79
}
@@ -246,6 +247,41 @@ public void schedule() {
246
247
verify (task1 ).run ();
247
248
}
248
249
250
+ @ Test
251
+ public void scheduleDuration () {
252
+ MockScheduledExecutorService executorService = new MockScheduledExecutorService ();
253
+ ScheduledHandle handle =
254
+ syncContext .schedule (task1 , Duration .ofSeconds (10 ), executorService );
255
+
256
+ assertThat (executorService .delay )
257
+ .isEqualTo (executorService .unit .convert (10 , TimeUnit .SECONDS ));
258
+ assertThat (handle .isPending ()).isTrue ();
259
+ verify (task1 , never ()).run ();
260
+
261
+ executorService .command .run ();
262
+
263
+ assertThat (handle .isPending ()).isFalse ();
264
+ verify (task1 ).run ();
265
+ }
266
+
267
+ @ Test
268
+ public void scheduleWithFixedDelayDuration () {
269
+ MockScheduledExecutorService executorService = new MockScheduledExecutorService ();
270
+ ScheduledHandle handle =
271
+ syncContext .scheduleWithFixedDelay (task1 , Duration .ofSeconds (10 ),
272
+ Duration .ofSeconds (10 ), executorService );
273
+
274
+ assertThat (executorService .delay )
275
+ .isEqualTo (executorService .unit .convert (10 , TimeUnit .SECONDS ));
276
+ assertThat (handle .isPending ()).isTrue ();
277
+ verify (task1 , never ()).run ();
278
+
279
+ executorService .command .run ();
280
+
281
+ assertThat (handle .isPending ()).isFalse ();
282
+ verify (task1 ).run ();
283
+ }
284
+
249
285
@ Test
250
286
public void scheduleDueImmediately () {
251
287
MockScheduledExecutorService executorService = new MockScheduledExecutorService ();
@@ -357,5 +393,13 @@ static class MockScheduledExecutorService extends ForwardingScheduledExecutorSer
357
393
this .unit = unit ;
358
394
return future = super .schedule (command , delay , unit );
359
395
}
396
+
397
+ @ Override public ScheduledFuture <?> scheduleWithFixedDelay (Runnable command , long intialDelay ,
398
+ long delay , TimeUnit unit ) {
399
+ this .command = command ;
400
+ this .delay = delay ;
401
+ this .unit = unit ;
402
+ return future = super .scheduleWithFixedDelay (command , intialDelay , delay , unit );
403
+ }
360
404
}
361
- }
405
+ }
0 commit comments