/** Example of asynchronously listing sinks, specifying the page size. */ // [TARGET listSinksAsync(ListOption...)] public Page<Sink> listSinksAsync() throws ExecutionException, InterruptedException { // [START listSinksAsync] Future<AsyncPage<Sink>> future = logging.listSinksAsync(ListOption.pageSize(100)); // ... AsyncPage<Sink> sinks = future.get(); for (Sink sink : sinks.iterateAll()) { // do something with the sink } // [END listSinksAsync] return sinks; }
@Test public void testListSinksAsyncEmpty() throws ExecutionException, InterruptedException { EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListSinksRequest request = ListSinksRequest.newBuilder().setParent(PROJECT_PB).build(); List<Sink> sinkList = ImmutableList.of(); ListSinksResponse response = ListSinksResponse.newBuilder() .setNextPageToken("") .addAllSinks(Lists.transform(sinkList, SINK_TO_PB_FUNCTION)) .build(); ApiFuture<ListSinksResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); AsyncPage<Sink> page = logging.listSinksAsync().get(); assertNull(page.getNextPageToken()); assertNull(page.getNextPage()); assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Sink.class)); }
@Test public void testListSinksAsync() throws ExecutionException, InterruptedException { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListSinksRequest request = ListSinksRequest.newBuilder().setParent(PROJECT_PB).build(); List<Sink> sinkList = ImmutableList.of( new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)), new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO))); ListSinksResponse response = ListSinksResponse.newBuilder() .setNextPageToken(cursor) .addAllSinks(Lists.transform(sinkList, SINK_TO_PB_FUNCTION)) .build(); ApiFuture<ListSinksResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); AsyncPage<Sink> page = logging.listSinksAsync().get(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Sink.class)); }
@Test public void testListSinksWithOptionsAsync() throws ExecutionException, InterruptedException { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListSinksRequest request = ListSinksRequest.newBuilder() .setPageToken(cursor) .setPageSize(42) .setParent(PROJECT_PB) .build(); List<Sink> sinkList = ImmutableList.of( new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO)), new Sink(logging, new SinkInfo.BuilderImpl(SINK_INFO))); ListSinksResponse response = ListSinksResponse.newBuilder() .setNextPageToken(cursor) .addAllSinks(Lists.transform(sinkList, SINK_TO_PB_FUNCTION)) .build(); ApiFuture<ListSinksResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); AsyncPage<Sink> page = logging.listSinksAsync(ListOption.pageSize(42), ListOption.pageToken(cursor)).get(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Sink.class)); }
EasyMock.expect(loggingRpcMock.list(request2)).andReturn(futureResponse2); EasyMock.replay(loggingRpcMock); AsyncPage<Sink> page = logging.listSinksAsync().get(); assertEquals(cursor1, page.getNextPageToken()); assertArrayEquals(sinkList1.toArray(), Iterables.toArray(page.getValues(), Sink.class));