@Override public V getUnchecked() { return Futures.getUnchecked(this); }
@Override public void close() { future.cancel(false); try { getUnchecked(future); } catch (CancellationException e) { // ignore } failRequests(); }
@Override protected boolean matchesSafely(Future<V> future, Description description) { return matcher.matches(Futures.getUnchecked(future)); }
@Override public void run() { assertTrue(futureInteger.isDone()); assertTrue(futureBoolean.isDone()); result[0] = createCombinedResult( Futures.getUnchecked(futureInteger), Futures.getUnchecked(futureBoolean)); } };
public void testGetUnchecked_success() { assertEquals("foo", getUnchecked(immediateFuture("foo"))); }
@Override public V reload(K key, V oldValue) { try { V value = Futures.getUnchecked(cacheLoader.reload(key, oldValue)); if (value == null) { throw new InvalidCacheLoadException("null value"); } return value; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new CacheLoaderException(e); } catch (Exception e) { Throwables.throwIfUnchecked(e); throw new RuntimeException(e); } } }
@GwtIncompatible // Thread.interrupt public void testGetUnchecked_interrupted() { Thread.currentThread().interrupt(); try { assertEquals("foo", getUnchecked(immediateFuture("foo"))); assertTrue(Thread.currentThread().isInterrupted()); } finally { Thread.interrupted(); } }
private void shutdown() { if (instance != null) { instance.getSignaller().signalShutdown(); requestHandler = null; instance = null; Futures.getUnchecked(currentHelperFuture); currentHelperFuture = null; } }
public void testGetUnchecked_cancelled() { SettableFuture<String> future = SettableFuture.create(); future.cancel(true); try { getUnchecked(future); fail(); } catch (CancellationException expected) { } }
public void testGetUnchecked_Error() { try { getUnchecked(ERROR_FUTURE); } catch (Error expected) { assertEquals(ERROR, expected); return; } fail(); } }
public void testGetUnchecked_RuntimeException() { try { getUnchecked(RUNTIME_EXCEPTION_FUTURE); fail(); } catch (RuntimeException expected) { assertEquals(RUNTIME_EXCEPTION, expected); } }
public void testGetUnchecked_ExecutionExceptionError() { try { getUnchecked(FAILED_FUTURE_ERROR); fail(); } catch (ExecutionError expected) { assertEquals(ERROR, expected.getCause()); } }
public void testGetUnchecked_ExecutionExceptionOtherThrowable() { try { getUnchecked(FAILED_FUTURE_OTHER_THROWABLE); fail(); } catch (UncheckedExecutionException expected) { assertEquals(OTHER_THROWABLE, expected.getCause()); } }
public void testGetUnchecked_ExecutionExceptionChecked() { try { getUnchecked(FAILED_FUTURE_CHECKED_EXCEPTION); fail(); } catch (UncheckedExecutionException expected) { assertEquals(CHECKED_EXCEPTION, expected.getCause()); } }
public void testGetUnchecked_ExecutionExceptionUnchecked() { try { getUnchecked(FAILED_FUTURE_UNCHECKED_EXCEPTION); fail(); } catch (UncheckedExecutionException expected) { assertEquals(UNCHECKED_EXCEPTION, expected.getCause()); } }
public static void check(Runnable... runnables) { Collection<Callable<Object>> callables = Lists.newArrayListWithCapacity(runnables.length); ExecutorService executor = MoreExecutors.getExitingScheduledExecutorService( new ScheduledThreadPoolExecutor(runnables.length)); for (Runnable runnable : runnables) { callables.add(Executors.callable(runnable)); } List<Future<Object>> result; try { result = executor.invokeAll(callables); } catch (InterruptedException ex) { throw new RuntimeException(ex); } for (Future<Object> each : result) { // all need to complete successfully Futures.getUnchecked(each); } } }
@Test(timeout = 5_000) public void testThreadSafety() throws Exception { final int numOfThreads = 3; CountDownLatch latch = new CountDownLatch(numOfThreads); AtomicInteger counter = new AtomicInteger(); Future<Integer> lazyFuture = new LazyFuture<Integer>() { @Override @SneakyThrows(InterruptedException.class) protected Integer resolve() { latch.await(); return counter.incrementAndGet(); } }; Future<List<Integer>> task = new ForkJoinPool(numOfThreads).submit(() -> { return IntStream.rangeClosed(1, numOfThreads).parallel().mapToObj(i -> Futures.getUnchecked(lazyFuture)).collect(toList()); }); while (latch.getCount() > 0) { latch.countDown(); } assertEquals("All threads receives the same result", Collections.nCopies(numOfThreads, 1), task.get()); }
@Test public void testDistributesSpillOverPaths() throws Exception { List<Type> types = ImmutableList.of(BIGINT); BlockEncodingSerde blockEncodingSerde = new BlockEncodingManager(new TypeRegistry()); List<Path> spillPaths = ImmutableList.of(spillPath1.toPath(), spillPath2.toPath()); FileSingleStreamSpillerFactory spillerFactory = new FileSingleStreamSpillerFactory( executor, // executor won't be closed, because we don't call destroy() on the spiller factory blockEncodingSerde, new SpillerStats(), spillPaths, 1.0); assertEquals(listFiles(spillPath1.toPath()).size(), 0); assertEquals(listFiles(spillPath2.toPath()).size(), 0); Page page = buildPage(); List<SingleStreamSpiller> spillers = new ArrayList<>(); for (int i = 0; i < 10; ++i) { SingleStreamSpiller singleStreamSpiller = spillerFactory.create(types, bytes -> {}, newSimpleAggregatedMemoryContext().newLocalMemoryContext("test")); getUnchecked(singleStreamSpiller.spill(page)); spillers.add(singleStreamSpiller); } assertEquals(listFiles(spillPath1.toPath()).size(), 5); assertEquals(listFiles(spillPath2.toPath()).size(), 5); spillers.forEach(SingleStreamSpiller::close); assertEquals(listFiles(spillPath1.toPath()).size(), 0); assertEquals(listFiles(spillPath2.toPath()).size(), 0); }
private List<TimestampRange> requestBatches(int... sizes) { List<BatchElement<Integer, TimestampRange>> elements = Arrays.stream(sizes) .mapToObj(size -> ImmutableTestBatchElement.builder() .argument(size) .result(SettableFuture.create()) .build()) .collect(toList()); RequestBatchingTimestampService.consumer(unbatchedDelegate).accept(elements); return Futures.getUnchecked(Futures.allAsList(Lists.transform(elements, BatchElement::result))); }
private void should_iterate_result_set_asynchronously(int totalCount, int fetchSize) { for (int i = 0; i < totalCount; i++) session().execute(String.format("insert into ints (i) values (%d)", i)); Statement statement = new SimpleStatement("select * from ints").setFetchSize(fetchSize); ResultsAccumulator results = new ResultsAccumulator(); ListenableFuture<ResultSet> future = GuavaCompatibility.INSTANCE.transformAsync(session().executeAsync(statement), results); Futures.getUnchecked(future); assertThat(results.all.size()).isEqualTo(totalCount); }