/** Example of asynchronously getting a metric. */ // [TARGET getMetricAsync(String)] // [VARIABLE "my_metric_name"] public Metric getMetricAsync(String metricName) throws ExecutionException, InterruptedException { // [START getMetricAsync] Future<Metric> future = logging.getMetricAsync(metricName); // ... Metric metric = future.get(); if (metric == null) { // metric was not found } // [END getMetricAsync] return metric; }
/** * Sends a request to fetch current metric's latest information. This method returns a {@code * ApiFuture} object to consume the result. {@link ApiFuture#get()} returns a {@code Metric} * object with latest information or {@code null} if not found. * * <p>Example of asynchronously getting the metric's latest information. * * <pre>{@code * ApiFuture<Metric> future = metric.reloadAsync(); * // ... * Metric latestMetric = future.get(); * if (latestMetric == null) { * // the metric was not found * } * }</pre> * * @throws LoggingException upon failure */ public ApiFuture<Metric> reloadAsync() { return logging.getMetricAsync(getName()); }
@Test public void testReloadAsyncNull() throws ExecutionException, InterruptedException { initializeExpectedMetric(1); expect(logging.getOptions()).andReturn(mockOptions); expect(logging.getMetricAsync(NAME)).andReturn(ApiFutures.<Metric>immediateFuture(null)); replay(logging); initializeMetric(); assertNull(metric.reloadAsync().get()); }
@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 testReloadAsync() throws ExecutionException, InterruptedException { initializeExpectedMetric(2); MetricInfo updatedInfo = METRIC_INFO.toBuilder().setFilter(NEW_FILTER).build(); Metric expectedMetric = new Metric(serviceMockReturnsOptions, new MetricInfo.BuilderImpl(updatedInfo)); expect(logging.getOptions()).andReturn(mockOptions); expect(logging.getMetricAsync(NAME)).andReturn(ApiFutures.immediateFuture(expectedMetric)); replay(logging); initializeMetric(); Metric updatedMetric = metric.reloadAsync().get(); compareMetric(expectedMetric, updatedMetric); }
@Test public void testGetMetricAsync() throws ExecutionException, InterruptedException { LogMetric sinkPb = METRIC_INFO.toPb(); ApiFuture<LogMetric> response = ApiFutures.immediateFuture(sinkPb); GetLogMetricRequest request = GetLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build(); EasyMock.expect(loggingRpcMock.get(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Metric sink = logging.getMetricAsync(METRIC_NAME).get(); assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), sink); }
/** * Sends a request to fetch current metric's latest information. This method returns a {@code * ApiFuture} object to consume the result. {@link ApiFuture#get()} returns a {@code Metric} * object with latest information or {@code null} if not found. * * <p>Example of asynchronously getting the metric's latest information. * * <pre>{@code * ApiFuture<Metric> future = metric.reloadAsync(); * // ... * Metric latestMetric = future.get(); * if (latestMetric == null) { * // the metric was not found * } * }</pre> * * @throws LoggingException upon failure */ public ApiFuture<Metric> reloadAsync() { return logging.getMetricAsync(getName()); }