use of com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockResponseObserver in project java-bigtable by googleapis.
the class ReframingResponseObserverTest method testReframerPopError.
@Test
public void testReframerPopError() {
final AtomicInteger popCount = new AtomicInteger();
MockResponseObserver<String> outerObserver = new MockResponseObserver<>(true);
Reframer<String, String> reframer = new DasherizingReframer(1) {
@Override
public String pop() {
if (popCount.incrementAndGet() == 2) {
throw new IllegalStateException("fake error");
}
return super.pop();
}
};
ReframingResponseObserver<String, String> middleware = new ReframingResponseObserver<>(outerObserver, reframer);
ServerStreamingStashCallable<String, String> innerCallable = new ServerStreamingStashCallable<>(ImmutableList.of("a", "boom", "c"));
innerCallable.call("request", middleware);
StreamControllerStash<String> lastCall = innerCallable.popLastCall();
Truth.assertThat(outerObserver.getFinalError()).isInstanceOf(IllegalStateException.class);
Truth.assertThat(outerObserver.getFinalError()).hasMessageThat().isEqualTo("fake error");
Truth.assertThat(outerObserver.popNextResponse()).isEqualTo("a");
Truth.assertThat(outerObserver.popNextResponse()).isNull();
Truth.assertThat(popCount.get()).isEqualTo(2);
Truth.assertThat(lastCall.getError()).isInstanceOf(CancellationException.class);
Truth.assertThat(lastCall.getNumDelivered()).isEqualTo(2);
}
use of com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockResponseObserver in project java-bigtable by googleapis.
the class ReframingResponseObserverTest method testFailedRecoveryHandling.
/**
* Test the scenario where the reframer throws an exception on incoming data and the upstream
* throws an exception during cleanup when cancel is called.
*/
@Test
public void testFailedRecoveryHandling() {
MockResponseObserver<String> outerObserver = new MockResponseObserver<>(true);
final RuntimeException fakeReframerError = new RuntimeException("fake reframer error");
Reframer<String, String> brokenReframer = new Reframer<String, String>() {
@Override
public void push(String ignored) {
throw fakeReframerError;
}
@Override
public boolean hasFullFrame() {
return false;
}
@Override
public boolean hasPartialFrame() {
return false;
}
@Override
public String pop() {
throw new IllegalStateException("should not be called");
}
};
ReframingResponseObserver<String, String> middleware = new ReframingResponseObserver<>(outerObserver, brokenReframer);
// Configure the mock inner controller to fail cancellation.
StreamController mockInnerController = Mockito.mock(StreamController.class);
RuntimeException fakeCancelError = new RuntimeException("fake cancel error");
Mockito.doThrow(fakeCancelError).when(mockInnerController).cancel();
// Jumpstart a call & feed it data
middleware.onStartImpl(mockInnerController);
middleware.onResponseImpl("1");
// Make sure that the outer observer was notified with the reframer, which contains a suppressed
// cancellation error.
Throwable finalError = outerObserver.getFinalError();
Truth.assertThat(finalError).isSameInstanceAs(fakeReframerError);
Truth.assertThat(ImmutableList.of(finalError.getSuppressed())).hasSize(1);
Truth.assertThat(finalError.getSuppressed()[0]).isInstanceOf(IllegalStateException.class);
Truth.assertThat(finalError.getSuppressed()[0]).hasMessageThat().isEqualTo("Failed to cancel upstream while recovering from an unexpected error");
Truth.assertThat(finalError.getSuppressed()[0].getCause()).isSameInstanceAs(fakeCancelError);
}
use of com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockResponseObserver in project java-bigtable by googleapis.
the class ReframingResponseObserverTest method testConcurrentCancel.
@Test
public void testConcurrentCancel() throws InterruptedException {
final MockResponseObserver<String> outerObserver = new MockResponseObserver<>(true);
ReframingResponseObserver<String, String> middleware = new ReframingResponseObserver<>(outerObserver, new DasherizingReframer(2));
MockServerStreamingCallable<String, String> innerCallable = new MockServerStreamingCallable<>();
innerCallable.call("request", middleware);
MockServerStreamingCall<String, String> lastCall = innerCallable.popLastCall();
final MockStreamController<String> innerController = lastCall.getController();
final CountDownLatch latch = new CountDownLatch(2);
executor.submit(new Runnable() {
@Override
public void run() {
while (!outerObserver.isDone()) {
outerObserver.popNextResponse();
}
latch.countDown();
}
});
executor.submit(new Runnable() {
@Override
public void run() {
while (!innerController.isCancelled()) {
if (innerController.popLastPull() > 0) {
innerController.getObserver().onResponse("a");
}
}
innerController.getObserver().onError(new RuntimeException("Some other upstream error"));
latch.countDown();
}
});
outerObserver.getController().cancel();
Truth.assertThat(latch.await(1, TimeUnit.MINUTES)).isTrue();
}
Aggregations