@Test public void testGenerateFailure() throws Exception { GenerateConsistencyTokenRequest expectedRequest = GenerateConsistencyTokenRequest.newBuilder().setName(TABLE_NAME.toString()).build(); FakeApiException fakeError = new FakeApiException("fake", null, Code.INTERNAL, false); Mockito.when(mockGenerateConsistencyTokenCallable.futureCall(expectedRequest, CALL_CONTEXT)) .thenReturn(ApiFutures.<GenerateConsistencyTokenResponse>immediateFailedFuture(fakeError)); ApiFuture<Void> future = callable.futureCall(TABLE_NAME, CALL_CONTEXT); Throwable actualError = null; try { future.get(); } catch (ExecutionException e) { actualError = e.getCause(); } assertThat(actualError).isSameAs(fakeError); }
@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 @SuppressWarnings("ConstantConditions") public void testInitialRetry() { resumptionStrategy = new MyStreamResumptionStrategy(); ServerStreamingAttemptCallable<String, String> callable = createCallable(); callable.start(); MockServerStreamingCall<String, String> call = innerCallable.popLastCall(); // Send initial error FakeApiException initialError = new FakeApiException(null, Code.UNAVAILABLE, true); call.getController().getObserver().onError(initialError); // Should notify the outer future Throwable outerError = null; try { fakeRetryingFuture.getAttemptResult().get(1, TimeUnit.SECONDS); } catch (ExecutionException e) { outerError = e.getCause(); } catch (Throwable e) { outerError = e; } Truth.assertThat(outerError).isInstanceOf(ServerStreamingAttemptException.class); Truth.assertThat(((ServerStreamingAttemptException) outerError).hasSeenResponses()).isFalse(); Truth.assertThat(((ServerStreamingAttemptException) outerError).canResume()).isTrue(); Truth.assertThat(outerError.getCause()).isEqualTo(initialError); // Make the retry call callable.call(); call = innerCallable.popLastCall(); // Verify the request and send a response Truth.assertThat(call.getRequest()).isEqualTo("request > 0"); }
@Test public void testRequestCountIsPreserved() { observer = new AccumulatingObserver(false); ServerStreamingAttemptCallable<String, String> callable = createCallable(); callable.start(); observer.controller.request(5); Truth.assertThat(observer.controller).isNotNull(); MockServerStreamingCall<String, String> call = innerCallable.popLastCall(); Truth.assertThat(call).isNotNull(); Truth.assertThat(call.getController().isAutoFlowControlEnabled()).isFalse(); Truth.assertThat(call.getController().popLastPull()).isEqualTo(5); // decrement call.getController().getObserver().onResponse("response"); // and then error call.getController() .getObserver() .onError(new FakeApiException(null, Code.UNAUTHENTICATED, true)); // Make the retry call callable.call(); call = innerCallable.popLastCall(); // Verify that the count is correct Truth.assertThat(call.getController().popLastPull()).isEqualTo(4); }
call.getController().getObserver().onResponse("response2"); FakeApiException innerError = new FakeApiException(null, Code.UNAVAILABLE, true); call.getController().getObserver().onError(innerError);
@Test public void testResponseSubstitution() { resumptionStrategy = new MyStreamResumptionStrategy() { @Override public String processResponse(String response) { return super.processResponse(response) + "+suffix"; } }; observer = new AccumulatingObserver(false); ServerStreamingAttemptCallable<String, String> callable = createCallable(); callable.start(); MockServerStreamingCall<String, String> call = innerCallable.popLastCall(); // Send initial response & then error call.getController().getObserver().onResponse("first"); call.getController().getObserver().onError(new FakeApiException(null, Code.UNAVAILABLE, true)); // Make the retry call callable.call(); call = innerCallable.popLastCall(); // Send another couple of responses (the first one will be ignored) call.getController().getObserver().onResponse("second"); call.getController().getObserver().onResponse("third"); call.getController().getObserver().onComplete(); // Verify the request and send a response Truth.assertThat(observer.responses) .containsExactly("first+suffix", "second+suffix", "third+suffix"); }