Skip to content

Commit 27df5f9

Browse files
grpc-api/stub: Included schedule() with Duration
1 parent de91ea3 commit 27df5f9

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

api/src/main/java/io/grpc/SynchronizationContext.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ public String toString() {
164164
return new ScheduledHandle(runnable, future);
165165
}
166166

167+
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/10245")
168+
public final ScheduledHandle schedule(
169+
final Runnable task, Duration delay, ScheduledExecutorService timerService) {
170+
return schedule(task, convertToNanos(delay), TimeUnit.NANOSECONDS, timerService);
171+
}
172+
167173
/**
168174
* Schedules a task to be added and run via {@link #execute} after an initial delay and then
169175
* repeated after the delay until cancelled.

api/src/test/java/io/grpc/SynchronizationContextTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,23 @@ public void schedule() {
247247
verify(task1).run();
248248
}
249249

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+
250267
@Test
251268
public void scheduleWithFixedDelayDuration() {
252269
MockScheduledExecutorService executorService = new MockScheduledExecutorService();

0 commit comments

Comments
 (0)