public void testSameThreadSuccess() { SettableFuture<String> f = SettableFuture.create(); MockCallback callback = new MockCallback("foo"); addCallback(f, callback, directExecutor()); f.set("foo"); }
@GwtIncompatible // get() timeout public void testTransformAsync_asyncFunction_timeout() throws InterruptedException, ExecutionException { AsyncFunction<String, Integer> function = constantAsyncFunction(immediateFuture(1)); ListenableFuture<Integer> future = transformAsync(SettableFuture.<String>create(), function, directExecutor()); try { future.get(1, MILLISECONDS); fail(); } catch (TimeoutException expected) { } }
public void testTransform_ExceptionAfterCancellation() throws Exception { class Transformer implements Function<Object, Object> { ListenableFuture<Object> output; @Override public Object apply(Object input) { output.cancel(false); throw new MyRuntimeException(); } } Transformer transformer = new Transformer(); SettableFuture<Object> input = SettableFuture.create(); ListenableFuture<Object> output = transform(input, transformer, directExecutor()); transformer.output = output; input.set("foo"); assertTrue(output.isCancelled()); }
private synchronized ListenableFuture<?> abortInternal() { // the callbacks in statement performed on another thread so are safe return nonCancellationPropagating(Futures.allAsList(connectorIdToMetadata.values().stream() .map(connection -> finishingExecutor.submit(() -> safeAbort(connection))) .collect(toList()))); }
public void testGetUnchecked_success() { assertEquals("foo", getUnchecked(immediateFuture("foo"))); }
public void testAllAsList_immediateFailure() throws Exception { Throwable exception = new Throwable("failed"); ListenableFuture<String> future1 = immediateFailedFuture(exception); ListenableFuture<String> future2 = immediateFuture("results"); ListenableFuture<List<String>> compound = allAsList(ImmutableList.of(future1, future2)); try { getDone(compound); fail(); } catch (ExecutionException expected) { assertSame(exception, expected.getCause()); } }
@Test public void testBinaryOperatorsIntegerDecimal() throws Exception { for (Integer left : intLefts) { for (BigDecimal right : decimalRights) { assertExecute(generateExpression("%s = %s", left, right), BOOLEAN, left == null || right == null ? null : new BigDecimal(left).equals(right)); assertExecute(generateExpression("%s <> %s", left, right), BOOLEAN, left == null || right == null ? null : !new BigDecimal(left).equals(right)); assertExecute(generateExpression("%s > %s", left, right), BOOLEAN, left == null || right == null ? null : new BigDecimal(left).compareTo(right) > 0); assertExecute(generateExpression("%s < %s", left, right), BOOLEAN, left == null || right == null ? null : new BigDecimal(left).compareTo(right) < 0); assertExecute(generateExpression("%s >= %s", left, right), BOOLEAN, left == null || right == null ? null : new BigDecimal(left).compareTo(right) >= 0); assertExecute(generateExpression("%s <= %s", left, right), BOOLEAN, left == null || right == null ? null : new BigDecimal(left).compareTo(right) <= 0); assertExecute(generateExpression("nullif(%s, %s)", left, right), INTEGER, left); assertExecute(generateExpression("%s is distinct from %s", left, right), BOOLEAN, !Objects.equals(left == null ? null : new BigDecimal(left), right)); // arithmetic operators are already tested in TestDecimalOperators } } Futures.allAsList(futures).get(); }
@Test public void testSearchCaseMultiple() throws Exception expected = null; else if (firstTest != null && value.doubleValue() == firstTest) { expected = "first"; else if (secondTest != null && value.doubleValue() == secondTest) { expected = "second"; Futures.allAsList(futures).get();
@Test public void testTernaryOperatorsLongDecimal() throws Exception { for (Long first : longLefts) { for (BigDecimal second : decimalMiddle) { for (Long third : longRights) { assertExecute(generateExpression("%s between %s and %s", first, second, third), BOOLEAN, first == null || second == null || third == null ? null : second.compareTo(new BigDecimal(first)) <= 0 && first <= third); } } } Futures.allAsList(futures).get(); }
private void testWithExecutor( final DimFilter filter, final List<String> expectedRows ) { ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(EXECUTOR_NUM_THREADS)); List<ListenableFuture<?>> futures = new ArrayList<>(); for (int i = 0; i < EXECUTOR_NUM_TASKS; i++) { Runnable runnable = makeFilterRunner(filter, expectedRows); ListenableFuture fut = executor.submit(runnable); futures.add(fut); } try { Futures.allAsList(futures).get(60, TimeUnit.SECONDS); } catch (Exception ex) { Assert.fail(ex.getMessage()); } executor.shutdown(); } }
@Test(timeOut = SHUTDOWN_TIMEOUT_MILLIS) public void testShutdown() throws Exception List<ListenableFuture<?>> queryFutures = new ArrayList<>(); for (int i = 0; i < 5; i++) { queryFutures.add(executor.submit(() -> queryRunner.execute("SELECT COUNT(*), clerk FROM orders GROUP BY clerk"))); Futures.allAsList(queryFutures).get();
@Test public void testUnaryOperators() throws Exception assertExecute(generateExpression("- (%s)", value), value == null ? null : value.negate()); assertExecute(generateExpression("%s is null", value), BOOLEAN, value == null); assertExecute(generateExpression("%s is not null", value), BOOLEAN, value != null); Futures.allAsList(futures).get();
private void verifyAndMergeCheckpoints(final Collection<TaskGroup> taskGroupsToVerify) { final List<ListenableFuture<?>> futures = new ArrayList<>(); for (TaskGroup taskGroup : taskGroupsToVerify) { futures.add(workerExec.submit(() -> verifyAndMergeCheckpoints(taskGroup))); } try { Futures.allAsList(futures).get(futureTimeoutInSeconds, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { throw new RuntimeException(e); } }
public void testAllAsList_resultCancelled() throws Exception { SettableFuture<String> future1 = SettableFuture.create(); SettableFuture<String> future2 = SettableFuture.create(); @SuppressWarnings("unchecked") // array is never modified ListenableFuture<List<String>> compound = allAsList(future1, future2); future2.set(DATA2); assertFalse(compound.isDone()); assertTrue(compound.cancel(false)); assertTrue(compound.isCancelled()); assertTrue(future1.isCancelled()); assertFalse(future1.wasInterrupted()); }
public void testAllAsList_failure() throws Exception { SingleCallListener listener = new SingleCallListener(); SettableFuture<String> future1 = SettableFuture.create(); SettableFuture<String> future2 = SettableFuture.create(); @SuppressWarnings("unchecked") // array is never modified ListenableFuture<List<String>> compound = allAsList(future1, future2); compound.addListener(listener, directExecutor()); listener.expectCall(); Throwable exception = new Throwable("failed1"); future1.setException(exception); assertTrue(compound.isDone()); assertTrue(listener.wasCalled()); assertFalse(future2.isDone()); try { getDone(compound); fail(); } catch (ExecutionException expected) { assertSame(exception, expected.getCause()); } }
public void testTransformAsync_cancelPropagatesToAsyncOutput() throws Exception { ListenableFuture<Foo> immediate = immediateFuture(new Foo()); final SettableFuture<Bar> secondary = SettableFuture.create(); AsyncFunction<Foo, Bar> function = new AsyncFunction<Foo, Bar>() { @Override public ListenableFuture<Bar> apply(Foo unused) { return secondary; } }; assertTrue(transformAsync(immediate, function, directExecutor()).cancel(false)); assertTrue(secondary.isCancelled()); assertFalse(secondary.wasInterrupted()); }
public void testTransform_listenerThrowsError() throws Exception { SettableFuture<Object> input = SettableFuture.create(); ListenableFuture<Object> output = transform(input, identity(), directExecutor()); output.addListener( new Runnable() { @Override public void run() { throw new MyError(); } }, directExecutor()); try { input.set("foo"); fail(); } catch (MyError expected) { } }
public void testSameThreadExecutionException() { SettableFuture<String> f = SettableFuture.create(); Exception e = new IllegalArgumentException("foo not found"); MockCallback callback = new MockCallback(e); addCallback(f, callback, directExecutor()); f.setException(e); }
public void testAllAsList_error() throws Exception { Error error = new Error("deliberate"); SettableFuture<String> future1 = SettableFuture.create(); ListenableFuture<String> future2 = immediateFuture("results"); ListenableFuture<List<String>> compound = allAsList(ImmutableList.of(future1, future2)); future1.setException(error); try { getDone(compound); fail(); } catch (ExecutionException expected) { assertSame(error, expected.getCause()); } }