private static <I, O> ApiFuture<O> transform( ApiFuture<I> future, final Function<? super I, ? extends O> function) { return ApiFutures.transform( future, new ApiFunction<I, O>() { @Override public O apply(I i) { return function.apply(i); } }); }
/** Converts an {@see ApiFuture} to a {@see ListenableFuture}. */ public static <T> ListenableFuture<T> apiFutureToListenableFuture(final ApiFuture<T> apiFuture) { SettableFuture<T> settableFuture = SettableFuture.create(); ApiFutures.addCallback( apiFuture, new ApiFutureCallback<T>() { @Override public void onFailure(Throwable t) { settableFuture.setException(t); } @Override public void onSuccess(T result) { settableFuture.set(result); } }); return settableFuture; }
@Test public void testGetMetricAsync_Null() throws ExecutionException, InterruptedException { ApiFuture<LogMetric> response = ApiFutures.immediateFuture(null); GetLogMetricRequest request = GetLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build(); EasyMock.expect(loggingRpcMock.get(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); assertNull(logging.getMetricAsync(METRIC_NAME).get()); }
@Test public void testDeleteMetric_Null() { DeleteLogMetricRequest request = DeleteLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build(); ApiFuture<Empty> response = ApiFutures.immediateFuture(null); EasyMock.expect(loggingRpcMock.delete(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); assertFalse(logging.deleteMetric(METRIC_NAME)); }
@Test public void testDeleteMetricAsync_Null() throws ExecutionException, InterruptedException { DeleteLogMetricRequest request = DeleteLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build(); ApiFuture<Empty> response = ApiFutures.immediateFuture(null); EasyMock.expect(loggingRpcMock.delete(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); assertFalse(logging.deleteMetricAsync(METRIC_NAME).get()); }
@Test public void testDeleteSink_Null() { DeleteSinkRequest request = DeleteSinkRequest.newBuilder().setSinkName(SINK_NAME_PB).build(); ApiFuture<Empty> response = ApiFutures.immediateFuture(null); EasyMock.expect(loggingRpcMock.delete(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); assertFalse(logging.deleteSink(SINK_NAME)); }
@Test public void testDeleteSinkAsync_Null() throws ExecutionException, InterruptedException { DeleteSinkRequest request = DeleteSinkRequest.newBuilder().setSinkName(SINK_NAME_PB).build(); ApiFuture<Empty> response = ApiFutures.immediateFuture(null); EasyMock.expect(loggingRpcMock.delete(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); assertFalse(logging.deleteSinkAsync(SINK_NAME).get()); }
@Test public void testDeleteLog_Null() { DeleteLogRequest request = DeleteLogRequest.newBuilder().setLogName(LOG_NAME_PB).build(); EasyMock.expect(loggingRpcMock.delete(request)) .andReturn(ApiFutures.<Empty>immediateFuture(null)); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); assertFalse(logging.deleteLog(LOG_NAME)); }
@Test public void testGetMetric_Null() { ApiFuture<LogMetric> response = ApiFutures.immediateFuture(null); GetLogMetricRequest request = GetLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build(); EasyMock.expect(loggingRpcMock.get(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); assertNull(logging.getMetric(METRIC_NAME)); }
@Test public void testGetSinkAsync_Null() throws ExecutionException, InterruptedException { ApiFuture<LogSink> response = ApiFutures.immediateFuture(null); GetSinkRequest request = GetSinkRequest.newBuilder().setSinkName(SINK_NAME_PB).build(); EasyMock.expect(loggingRpcMock.get(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); assertNull(logging.getSinkAsync(SINK_NAME).get()); }
@Test public void testDeleteLogAsync_Null() throws ExecutionException, InterruptedException { DeleteLogRequest request = DeleteLogRequest.newBuilder().setLogName(LOG_NAME_PB).build(); EasyMock.expect(loggingRpcMock.delete(request)) .andReturn(ApiFutures.<Empty>immediateFuture(null)); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); assertFalse(logging.deleteLogAsync(LOG_NAME).get()); }
@Test public void testGetSink() { LogSink sinkPb = SINK_INFO.toPb(PROJECT); ApiFuture<LogSink> response = ApiFutures.immediateFuture(sinkPb); GetSinkRequest request = GetSinkRequest.newBuilder().setSinkName(SINK_NAME_PB).build(); EasyMock.expect(loggingRpcMock.get(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Sink sink = logging.getSink(SINK_NAME); assertEquals(new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)), sink); }
@Test public void testUpdateSink() { LogSink sinkPb = SINK_INFO.toPb(PROJECT); ApiFuture<LogSink> response = ApiFutures.immediateFuture(sinkPb); UpdateSinkRequest request = UpdateSinkRequest.newBuilder().setSinkName(SINK_NAME_PB).setSink(sinkPb).build(); EasyMock.expect(loggingRpcMock.update(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Sink sink = logging.update(SINK_INFO); assertEquals(new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)), sink); }
@Test public void testCreateSink() { LogSink sinkPb = SINK_INFO.toPb(PROJECT); ApiFuture<LogSink> response = ApiFutures.immediateFuture(sinkPb); CreateSinkRequest request = CreateSinkRequest.newBuilder().setParent(PROJECT_PB).setSink(sinkPb).build(); EasyMock.expect(loggingRpcMock.create(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Sink sink = logging.create(SINK_INFO); assertEquals(new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)), sink); }
@Test public void testUpdateMetricAsync() throws ExecutionException, InterruptedException { LogMetric sinkPb = METRIC_INFO.toPb(); ApiFuture<LogMetric> response = ApiFutures.immediateFuture(sinkPb); UpdateLogMetricRequest request = UpdateLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).setMetric(sinkPb).build(); EasyMock.expect(loggingRpcMock.update(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Metric sink = logging.updateAsync(METRIC_INFO).get(); assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), sink); }
@Test public void testCreateSinkAsync() throws ExecutionException, InterruptedException { LogSink sinkPb = SINK_INFO.toPb(PROJECT); ApiFuture<LogSink> response = ApiFutures.immediateFuture(sinkPb); CreateSinkRequest request = CreateSinkRequest.newBuilder().setParent(PROJECT_PB).setSink(sinkPb).build(); EasyMock.expect(loggingRpcMock.create(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Sink sink = logging.createAsync(SINK_INFO).get(); assertEquals(new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)), sink); }
ApiFutures.catching( innerFuture, Throwable.class, attemptFailedCallback, MoreExecutors.directExecutor()); ApiFutures.transform(catching, attemptSuccessfulCallback, MoreExecutors.directExecutor()); externalFuture.setAttemptFuture(ApiFutures.<Void>immediateFailedFuture(e));
@Test public void testCheckFailure() throws Exception { GenerateConsistencyTokenRequest expectedRequest = GenerateConsistencyTokenRequest.newBuilder().setName(TABLE_NAME.toString()).build(); GenerateConsistencyTokenResponse expectedResponse = GenerateConsistencyTokenResponse.newBuilder().setConsistencyToken("fake-token").build(); Mockito.when(mockGenerateConsistencyTokenCallable.futureCall(expectedRequest, CALL_CONTEXT)) .thenReturn(ApiFutures.immediateFuture(expectedResponse)); CheckConsistencyRequest expectedRequest2 = CheckConsistencyRequest.newBuilder() .setName(TABLE_NAME.toString()) .setConsistencyToken("fake-token") .build(); FakeApiException expectedError = new FakeApiException("fake", null, Code.INTERNAL, false); Mockito.when(mockCheckConsistencyCallable.futureCall(expectedRequest2, CALL_CONTEXT)) .thenReturn(ApiFutures.<CheckConsistencyResponse>immediateFailedFuture(expectedError)); ApiFuture<Void> future = callable.futureCall(TABLE_NAME, CALL_CONTEXT); Throwable actualError = null; try { future.get(); } catch (ExecutionException e) { actualError = e.getCause(); } assertThat(actualError).isSameAs(expectedError); }
@Test public void testSuccess() { ApiFuture<String> future = ApiFutures.immediateFuture("result"); ApiFutures.addCallback( future, new TraceFinisher<String>(mockTracer), MoreExecutors.directExecutor()); verify(mockTracer, times(1)).operationSucceeded(); }