2727
2828import com .google .common .util .concurrent .testing .TestingExecutors ;
2929import io .grpc .SynchronizationContext .ScheduledHandle ;
30+ import java .time .Duration ;
3031import java .util .concurrent .BlockingQueue ;
3132import java .util .concurrent .CountDownLatch ;
3233import java .util .concurrent .LinkedBlockingQueue ;
@@ -72,7 +73,7 @@ public void uncaughtException(Thread t, Throwable e) {
7273
7374 @ Mock
7475 private Runnable task3 ;
75-
76+
7677 @ After public void tearDown () {
7778 assertThat (uncaughtErrors ).isEmpty ();
7879 }
@@ -246,6 +247,41 @@ public void schedule() {
246247 verify (task1 ).run ();
247248 }
248249
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+
249285 @ Test
250286 public void scheduleDueImmediately () {
251287 MockScheduledExecutorService executorService = new MockScheduledExecutorService ();
@@ -357,5 +393,13 @@ static class MockScheduledExecutorService extends ForwardingScheduledExecutorSer
357393 this .unit = unit ;
358394 return future = super .schedule (command , delay , unit );
359395 }
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+ }
360404 }
361- }
405+ }
0 commit comments