/** Example of asynchronously listing metrics, specifying the page size. */ // [TARGET listMetricsAsync(ListOption...)] public Page<Metric> listMetricsAsync() throws ExecutionException, InterruptedException { // [START listMetricsAsync] Future<AsyncPage<Metric>> future = logging.listMetricsAsync(ListOption.pageSize(100)); // ... AsyncPage<Metric> metrics = future.get(); for (Metric metric : metrics.iterateAll()) { // do something with the metric } // [END listMetricsAsync] return metrics; }
@Test public void testListMetricsAsyncEmpty() throws ExecutionException, InterruptedException { EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).build(); List<Metric> sinkList = ImmutableList.of(); ListLogMetricsResponse response = ListLogMetricsResponse.newBuilder() .setNextPageToken("") .addAllMetrics(Lists.transform(sinkList, METRIC_TO_PB_FUNCTION)) .build(); ApiFuture<ListLogMetricsResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); AsyncPage<Metric> page = logging.listMetricsAsync().get(); assertNull(page.getNextPageToken()); assertNull(page.getNextPage()); assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Metric.class)); }
@Test public void testListMetricsAsync() throws ExecutionException, InterruptedException { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).build(); List<Metric> sinkList = ImmutableList.of( new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO))); ListLogMetricsResponse response = ListLogMetricsResponse.newBuilder() .setNextPageToken(cursor) .addAllMetrics(Lists.transform(sinkList, METRIC_TO_PB_FUNCTION)) .build(); ApiFuture<ListLogMetricsResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); AsyncPage<Metric> page = logging.listMetricsAsync().get(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Metric.class)); }
@Test public void testListMetricsWithOptionsAsync() throws ExecutionException, InterruptedException { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder() .setPageToken(cursor) .setPageSize(42) .setParent(PROJECT_PB) .build(); List<Metric> sinkList = ImmutableList.of( new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO))); ListLogMetricsResponse response = ListLogMetricsResponse.newBuilder() .setNextPageToken(cursor) .addAllMetrics(Lists.transform(sinkList, METRIC_TO_PB_FUNCTION)) .build(); ApiFuture<ListLogMetricsResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); AsyncPage<Metric> page = logging.listMetricsAsync(ListOption.pageSize(42), ListOption.pageToken(cursor)).get(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Metric.class)); }
EasyMock.expect(loggingRpcMock.list(request2)).andReturn(futureResponse2); EasyMock.replay(loggingRpcMock); AsyncPage<Metric> page = logging.listMetricsAsync().get(); assertEquals(cursor1, page.getNextPageToken()); assertArrayEquals(sinkList1.toArray(), Iterables.toArray(page.getValues(), Metric.class));