### Question:
AbstractService { final <R> R poll(final PollHandler<?,R> handler, long timeout, final TimeUnit unit) { notNull(handler, "handler"); final long start = System.currentTimeMillis(); while (true) { if (pollOnce(handler)) { return handler.getResult(); } if (unit != null && start + unit.toMillis(timeout) < System.currentTimeMillis()) { throw new GoodDataException("timeout"); } try { Thread.sleep(settings.getPollSleep()); } catch (InterruptedException e) { throw new GoodDataException("interrupted"); } } } AbstractService(final RestTemplate restTemplate, final GoodDataSettings settings);   }### Answer:
@Test public void pollShouldSucceedWhenUnderTimeout() throws Exception { PollHandler<?, ?> handler = mock(PollHandler.class); when(handler.isFinished(any(ClientHttpResponse.class))).thenReturn(false); when(handler.isDone()).thenReturn(false, true); service.poll(handler, 5, TimeUnit.SECONDS); }
@Test(expectedExceptions = GoodDataException.class, expectedExceptionsMessageRegExp = ".*timeout.*") public void pollShouldThrowExceptionWhenOverTimeout() throws Exception { PollHandler<?, ?> handler = mock(PollHandler.class); service.poll(handler, 5, TimeUnit.SECONDS); }