Search in sources :

Example 1 with MockResponseObserver

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);
}
Also used : MockResponseObserver(com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockResponseObserver) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerStreamingStashCallable(com.google.cloud.bigtable.gaxx.testing.FakeStreamingApi.ServerStreamingStashCallable) Test(org.junit.Test)

Example 2 with MockResponseObserver

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);
}
Also used : MockResponseObserver(com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockResponseObserver) MockStreamController(com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockStreamController) StreamController(com.google.api.gax.rpc.StreamController) Test(org.junit.Test)

Example 3 with MockResponseObserver

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();
}
Also used : MockResponseObserver(com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockResponseObserver) CountDownLatch(java.util.concurrent.CountDownLatch) MockServerStreamingCallable(com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockServerStreamingCallable) Test(org.junit.Test)

Aggregations

MockResponseObserver (com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockResponseObserver)3 Test (org.junit.Test)3 StreamController (com.google.api.gax.rpc.StreamController)1 ServerStreamingStashCallable (com.google.cloud.bigtable.gaxx.testing.FakeStreamingApi.ServerStreamingStashCallable)1 MockServerStreamingCallable (com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockServerStreamingCallable)1 MockStreamController (com.google.cloud.bigtable.gaxx.testing.MockStreamingApi.MockStreamController)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1