4444import java .util .concurrent .ScheduledExecutorService ;
4545import java .util .concurrent .ScheduledFuture ;
4646import java .util .concurrent .TimeUnit ;
47+ import java .util .concurrent .TimeoutException ;
4748import org .junit .Before ;
4849import org .junit .Test ;
4950import org .junit .runner .RunWith ;
@@ -195,14 +196,7 @@ public void testMultiple() throws Exception {
195196 @ SuppressWarnings ("unchecked" )
196197 public void testWatchdogBeingClosed () {
197198 ScheduledFuture future = Mockito .mock (ScheduledFuture .class );
198- ScheduledExecutorService mockExecutor = Mockito .mock (ScheduledExecutorService .class );
199- Mockito .when (
200- mockExecutor .scheduleAtFixedRate (
201- Mockito .any (Watchdog .class ),
202- Mockito .anyLong (),
203- Mockito .anyLong (),
204- Mockito .any (TimeUnit .class )))
205- .thenReturn (future );
199+ ScheduledExecutorService mockExecutor = getMockExecutorService (future );
206200 Watchdog underTest = Watchdog .create (clock , checkInterval , mockExecutor );
207201 assertThat (underTest ).isInstanceOf (BackgroundResource .class );
208202
@@ -219,6 +213,77 @@ public void testWatchdogBeingClosed() {
219213 Mockito .verifyNoMoreInteractions (mockExecutor );
220214 }
221215
216+ @ Test
217+ public void awaitTermination_shouldReturnTrueIfFutureIsDone () throws Exception {
218+ int duration = 1000 ;
219+ TimeUnit timeUnit = TimeUnit .MILLISECONDS ;
220+ ScheduledFuture future = Mockito .mock (ScheduledFuture .class );
221+ ScheduledExecutorService mockExecutor = getMockExecutorService (future );
222+ Watchdog watchdog = Watchdog .create (clock , checkInterval , mockExecutor );
223+ watchdog .shutdown ();
224+
225+ boolean actual = watchdog .awaitTermination (duration , timeUnit );
226+
227+ assertThat (actual ).isTrue ();
228+ }
229+
230+ @ Test
231+ public void awaitTermination_shouldReturnFalseIfGettingFutureTimedOut () throws Exception {
232+ int duration = 1000 ;
233+ TimeUnit timeUnit = TimeUnit .MILLISECONDS ;
234+ ScheduledFuture future = Mockito .mock (ScheduledFuture .class );
235+ Mockito .doThrow (new TimeoutException ()).when (future ).get (duration , timeUnit );
236+ ScheduledExecutorService mockExecutor = getMockExecutorService (future );
237+ Watchdog watchdog = Watchdog .create (clock , checkInterval , mockExecutor );
238+
239+ boolean actual = watchdog .awaitTermination (duration , timeUnit );
240+
241+ assertThat (actual ).isFalse ();
242+ }
243+
244+ @ Test
245+ public void awaitTermination_shouldReturnTrueIfFutureIsAlreadyCancelled () throws Exception {
246+ int duration = 1000 ;
247+ TimeUnit timeUnit = TimeUnit .MILLISECONDS ;
248+ ScheduledFuture future = Mockito .mock (ScheduledFuture .class );
249+ Mockito .doThrow (new CancellationException ()).when (future ).get (duration , timeUnit );
250+ ScheduledExecutorService mockExecutor = getMockExecutorService (future );
251+ Watchdog watchdog = Watchdog .create (clock , checkInterval , mockExecutor );
252+
253+ boolean actual = watchdog .awaitTermination (duration , timeUnit );
254+
255+ assertThat (actual ).isTrue ();
256+ }
257+
258+ @ Test
259+ public void awaitTermination_shouldReturnFalseIfGettingFutureThrowsExecutionException ()
260+ throws Exception {
261+ int duration = 1000 ;
262+ TimeUnit timeUnit = TimeUnit .MILLISECONDS ;
263+ ScheduledFuture future = Mockito .mock (ScheduledFuture .class );
264+ Mockito .doThrow (new ExecutionException (new RuntimeException ()))
265+ .when (future )
266+ .get (duration , timeUnit );
267+ ScheduledExecutorService mockExecutor = getMockExecutorService (future );
268+ Watchdog watchdog = Watchdog .create (clock , checkInterval , mockExecutor );
269+
270+ boolean actual = watchdog .awaitTermination (duration , timeUnit );
271+
272+ assertThat (actual ).isTrue ();
273+ }
274+
275+ private ScheduledExecutorService getMockExecutorService (ScheduledFuture future ) {
276+ ScheduledExecutorService mockExecutor = Mockito .mock (ScheduledExecutorService .class );
277+ Mockito .when (
278+ mockExecutor .scheduleAtFixedRate (
279+ Mockito .any (Watchdog .class ),
280+ Mockito .anyLong (),
281+ Mockito .anyLong (),
282+ Mockito .any (TimeUnit .class )))
283+ .thenReturn (future );
284+ return mockExecutor ;
285+ }
286+
222287 static class AccumulatingObserver <T > implements ResponseObserver <T > {
223288 SettableApiFuture <StreamController > controller = SettableApiFuture .create ();
224289 Queue <T > responses = Queues .newLinkedBlockingDeque ();
0 commit comments