SpoolingCallable extends UnaryCallable<RequestT, List<ResponseT>> { @Override public ApiFuture<List<ResponseT>> futureCall(RequestT request, ApiCallContext context) { SpoolingResponseObserver<ResponseT> observer = new SpoolingResponseObserver<>(); streamingCallable.call(request, observer, context); return observer.getFuture(); } SpoolingCallable(ServerStreamingCallable<RequestT, ResponseT> streamingCallable); @Override ApiFuture<List<ResponseT>> futureCall(RequestT request, ApiCallContext context);  }
@Test public void testHappyPath() throws InterruptedException, ExecutionException { ApiFuture<List<String>> result = callable.futureCall("request"); MockServerStreamingCall<String, String> call = upstream.popLastCall(); Truth.assertThat(call.getController().isAutoFlowControlEnabled()).isTrue(); call.getController().getObserver().onResponse("response1"); call.getController().getObserver().onResponse("response2"); call.getController().getObserver().onComplete(); Truth.assertThat(result.get()).containsExactly("response1", "response2").inOrder(); }
@Test public void testEarlyTermination() throws Exception { ApiFuture<List<String>> result = callable.futureCall("request"); MockServerStreamingCall<String, String> call = upstream.popLastCall(); call.getController().getObserver().onResponse("response1"); result.cancel(true); call.getController().getObserver().onResponse("response2"); Truth.assertThat(call.getController().isCancelled()).isTrue(); call.getController() .getObserver() .onError(new RuntimeException("Some other upstream cancellation indicator")); try { result.get(); Assert.fail("Callable should have thrown an exception"); } catch (CancellationException expected) { Truth.assertThat(expected).hasMessageThat().contains("Task was cancelled."); } }
@Test public void testNoResults() throws Exception { ApiFuture<List<String>> result = callable.futureCall("request"); MockServerStreamingCall<String, String> call = upstream.popLastCall(); call.getController().getObserver().onComplete(); Truth.assertThat(result.get()).isEmpty(); }