Search in sources :

Example 1 with SubscribeRequest

use of com.google.cloud.pubsublite.proto.SubscribeRequest in project java-pubsublite by googleapis.

the class SubscriberImplTest method reinitialize_handlesIgnoredReset.

@Test
public void reinitialize_handlesIgnoredReset() throws Exception {
    subscriber.allowFlow(FlowControlRequest.newBuilder().setAllowedBytes(100).setAllowedMessages(100).build());
    ImmutableList<SequencedMessage> messages = ImmutableList.of(SequencedMessage.of(Message.builder().build(), Timestamps.EPOCH, Offset.of(0), 10), SequencedMessage.of(Message.builder().build(), Timestamps.EPOCH, Offset.of(1), 10));
    CountDownLatch messagesReceived = countdownMessageBatches(1);
    leakedResponseObserver.onResponse(messages);
    assertThat(messagesReceived.await(10, SECONDS)).isTrue();
    verify(mockMessageConsumer).accept(messages);
    final SubscribeRequest nextOffsetRequest = SubscribeRequest.newBuilder().setInitial(BASE_INITIAL_SUBSCRIBE_REQUEST.toBuilder().setInitialLocation(SeekRequest.newBuilder().setCursor(Cursor.newBuilder().setOffset(2)))).build();
    doAnswer(args -> {
        leakedResponseObserver = args.getArgument(1);
        return mockConnectedSubscriber2;
    }).when(mockSubscriberFactory).New(any(), any(), eq(nextOffsetRequest));
    // If the RESET signal is received and subscriber reset is ignored, the subscriber should read
    // from the next offset upon reconnect.
    when(mockResetHandler.handleReset()).thenReturn(false);
    subscriber.triggerReinitialize(TestResetSignal.newCheckedException());
    verify(mockSubscriberFactory, times(1)).New(any(), any(), eq(initialRequest()));
    verify(mockSubscriberFactory, times(1)).New(any(), any(), eq(nextOffsetRequest));
    verify(mockConnectedSubscriber2).allowFlow(FlowControlRequest.newBuilder().setAllowedBytes(80).setAllowedMessages(98).build());
}
Also used : SubscribeRequest(com.google.cloud.pubsublite.proto.SubscribeRequest) InitialSubscribeRequest(com.google.cloud.pubsublite.proto.InitialSubscribeRequest) SequencedMessage(com.google.cloud.pubsublite.SequencedMessage) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with SubscribeRequest

use of com.google.cloud.pubsublite.proto.SubscribeRequest in project java-pubsublite by googleapis.

the class SubscriberImplTest method reinitialize_reconnectsToNextOffset.

@Test
public void reinitialize_reconnectsToNextOffset() throws Exception {
    subscriber.allowFlow(FlowControlRequest.newBuilder().setAllowedBytes(100).setAllowedMessages(100).build());
    ImmutableList<SequencedMessage> messages = ImmutableList.of(SequencedMessage.of(Message.builder().build(), Timestamps.EPOCH, Offset.of(0), 10), SequencedMessage.of(Message.builder().build(), Timestamps.EPOCH, Offset.of(1), 10));
    CountDownLatch messagesReceived = countdownMessageBatches(1);
    leakedResponseObserver.onResponse(messages);
    assertThat(messagesReceived.await(10, SECONDS)).isTrue();
    verify(mockMessageConsumer).accept(messages);
    final SubscribeRequest nextOffsetRequest = SubscribeRequest.newBuilder().setInitial(BASE_INITIAL_SUBSCRIBE_REQUEST.toBuilder().setInitialLocation(SeekRequest.newBuilder().setCursor(Cursor.newBuilder().setOffset(2)))).build();
    doAnswer(args -> {
        leakedResponseObserver = args.getArgument(1);
        return mockConnectedSubscriber2;
    }).when(mockSubscriberFactory).New(any(), any(), eq(nextOffsetRequest));
    subscriber.triggerReinitialize(new CheckedApiException(Code.ABORTED));
    verify(mockSubscriberFactory, times(1)).New(any(), any(), eq(initialRequest()));
    verify(mockSubscriberFactory, times(1)).New(any(), any(), eq(nextOffsetRequest));
    verify(mockConnectedSubscriber2).allowFlow(FlowControlRequest.newBuilder().setAllowedBytes(80).setAllowedMessages(98).build());
}
Also used : SubscribeRequest(com.google.cloud.pubsublite.proto.SubscribeRequest) InitialSubscribeRequest(com.google.cloud.pubsublite.proto.InitialSubscribeRequest) SequencedMessage(com.google.cloud.pubsublite.SequencedMessage) CheckedApiException(com.google.cloud.pubsublite.internal.CheckedApiException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with SubscribeRequest

use of com.google.cloud.pubsublite.proto.SubscribeRequest in project java-pubsublite by googleapis.

the class SubscriberServiceClientTest method subscribeTest.

@Test
public void subscribeTest() throws Exception {
    SubscribeResponse expectedResponse = SubscribeResponse.newBuilder().build();
    mockSubscriberService.addResponse(expectedResponse);
    SubscribeRequest request = SubscribeRequest.newBuilder().build();
    MockStreamObserver<SubscribeResponse> responseObserver = new MockStreamObserver<>();
    BidiStreamingCallable<SubscribeRequest, SubscribeResponse> callable = client.subscribeCallable();
    ApiStreamObserver<SubscribeRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
    requestObserver.onNext(request);
    requestObserver.onCompleted();
    List<SubscribeResponse> actualResponses = responseObserver.future().get();
    Assert.assertEquals(1, actualResponses.size());
    Assert.assertEquals(expectedResponse, actualResponses.get(0));
}
Also used : SubscribeRequest(com.google.cloud.pubsublite.proto.SubscribeRequest) SubscribeResponse(com.google.cloud.pubsublite.proto.SubscribeResponse) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) Test(org.junit.Test)

Example 4 with SubscribeRequest

use of com.google.cloud.pubsublite.proto.SubscribeRequest in project java-pubsublite by googleapis.

the class SubscriberServiceClientTest method subscribeExceptionTest.

@Test
public void subscribeExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockSubscriberService.addException(exception);
    SubscribeRequest request = SubscribeRequest.newBuilder().build();
    MockStreamObserver<SubscribeResponse> responseObserver = new MockStreamObserver<>();
    BidiStreamingCallable<SubscribeRequest, SubscribeResponse> callable = client.subscribeCallable();
    ApiStreamObserver<SubscribeRequest> requestObserver = callable.bidiStreamingCall(responseObserver);
    requestObserver.onNext(request);
    try {
        List<SubscribeResponse> actualResponses = responseObserver.future().get();
        Assert.fail("No exception thrown");
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof InvalidArgumentException);
        InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
        Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) SubscribeRequest(com.google.cloud.pubsublite.proto.SubscribeRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) SubscribeResponse(com.google.cloud.pubsublite.proto.SubscribeResponse) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

SubscribeRequest (com.google.cloud.pubsublite.proto.SubscribeRequest)4 Test (org.junit.Test)4 MockStreamObserver (com.google.api.gax.grpc.testing.MockStreamObserver)2 SequencedMessage (com.google.cloud.pubsublite.SequencedMessage)2 InitialSubscribeRequest (com.google.cloud.pubsublite.proto.InitialSubscribeRequest)2 SubscribeResponse (com.google.cloud.pubsublite.proto.SubscribeResponse)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)1 CheckedApiException (com.google.cloud.pubsublite.internal.CheckedApiException)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 ExecutionException (java.util.concurrent.ExecutionException)1