private static void assertFutureFailsWithMockFailure(ListenableFuture<?> future) { assertTrue(future.isDone()); try { future.get(); fail(); } catch (Exception e) { assertContains(e.getMessage(), "Mock failure"); } }
/** * Submits the task and adds a listener that adds the future to {@code queue} when it completes. */ @GwtIncompatible // TODO private static <T> ListenableFuture<T> submitAndAddQueueListener( ListeningExecutorService executorService, Callable<T> task, final BlockingQueue<Future<T>> queue) { final ListenableFuture<T> future = executorService.submit(task); future.addListener( new Runnable() { @Override public void run() { queue.add(future); } }, directExecutor()); return future; }
public void testCompletionOrderFutureInterruption() throws Exception { SettableFuture<Long> future1 = SettableFuture.create(); SettableFuture<Long> future2 = SettableFuture.create(); SettableFuture<Long> future3 = SettableFuture.create(); ImmutableList<ListenableFuture<Long>> futures = inCompletionOrder(ImmutableList.<ListenableFuture<Long>>of(future1, future2, future3)); future2.set(1L); futures.get(1).cancel(true); futures.get(2).cancel(false); assertTrue(future1.isCancelled()); assertFalse(future1.wasInterrupted()); assertTrue(future3.isCancelled()); assertFalse(future3.wasInterrupted()); }
public void testCancelledFuture() throws InterruptedException, ExecutionException { assertTrue(future.isDone()); assertTrue(future.isCancelled()); assertTrue(latch.await(5, TimeUnit.SECONDS)); assertTrue(future.isDone()); assertTrue(future.isCancelled()); try { future.get(); fail("Future should throw CancellationException on cancel."); } catch (CancellationException expected) { } }
public void setUp() { future.addListener( new Runnable() { @Override public void run() { latch.countDown(); } }, exec); assertEquals(1, latch.getCount()); assertFalse(future.isDone()); assertFalse(future.isCancelled()); }
@Test(timeOut = 10_000) public void testEmptyQueue() throws Exception { AsyncQueue<String> queue = new AsyncQueue<>(4, executor); assertTrue(queue.offer("1").isDone()); assertTrue(queue.offer("2").isDone()); assertTrue(queue.offer("3").isDone()); assertEquals(queue.getBatchAsync(2).get(), ImmutableList.of("1", "2")); assertEquals(queue.getBatchAsync(2).get(), ImmutableList.of("3")); ListenableFuture<?> batchFuture = queue.getBatchAsync(2); assertFalse(batchFuture.isDone()); assertTrue(queue.offer("4").isDone()); assertEquals(batchFuture.get(), ImmutableList.of("4")); batchFuture = queue.getBatchAsync(2); assertFalse(batchFuture.isDone()); queue.finish(); batchFuture.get(); assertTrue(queue.isFinished()); }
@Test public void testMultipleNodes() { FixedCountScheduler nodeScheduler = new FixedCountScheduler( (node, partition, totalPartitions) -> Optional.of(taskFactory.createTableScanTask( new TaskId("test", 1, 1), node, ImmutableList.of(), new PartitionedSplitCountTracker(delta -> {}))), generateRandomNodes(5)); ScheduleResult result = nodeScheduler.schedule(); assertTrue(result.isFinished()); assertTrue(result.getBlocked().isDone()); assertEquals(result.getNewTasks().size(), 5); assertEquals(result.getNewTasks().stream().map(RemoteTask::getNodeId).collect(toImmutableSet()).size(), 5); }
@Test(timeOut = 10_000) public void testGetPartial() throws Exception { AsyncQueue<String> queue = new AsyncQueue<>(4, executor); queue.offer("1"); queue.offer("2"); queue.offer("3"); assertEquals(queue.getBatchAsync(100).get(), ImmutableList.of("1", "2", "3")); queue.finish(); assertTrue(queue.isFinished()); }
public static void addPage(OutputBuffer buffer, Page page) { buffer.enqueue(ImmutableList.of(PAGES_SERDE.serialize(page))); assertTrue(buffer.isFull().isDone(), "Expected add page to not block"); }
private static ListenableFuture<?> enqueuePage(OutputBuffer buffer, Page page) { buffer.enqueue(ImmutableList.of(PAGES_SERDE.serialize(page))); ListenableFuture<?> future = buffer.isFull(); assertFalse(future.isDone()); return future; }
public void testAllAsList_emptyList() throws Exception { SingleCallListener listener = new SingleCallListener(); listener.expectCall(); List<ListenableFuture<String>> futures = ImmutableList.of(); ListenableFuture<List<String>> compound = allAsList(futures); compound.addListener(listener, directExecutor()); assertThat(getDone(compound)).isEmpty(); assertTrue(listener.wasCalled()); }
@AndroidIncompatible // reference is never cleared under some versions of the emulator @GwtIncompatible public void testInputGCedIfUnreferenced() throws Exception { SettableFuture<Long> future1 = SettableFuture.create(); SettableFuture<Long> future2 = SettableFuture.create(); WeakReference<SettableFuture<Long>> future1Ref = new WeakReference<>(future1); WeakReference<SettableFuture<Long>> future2Ref = new WeakReference<>(future2); ImmutableList<ListenableFuture<Long>> delegates = inCompletionOrder(ImmutableList.<ListenableFuture<Long>>of(future1, future2)); future1.set(1L); future1 = null; // First future is complete, should be unreferenced GcFinalization.awaitClear(future1Ref); ListenableFuture<Long> outputFuture1 = delegates.get(0); delegates = null; future2 = null; // No references to list or other output future, second future should be unreferenced GcFinalization.awaitClear(future2Ref); outputFuture1.get(); }
@Test public void testNullif() throws Exception { assertExecute("nullif(NULL, NULL)", UNKNOWN, null); assertExecute("nullif(NULL, 2)", UNKNOWN, null); assertExecute("nullif(2, NULL)", INTEGER, 2); assertExecute("nullif(BIGINT '2', NULL)", BIGINT, 2L); assertExecute("nullif(ARRAY[CAST(1 AS BIGINT)], ARRAY[CAST(1 AS BIGINT)])", new ArrayType(BIGINT), null); assertExecute("nullif(ARRAY[CAST(1 AS BIGINT)], ARRAY[CAST(NULL AS BIGINT)])", new ArrayType(BIGINT), ImmutableList.of(1L)); assertExecute("nullif(ARRAY[CAST(NULL AS BIGINT)], ARRAY[CAST(NULL AS BIGINT)])", new ArrayType(BIGINT), singletonList(null)); // Test coercion in which the CAST function takes ConnectorSession (e.g. MapToMapCast) assertExecute("nullif(" + "map(array[1], array[smallint '1']), " + "map(array[1], array[integer '1']))", mapType(INTEGER, SMALLINT), null); Futures.allAsList(futures).get(); }
public void testSuccessfulAsList_emptyList() throws Exception { SingleCallListener listener = new SingleCallListener(); listener.expectCall(); List<ListenableFuture<String>> futures = ImmutableList.of(); ListenableFuture<List<String>> compound = successfulAsList(futures); compound.addListener(listener, directExecutor()); assertThat(getDone(compound)).isEmpty(); assertTrue(listener.wasCalled()); }
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 public void testMemoryFutureCancellation() { setUpCountStarFromOrdersWithJoin(); ListenableFuture future = userPool.reserve(fakeQueryId, "test", TEN_MEGABYTES.toBytes()); assertTrue(!future.isDone()); try { future.cancel(true); fail("cancel should fail"); } catch (UnsupportedOperationException e) { assertEquals(e.getMessage(), "cancellation is not supported"); } userPool.free(fakeQueryId, "test", TEN_MEGABYTES.toBytes()); assertTrue(future.isDone()); }
@GwtIncompatible public void testImmediateCancelledFutureStack() throws Exception { ListenableFuture<String> future = CallerClass1.makeImmediateCancelledFuture(); assertTrue(future.isCancelled()); try { CallerClass2.get(future); fail(); } catch (CancellationException expected) { // There should be two CancellationException chained together. The outer one should have the // stack trace of where the get() call was made, and the inner should have the stack trace of // where the immediateCancelledFuture() call was made. List<StackTraceElement> stackTrace = ImmutableList.copyOf(expected.getStackTrace()); assertFalse(Iterables.any(stackTrace, hasClassName(CallerClass1.class))); assertTrue(Iterables.any(stackTrace, hasClassName(CallerClass2.class))); // See AbstractFutureCancellationCauseTest for how to set causes. assertThat(expected.getCause()).isNull(); } }
private static void assertPartitionedRemovePage(LocalExchangeSource source, int partition, int partitionCount) { assertTrue(source.waitForReading().isDone()); Page page = source.removePage(); assertNotNull(page); LocalPartitionGenerator partitionGenerator = new LocalPartitionGenerator(new InterpretedHashGenerator(TYPES, new int[] {0}), partitionCount); for (int position = 0; position < page.getPositionCount(); position++) { assertEquals(partitionGenerator.getPartition(page, position), partition); } }
@Test public void interruptTest() throws InterruptedException { ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor()); final ListenableFuture<Object> future = executor.submit(() -> { // try { System.out.println("Waiting"); Thread.sleep(10000); System.out.println("Complete"); return null; // } catch (Exception e) { // e.printStackTrace(); // throw e; // } }); future.addListener(() -> { System.out.println("Listener: " + future.isCancelled() + ", " + future.isDone()); try { future.get(); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (ExecutionException e) { throw new RuntimeException(e); } }, MoreExecutors.directExecutor()); Thread.sleep(1000); future.cancel(true); }