### Question:
GlusterWatchService implements WatchService { @Override public WatchKey poll() { if (!running) { throw new ClosedWatchServiceException(); } WatchKey pending = popPending(); if (null != pending) { return pending; } for (GlusterWatchKey k : paths) { if (k.isValid() && k.isReady() && k.update()) { pendingPaths.add(k); } } return popPending(); }  WatchKey registerPath(GlusterPath path, WatchEvent.Kind... kinds); @Override void close(); @Override WatchKey poll(); @Override WatchKey poll(long timeout, TimeUnit unit); @Override WatchKey take(); static final int MILLIS_PER_SECOND; static final int MILLIS_PER_MINUTE; static final int MILLIS_PER_HOUR; static final int MILLIS_PER_DAY; static long PERIOD; }### Answer:
@Test public void testPollTimeout() throws InterruptedException { long timeout = 150L; TimeUnit unit = TimeUnit.MILLISECONDS; WatchKey mockKey = mock(WatchKey.class); PowerMockito.when(watchService.poll()).thenReturn(null).thenReturn(mockKey); PowerMockito.spy(Thread.class); PowerMockito.doThrow(new InterruptedException()).when(Thread.class); Thread.sleep(GlusterWatchService.PERIOD); WatchKey key = watchService.poll(timeout, unit); assertEquals(mockKey, key); Mockito.verify(watchService, Mockito.times(2)).poll(); PowerMockito.verifyStatic(Mockito.times(1)); Thread.sleep(GlusterWatchService.PERIOD); }
@Test(expected = ClosedWatchServiceException.class) public void testPoll_whenNotRunning() { watchService.setRunning(false); watchService.poll(); }
@Test public void testPoll_whenPending() { watchService.setRunning(true); GlusterWatchKey mockKey = mock(GlusterWatchKey.class); watchService.getPendingPaths().add(mockKey); WatchKey key = watchService.poll(); assertEquals(mockKey, key); }
@Test public void testPoll_whenReadyAndNoEvent() { watchService.setRunning(true); GlusterWatchKey mockKey = mock(GlusterWatchKey.class); watchService.getPaths().add(mockKey); doReturn(true).when(mockKey).isValid(); doReturn(true).when(mockKey).isReady(); doReturn(false).when(mockKey).update(); WatchKey key = watchService.poll(); assertEquals(null, key); Mockito.verify(mockKey).isValid(); Mockito.verify(mockKey).isReady(); Mockito.verify(mockKey).update(); }