@GwtIncompatible // makeChecked public void testMakeChecked_listenersRunOnFailure() throws Exception { SettableFuture<String> future = SettableFuture.create(); CheckedFuture<String, TestException> checked = makeChecked( future, new Function<Exception, TestException>() { @Override public TestException apply(Exception from) { throw new NullPointerException(); } }); ListenableFutureTester tester = new ListenableFutureTester(checked); tester.setUp(); future.setException(new Exception("failed")); tester.testFailedFuture("failed"); tester.tearDown(); }
@GwtIncompatible // makeChecked public void testMakeChecked_listenersRunOnceCompleted() throws Exception { SettableFuture<String> future = SettableFuture.create(); CheckedFuture<String, TestException> checked = makeChecked( future, new Function<Exception, TestException>() { @Override public TestException apply(Exception from) { throw new NullPointerException(); } }); ListenableFutureTester tester = new ListenableFutureTester(checked); tester.setUp(); future.set(DATA1); tester.testCompletedFuture(DATA1); tester.tearDown(); }
@GwtIncompatible // makeChecked public void testMakeChecked_propagatesFailedMappers() throws Exception { SettableFuture<String> future = SettableFuture.create(); CheckedFuture<String, TestException> checked = makeChecked( future, new Function<Exception, TestException>() { @Override public TestException apply(Exception from) { throw new NullPointerException(); } }); future.setException(new Exception("failed")); try { checked.checkedGet(); fail(); } catch (NullPointerException expected) { } try { checked.checkedGet(5, SECONDS); fail(); } catch (NullPointerException expected) { } }
SettableFuture<String> future = SettableFuture.create(); CheckedFuture<String, TestException> checked = makeChecked(future, mapper);
@GwtIncompatible // makeChecked public void testMakeChecked_listenersRunOnCancel() throws Exception { SettableFuture<String> future = SettableFuture.create(); CheckedFuture<String, TestException> checked = makeChecked( future, new Function<Exception, TestException>() { @Override public TestException apply(Exception from) { throw new NullPointerException(); } }); ListenableFutureTester tester = new ListenableFutureTester(checked); tester.setUp(); future.cancel(true); // argument is ignored tester.testCancelledFuture(); tester.tearDown(); }
SettableFuture<String> future = SettableFuture.create(); CheckedFuture<String, TestException> checked = makeChecked(future, mapper);
@GwtIncompatible // makeChecked public void testMakeChecked_mapsCancellation() throws Exception { SettableFuture<String> future = SettableFuture.create(); CheckedFuture<String, TestException> checked = makeChecked(future, mapper); assertTrue(future.cancel(true)); // argument is ignored try { checked.get(); fail(); } catch (CancellationException expected) { } try { checked.get(5, SECONDS); fail(); } catch (CancellationException expected) { } try { checked.checkedGet(); fail(); } catch (TestException expected) { assertThat(expected.getCause()).isInstanceOf(CancellationException.class); } try { checked.checkedGet(5, SECONDS); fail(); } catch (TestException expected) { assertThat(expected.getCause()).isInstanceOf(CancellationException.class); } }
/** * @param rolePushResult result of role push request * @return future which throws {@link RolePushException} */ public static CheckedFuture<Boolean, RolePushException> makeCheckedRuleRequestFxResult( ListenableFuture<Boolean> rolePushResult) { return Futures.makeChecked( rolePushResult, exceptionFunction ); } }
/** * Returns a {@code CheckedFuture} which has its value set immediately upon * construction. * * <p>The returned {@code Future} can't be cancelled, and its {@code isDone()} * method always returns {@code true}. Calling {@code get()} or {@code * checkedGet()} will immediately return the provided value. */ public static <V, X extends Exception> CheckedFuture<V, X> immediateCheckedFuture(@Nullable V value) { SettableFuture<V> future = SettableFuture.create(); future.set(value); return Futures.makeChecked(future, new Function<Exception, X>() { @Override public X apply(Exception e) { throw new AssertionError("impossible"); } }); }
@Override public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) { LOG.debug("ReadOnlyTransaction.exists() store={}, path={}", store, path); ListenableFuture<Boolean> listenableFuture = Futures.transform(read(store, path), IS_NODE_PRESENT); return Futures.makeChecked(listenableFuture, ANY_EX_TO_READ_FAILED_EXCEPTION_MAPPER); }
@Override public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() { final ListenableFuture<Void> commitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() { @Override public Void apply(final RpcResult<TransactionStatus> input) { return null; } }); return Futures.makeChecked(commitFutureAsVoid, new Function<Exception, TransactionCommitFailedException>() { @Override public TransactionCommitFailedException apply(final Exception input) { return new TransactionCommitFailedException("Submit of transaction " + getIdentifier() + " failed", input); } }); }
@Override public synchronized CheckedFuture<Void, TransactionCommitFailedException> submit() { final ListenableFuture<Void> commitFutureAsVoid = Futures.transform(commit(), new Function<RpcResult<TransactionStatus>, Void>() { @Override public Void apply(final RpcResult<TransactionStatus> input) { Preconditions.checkArgument(input.isSuccessful() && input.getErrors().isEmpty(), "Submit failed with errors: %s", input.getErrors()); return null; } }); return Futures.makeChecked(commitFutureAsVoid, new Function<Exception, TransactionCommitFailedException>() { @Override public TransactionCommitFailedException apply(final Exception input) { return new TransactionCommitFailedException("Submit of transaction " + getIdentifier() + " failed", input); } }); }
/** * Returns a {@code CheckedFuture} which has its value set immediately upon * construction. * * <p>The returned {@code Future} can't be cancelled, and its {@code isDone()} * method always returns {@code true}. Calling {@code get()} or {@code * checkedGet()} will immediately return the provided value. */ public static <V, X extends Exception> CheckedFuture<V, X> immediateCheckedFuture(@Nullable V value) { ValueFuture<V> future = ValueFuture.create(); future.set(value); return Futures.makeChecked(future, new Function<Exception, X>() { public X apply(Exception e) { throw new AssertionError("impossible"); } }); }
@Override public CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(SourceIdentifier sourceIdentifier) { LOG.trace("Getting yang schema source for {}", sourceIdentifier.getName()); Future<YangTextSchemaSourceSerializationProxy> result = remoteRepo.getYangTextSchemaSource(sourceIdentifier); final SettableFuture<YangTextSchemaSource> res = SettableFuture.create(); result.onComplete(new OnComplete<YangTextSchemaSourceSerializationProxy>() { @Override public void onComplete(Throwable throwable, YangTextSchemaSourceSerializationProxy yangTextSchemaSourceSerializationProxy) { if(yangTextSchemaSourceSerializationProxy != null) { res.set(yangTextSchemaSourceSerializationProxy.getRepresentation()); } if(throwable != null) { res.setException(throwable); } } }, executionContext); return Futures.makeChecked(res, MAPPER); } }
@Nonnull @Override public CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(@Nonnull final SchemaPath schemaPath, @Nullable final NormalizedNode<?, ?> normalizedNode) { DataObject input = null; if (normalizedNode != null) { // RPC input is optional final SchemaPath nodePatch = schemaPath.createChild(normalizedNode.getNodeType()); input = serializer.fromNormalizedNodeRpcData(nodePatch, (ContainerNode) normalizedNode); } final CompletableFuture<DataObject> result = rpcRegistry.invoke(schemaPath, input).toCompletableFuture(); final ListenableFuture<DOMRpcResult> output = getDOMRpcResult(toListenableFuture(result)); return Futures.makeChecked(output, ANY_EX_TO_RPC_EXCEPTION_MAPPER); }
/** * Asynchronously executes the HTTP request. * * @param request the request * @param contentType the content type used for the request * @param httpContext the HTTP Context to use for the request * * @return the future representing the response once it's available */ protected final CheckedFuture execute(@Nonnull final HttpUriRequest request, @Nonnull final MediaType contentType, @Nonnull final HttpContext httpContext) { final ListenableFuture task = executor.submit(new Callable() { public Object call() throws Exception { request.setHeader(new BasicHeader(HttpHeaders.ACCEPT, contentType.toString())); final HttpResponse response = httpClient.execute(request, httpContext); return handleResponse(request, response, httpContext); } }); return Futures.makeChecked(task, ExceptionMapper.INSTANCE); }
public <T> CheckedFuture<T, UserException> executeAsync(final ElasticAction2<T> action){ final ContextListenerImpl listener = new ContextListenerImpl(); // need to cast to jersey since the core javax.ws.rs Invocation doesn't support a typed submission. final JerseyInvocation invocation = (JerseyInvocation) action.buildRequest(target, listener); final SettableFuture<T> future = SettableFuture.create(); invocation.submit(new GenericType<T>(action.getResponseClass()), new AsyncCallback<>(future)); return Futures.makeChecked(future, new Function<Exception, UserException>(){ @Override public UserException apply(Exception input) { if(input instanceof ExecutionException){ input = (Exception) input.getCause(); } return handleException(input, action, listener); }}); }
@GwtIncompatible // makeChecked public void testMakeChecked_listenersRunOnFailure() throws Exception { SettableFuture<String> future = SettableFuture.create(); CheckedFuture<String, TestException> checked = makeChecked( future, new Function<Exception, TestException>() { @Override public TestException apply(Exception from) { throw new NullPointerException(); } }); ListenableFutureTester tester = new ListenableFutureTester(checked); tester.setUp(); future.setException(new Exception("failed")); tester.testFailedFuture("failed"); tester.tearDown(); }
@GwtIncompatible // makeChecked public void testMakeChecked_listenersRunOnceCompleted() throws Exception { SettableFuture<String> future = SettableFuture.create(); CheckedFuture<String, TestException> checked = makeChecked( future, new Function<Exception, TestException>() { @Override public TestException apply(Exception from) { throw new NullPointerException(); } }); ListenableFutureTester tester = new ListenableFutureTester(checked); tester.setUp(); future.set(DATA1); tester.testCompletedFuture(DATA1); tester.tearDown(); }
@GwtIncompatible // makeChecked public void testMakeChecked_listenersRunOnCancel() throws Exception { SettableFuture<String> future = SettableFuture.create(); CheckedFuture<String, TestException> checked = makeChecked( future, new Function<Exception, TestException>() { @Override public TestException apply(Exception from) { throw new NullPointerException(); } }); ListenableFutureTester tester = new ListenableFutureTester(checked); tester.setUp(); future.cancel(true); // argument is ignored tester.testCancelledFuture(); tester.tearDown(); }