/** * Get a future for a non-null result from the callback. Only when the * result is asked for (using {@link Future#get()} or * {@link Future#get(long, TimeUnit)} will the polling actually start. * * @see Poller#poll(Callable) */ @Override public Future<S> poll(Callable<S> callable) throws Exception { return new DirectPollingFuture<>(interval, callable); }
@Override public S get() throws InterruptedException, ExecutionException { try { return get(-1, TimeUnit.MILLISECONDS); } catch (TimeoutException e) { throw new IllegalStateException("Unexpected timeout waiting for result", e); } }
Poller<Collection<StepExecution>> poller = new DirectPoller<>(pollInterval); Future<Collection<StepExecution>> resultsFuture = poller.poll(callback);
@Test public void testWithError() throws Exception { Callable<String> callback = new Callable<String>() { @Override public String call() throws Exception { Set<String> executions = new HashSet<>(repository); if (executions.isEmpty()) { return null; } throw new RuntimeException("Expected"); } }; Poller<String> poller = new DirectPoller<>(100L); sleepAndCreateStringInBackground(500L); try { String value = poller.poll(callback).get(1000L, TimeUnit.MILLISECONDS); assertEquals(null, value); fail("Expected ExecutionException"); } catch (ExecutionException e) { assertEquals("Expected", e.getCause().getMessage()); } }
@Test public void testSimpleSingleThreaded() throws Exception { Callable<String> callback = new Callable<String>() { @Override public String call() throws Exception { Set<String> executions = new HashSet<>(repository); if (executions.isEmpty()) { return null; } return executions.iterator().next(); } }; sleepAndCreateStringInBackground(500L); Future<String> task = new DirectPoller<String>(100L).poll(callback); String value = task.get(1000L, TimeUnit.MILLISECONDS); assertEquals("foo", value); }
Poller<Collection<StepExecution>> poller = new DirectPoller<>(this.pollInterval); Future<Collection<StepExecution>> resultsFuture = poller.poll(callback);
@Test public void testTimeUnit() throws Exception { Callable<String> callback = new Callable<String>() { @Override public String call() throws Exception { Set<String> executions = new HashSet<>(repository); if (executions.isEmpty()) { return null; } return executions.iterator().next(); } }; sleepAndCreateStringInBackground(500L); Future<String> task = new DirectPoller<String>(100L).poll(callback); String value = task.get(1L, TimeUnit.SECONDS); assertEquals("foo", value); }
/** * Get a future for a non-null result from the callback. Only when the * result is asked for (using {@link Future#get()} or * {@link Future#get(long, TimeUnit)} will the polling actually start. * * @see Poller#poll(Callable) */ @Override public Future<S> poll(Callable<S> callable) throws Exception { return new DirectPollingFuture<S>(interval, callable); }
@Override public S get() throws InterruptedException, ExecutionException { try { return get(-1, TimeUnit.MILLISECONDS); } catch (TimeoutException e) { throw new IllegalStateException("Unexpected timeout waiting for result", e); } }
Poller<Collection<StepExecution>> poller = new DirectPoller<>(this.pollInterval); Future<Collection<StepExecution>> resultsFuture = poller.poll(callback);