Search in sources :

Example 1 with CheckedApiException

use of com.google.cloud.pubsublite.internal.CheckedApiException in project beam by apache.

the class PubsubLiteSinkTest method exceptionMixedWithOK.

@Test
public void exceptionMixedWithOK() throws Exception {
    Message message1 = Message.builder().build();
    Message message2 = Message.builder().setKey(ByteString.copyFromUtf8("abc")).build();
    Message message3 = Message.builder().setKey(ByteString.copyFromUtf8("def")).build();
    SettableApiFuture<MessageMetadata> future1 = SettableApiFuture.create();
    SettableApiFuture<MessageMetadata> future2 = SettableApiFuture.create();
    SettableApiFuture<MessageMetadata> future3 = SettableApiFuture.create();
    CountDownLatch startedLatch = new CountDownLatch(3);
    when(publisher.publish(message1)).then(invocation -> {
        startedLatch.countDown();
        return future1;
    });
    when(publisher.publish(message2)).then(invocation -> {
        startedLatch.countDown();
        return future2;
    });
    when(publisher.publish(message3)).then(invocation -> {
        startedLatch.countDown();
        return future3;
    });
    ExecutorService exec = Executors.newCachedThreadPool();
    exec.execute(() -> {
        try {
            startedLatch.await();
            future1.set(MessageMetadata.of(Partition.of(1), Offset.of(2)));
            future2.setException(new CheckedApiException(Code.INTERNAL).underlying);
            future3.set(MessageMetadata.of(Partition.of(1), Offset.of(3)));
        } catch (InterruptedException e) {
            fail();
            throw new RuntimeException(e);
        }
    });
    PipelineExecutionException e = assertThrows(PipelineExecutionException.class, () -> runWith(message1, message2, message3));
    verify(publisher, times(3)).publish(publishedMessageCaptor.capture());
    assertThat(publishedMessageCaptor.getAllValues(), containsInAnyOrder(message1, message2, message3));
    Optional<CheckedApiException> statusOr = ExtractStatus.extract(e.getCause());
    assertTrue(statusOr.isPresent());
    assertThat(statusOr.get().code(), equalTo(Code.INTERNAL));
    exec.shutdownNow();
}
Also used : MessageMetadata(com.google.cloud.pubsublite.MessageMetadata) Message(com.google.cloud.pubsublite.Message) PipelineExecutionException(org.apache.beam.sdk.Pipeline.PipelineExecutionException) ExecutorService(java.util.concurrent.ExecutorService) CheckedApiException(com.google.cloud.pubsublite.internal.CheckedApiException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with CheckedApiException

use of com.google.cloud.pubsublite.internal.CheckedApiException in project beam by apache.

the class SubscriptionPartitionProcessorImplTest method allowFlowFailureFails.

@Test
public void allowFlowFailureFails() throws Exception {
    when(tracker.currentRestriction()).thenReturn(initialRange());
    when(tracker.tryClaim(any())).thenReturn(true);
    doThrow(new CheckedApiException(Code.OUT_OF_RANGE)).when(subscriber).allowFlow(any());
    SystemExecutors.getFuturesExecutor().execute(() -> leakedConsumer.accept(ImmutableList.of(messageWithOffset(1))));
    ApiException e = assertThrows(ApiException.class, () -> processor.runFor(Duration.standardHours(10)));
    assertEquals(Code.OUT_OF_RANGE, e.getStatusCode().getCode());
}
Also used : CheckedApiException(com.google.cloud.pubsublite.internal.CheckedApiException) CheckedApiException(com.google.cloud.pubsublite.internal.CheckedApiException) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Example 3 with CheckedApiException

use of com.google.cloud.pubsublite.internal.CheckedApiException in project beam by apache.

the class SubscriptionPartitionProcessorImplTest method lifecycleFlowControlThrows.

@Test
public void lifecycleFlowControlThrows() throws Exception {
    when(tracker.currentRestriction()).thenReturn(initialRange());
    doThrow(new CheckedApiException(Code.OUT_OF_RANGE)).when(subscriber).allowFlow(any());
    assertThrows(ApiException.class, () -> processor.runFor(Duration.ZERO));
}
Also used : CheckedApiException(com.google.cloud.pubsublite.internal.CheckedApiException) Test(org.junit.Test)

Example 4 with CheckedApiException

use of com.google.cloud.pubsublite.internal.CheckedApiException in project beam by apache.

the class SubscriptionPartitionProcessorImplTest method subscriberFailureFails.

@Test
public void subscriberFailureFails() throws Exception {
    when(tracker.currentRestriction()).thenReturn(initialRange());
    doAnswer((Answer<Void>) args -> {
        subscriber.fail(new CheckedApiException(Code.OUT_OF_RANGE));
        return null;
    }).when(subscriber).awaitRunning();
    ApiException e = assertThrows(// Longer wait is needed due to listener asynchrony, but should never wait this long.
    ApiException.class, () -> processor.runFor(Duration.standardMinutes(2)));
    assertEquals(Code.OUT_OF_RANGE, e.getStatusCode().getCode());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) FlowControlRequest(com.google.cloud.pubsublite.proto.FlowControlRequest) Mock(org.mockito.Mock) Assert.assertThrows(org.junit.Assert.assertThrows) Duration(org.joda.time.Duration) RunWith(org.junit.runner.RunWith) ProcessContinuation(org.apache.beam.sdk.transforms.DoFn.ProcessContinuation) Function(java.util.function.Function) Mockito.doThrow(org.mockito.Mockito.doThrow) Answer(org.mockito.stubbing.Answer) SequencedMessage(com.google.cloud.pubsublite.proto.SequencedMessage) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Spy(org.mockito.Spy) Timeout(org.junit.rules.Timeout) OutputReceiver(org.apache.beam.sdk.transforms.DoFn.OutputReceiver) RestrictionTracker(org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker) Before(org.junit.Before) InOrder(org.mockito.InOrder) CheckedApiException(com.google.cloud.pubsublite.internal.CheckedApiException) UnitTestExamples.example(com.google.cloud.pubsublite.internal.testing.UnitTestExamples.example) Assert.assertNotNull(org.junit.Assert.assertNotNull) Offset(com.google.cloud.pubsublite.Offset) DEFAULT_FLOW_CONTROL(org.apache.beam.sdk.io.gcp.pubsublite.SubscriberOptions.DEFAULT_FLOW_CONTROL) Mockito.times(org.mockito.Mockito.times) Timestamps(com.google.protobuf.util.Timestamps) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JUnit4(org.junit.runners.JUnit4) Subscriber(com.google.cloud.pubsublite.internal.wire.Subscriber) ApiException(com.google.api.gax.rpc.ApiException) Mockito.verify(org.mockito.Mockito.verify) SettableApiFuture(com.google.api.core.SettableApiFuture) Consumer(java.util.function.Consumer) List(java.util.List) FakeApiService(com.google.cloud.pubsublite.internal.testing.FakeApiService) Rule(org.junit.Rule) Code(com.google.api.gax.rpc.StatusCode.Code) Assert.assertFalse(org.junit.Assert.assertFalse) Mockito.inOrder(org.mockito.Mockito.inOrder) Instant(org.joda.time.Instant) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Cursor(com.google.cloud.pubsublite.proto.Cursor) OffsetRange(org.apache.beam.sdk.io.range.OffsetRange) SystemExecutors(com.google.cloud.pubsublite.internal.wire.SystemExecutors) Assert.assertEquals(org.junit.Assert.assertEquals) CheckedApiException(com.google.cloud.pubsublite.internal.CheckedApiException) CheckedApiException(com.google.cloud.pubsublite.internal.CheckedApiException) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Example 5 with CheckedApiException

use of com.google.cloud.pubsublite.internal.CheckedApiException in project beam by apache.

the class PubsubLiteSinkTest method singleExceptionWhenProcessing.

@Test
public void singleExceptionWhenProcessing() {
    Message message1 = Message.builder().build();
    when(publisher.publish(message1)).thenReturn(ApiFutures.immediateFailedFuture(new CheckedApiException(Code.INTERNAL).underlying));
    PipelineExecutionException e = assertThrows(PipelineExecutionException.class, () -> runWith(message1));
    verify(publisher).publish(message1);
    Optional<CheckedApiException> statusOr = ExtractStatus.extract(e.getCause());
    assertTrue(statusOr.isPresent());
    assertThat(statusOr.get().code(), equalTo(Code.INTERNAL));
}
Also used : Message(com.google.cloud.pubsublite.Message) PipelineExecutionException(org.apache.beam.sdk.Pipeline.PipelineExecutionException) CheckedApiException(com.google.cloud.pubsublite.internal.CheckedApiException) Test(org.junit.Test)

Aggregations

CheckedApiException (com.google.cloud.pubsublite.internal.CheckedApiException)7 Test (org.junit.Test)6 ApiException (com.google.api.gax.rpc.ApiException)3 Offset (com.google.cloud.pubsublite.Offset)3 Message (com.google.cloud.pubsublite.Message)2 PipelineExecutionException (org.apache.beam.sdk.Pipeline.PipelineExecutionException)2 Instant (org.joda.time.Instant)2 SettableApiFuture (com.google.api.core.SettableApiFuture)1 Code (com.google.api.gax.rpc.StatusCode.Code)1 MessageMetadata (com.google.cloud.pubsublite.MessageMetadata)1 Partition (com.google.cloud.pubsublite.Partition)1 TopicPath (com.google.cloud.pubsublite.TopicPath)1 FakeApiService (com.google.cloud.pubsublite.internal.testing.FakeApiService)1 UnitTestExamples.example (com.google.cloud.pubsublite.internal.testing.UnitTestExamples.example)1 Subscriber (com.google.cloud.pubsublite.internal.wire.Subscriber)1 SystemExecutors (com.google.cloud.pubsublite.internal.wire.SystemExecutors)1 Cursor (com.google.cloud.pubsublite.proto.Cursor)1 FlowControlRequest (com.google.cloud.pubsublite.proto.FlowControlRequest)1 SequencedMessage (com.google.cloud.pubsublite.proto.SequencedMessage)1 Timestamps (com.google.protobuf.util.Timestamps)1