Search in sources :

Example 1 with Builder

use of com.google.cloud.pubsub.spi.v1.Publisher.Builder in project google-cloud-java by GoogleCloudPlatform.

the class PublisherImplTest method testBuilderParametersAndDefaults.

@Test
public void testBuilderParametersAndDefaults() {
    Publisher.Builder builder = Publisher.defaultBuilder(TEST_TOPIC);
    assertEquals(TEST_TOPIC, builder.topicName);
    assertEquals(Publisher.Builder.DEFAULT_EXECUTOR_PROVIDER, builder.executorProvider);
    assertEquals(LimitExceededBehavior.Block, builder.flowControlSettings.getLimitExceededBehavior());
    assertEquals(Publisher.Builder.DEFAULT_REQUEST_BYTES_THRESHOLD, builder.batchingSettings.getRequestByteThreshold().longValue());
    assertEquals(Publisher.Builder.DEFAULT_DELAY_THRESHOLD, builder.batchingSettings.getDelayThreshold());
    assertEquals(Publisher.Builder.DEFAULT_ELEMENT_COUNT_THRESHOLD, builder.batchingSettings.getElementCountThreshold().longValue());
    assertEquals(FlowControlSettings.getDefaultInstance(), builder.flowControlSettings);
    assertEquals(Publisher.Builder.DEFAULT_RETRY_SETTINGS, builder.retrySettings);
}
Also used : Builder(com.google.cloud.pubsub.spi.v1.Publisher.Builder) Test(org.junit.Test)

Example 2 with Builder

use of com.google.cloud.pubsub.spi.v1.Publisher.Builder in project google-cloud-java by GoogleCloudPlatform.

the class PublisherImplTest method testBuilderInvalidArguments.

@Test
public void testBuilderInvalidArguments() {
    Publisher.Builder builder = Publisher.defaultBuilder(TEST_TOPIC);
    try {
        builder.setChannelProvider(null);
        fail("Should have thrown an IllegalArgumentException");
    } catch (NullPointerException expected) {
    // Expected
    }
    try {
        builder.setExecutorProvider(null);
        fail("Should have thrown an IllegalArgumentException");
    } catch (NullPointerException expected) {
    // Expected
    }
    try {
        builder.setBatchingSettings(Publisher.Builder.DEFAULT_BATCHING_SETTINGS.toBuilder().setRequestByteThreshold((Long) null).build());
        fail("Should have thrown an NullPointerException");
    } catch (NullPointerException expected) {
    // Expected
    }
    try {
        builder.setBatchingSettings(Publisher.Builder.DEFAULT_BATCHING_SETTINGS.toBuilder().setRequestByteThreshold(0L).build());
        fail("Should have thrown an IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    // Expected
    }
    try {
        builder.setBatchingSettings(Publisher.Builder.DEFAULT_BATCHING_SETTINGS.toBuilder().setRequestByteThreshold(-1L).build());
        fail("Should have thrown an IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    // Expected
    }
    builder.setBatchingSettings(Publisher.Builder.DEFAULT_BATCHING_SETTINGS.toBuilder().setDelayThreshold(Duration.ofMillis(1)).build());
    try {
        builder.setBatchingSettings(Publisher.Builder.DEFAULT_BATCHING_SETTINGS.toBuilder().setDelayThreshold(null).build());
        fail("Should have thrown an NullPointerException");
    } catch (NullPointerException expected) {
    // Expected
    }
    try {
        builder.setBatchingSettings(Publisher.Builder.DEFAULT_BATCHING_SETTINGS.toBuilder().setDelayThreshold(Duration.ofMillis(-1)).build());
        fail("Should have thrown an IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    // Expected
    }
    builder.setBatchingSettings(Publisher.Builder.DEFAULT_BATCHING_SETTINGS.toBuilder().setElementCountThreshold(1L).build());
    try {
        builder.setBatchingSettings(Publisher.Builder.DEFAULT_BATCHING_SETTINGS.toBuilder().setElementCountThreshold((Long) null).build());
        fail("Should have thrown an NullPointerException");
    } catch (NullPointerException expected) {
    // Expected
    }
    try {
        builder.setBatchingSettings(Publisher.Builder.DEFAULT_BATCHING_SETTINGS.toBuilder().setElementCountThreshold(0L).build());
        fail("Should have thrown an IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    // Expected
    }
    try {
        builder.setBatchingSettings(Publisher.Builder.DEFAULT_BATCHING_SETTINGS.toBuilder().setElementCountThreshold(-1L).build());
        fail("Should have thrown an IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    // Expected
    }
    builder.setFlowControlSettings(FlowControlSettings.getDefaultInstance().toBuilder().setMaxOutstandingRequestBytes(1).build());
    try {
        builder.setFlowControlSettings(FlowControlSettings.getDefaultInstance().toBuilder().setMaxOutstandingRequestBytes(0).build());
        fail("Should have thrown an IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    // Expected
    }
    try {
        builder.setFlowControlSettings(FlowControlSettings.getDefaultInstance().toBuilder().setMaxOutstandingRequestBytes(-1).build());
        fail("Should have thrown an IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    // Expected
    }
    builder.setFlowControlSettings(FlowControlSettings.getDefaultInstance().toBuilder().setMaxOutstandingElementCount(1).build());
    try {
        builder.setFlowControlSettings(FlowControlSettings.getDefaultInstance().toBuilder().setMaxOutstandingElementCount(0).build());
        fail("Should have thrown an IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    // Expected
    }
    try {
        builder.setFlowControlSettings(FlowControlSettings.getDefaultInstance().toBuilder().setMaxOutstandingElementCount(-1).build());
        fail("Should have thrown an IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    // Expected
    }
    builder.setRetrySettings(Publisher.Builder.DEFAULT_RETRY_SETTINGS.toBuilder().setInitialRpcTimeout(Publisher.Builder.MIN_RPC_TIMEOUT).build());
    try {
        builder.setRetrySettings(Publisher.Builder.DEFAULT_RETRY_SETTINGS.toBuilder().setInitialRpcTimeout(Publisher.Builder.MIN_RPC_TIMEOUT.minusMillis(1)).build());
        fail("Should have thrown an IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    // Expected
    }
    builder.setRetrySettings(Publisher.Builder.DEFAULT_RETRY_SETTINGS.toBuilder().setTotalTimeout(Publisher.Builder.MIN_TOTAL_TIMEOUT).build());
    try {
        builder.setRetrySettings(Publisher.Builder.DEFAULT_RETRY_SETTINGS.toBuilder().setTotalTimeout(Publisher.Builder.MIN_TOTAL_TIMEOUT.minusMillis(1)).build());
        fail("Should have thrown an IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    // Expected
    }
}
Also used : Builder(com.google.cloud.pubsub.spi.v1.Publisher.Builder) Test(org.junit.Test)

Example 3 with Builder

use of com.google.cloud.pubsub.spi.v1.Publisher.Builder in project google-cloud-java by GoogleCloudPlatform.

the class StreamingSubscriberConnection method partitionAckOperations.

@VisibleForTesting
static List<StreamingPullRequest> partitionAckOperations(List<String> acksToSend, List<PendingModifyAckDeadline> ackDeadlineExtensions, int size) {
    int numExtensions = 0;
    for (PendingModifyAckDeadline modify : ackDeadlineExtensions) {
        numExtensions += modify.ackIds.size();
    }
    int numChanges = Math.max(numExtensions, acksToSend.size());
    int numRequests = numChanges / size + (numChanges % size == 0 ? 0 : 1);
    List<StreamingPullRequest.Builder> requests = new ArrayList<>(numRequests);
    for (int i = 0; i < numRequests; i++) {
        requests.add(StreamingPullRequest.newBuilder());
    }
    int reqCount = 0;
    for (List<String> acksChunk : Lists.partition(acksToSend, size)) {
        requests.get(reqCount).addAllAckIds(acksChunk);
        reqCount++;
    }
    reqCount = 0;
    int ackCount = 0;
    for (PendingModifyAckDeadline modify : ackDeadlineExtensions) {
        for (String ackId : modify.ackIds) {
            requests.get(reqCount).addModifyDeadlineSeconds(modify.deadlineExtensionSeconds).addModifyDeadlineAckIds(ackId);
            ackCount++;
            if (ackCount == size) {
                reqCount++;
                ackCount = 0;
            }
        }
    }
    List<StreamingPullRequest> ret = new ArrayList<>(requests.size());
    for (StreamingPullRequest.Builder builder : requests) {
        ret.add(builder.build());
    }
    return ret;
}
Also used : ArrayList(java.util.ArrayList) StreamingPullRequest(com.google.pubsub.v1.StreamingPullRequest) PendingModifyAckDeadline(com.google.cloud.pubsub.spi.v1.MessageDispatcher.PendingModifyAckDeadline) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with Builder

use of com.google.cloud.pubsub.spi.v1.Publisher.Builder in project google-cloud-java by GoogleCloudPlatform.

the class PublisherImplTest method testPublisherGetters.

@Test
public void testPublisherGetters() throws Exception {
    Publisher.Builder builder = Publisher.defaultBuilder(TEST_TOPIC);
    builder.setChannelProvider(TEST_CHANNEL_PROVIDER);
    builder.setExecutorProvider(SINGLE_THREAD_EXECUTOR);
    builder.setBatchingSettings(BatchingSettings.newBuilder().setRequestByteThreshold(10L).setDelayThreshold(Duration.ofMillis(11)).setElementCountThreshold(12L).build());
    builder.setCredentialsProvider(NO_CREDENTIALS_PROVIDER);
    builder.setFlowControlSettings(FlowControlSettings.newBuilder().setMaxOutstandingRequestBytes(13).setMaxOutstandingElementCount(14).setLimitExceededBehavior(LimitExceededBehavior.ThrowException).build());
    Publisher publisher = builder.build();
    assertEquals(TEST_TOPIC, publisher.getTopicName());
    assertEquals(10, (long) publisher.getBatchingSettings().getRequestByteThreshold());
    assertEquals(Duration.ofMillis(11), publisher.getBatchingSettings().getDelayThreshold());
    assertEquals(12, (long) publisher.getBatchingSettings().getElementCountThreshold());
    assertEquals(13, (long) publisher.getFlowControlSettings().getMaxOutstandingRequestBytes());
    assertEquals(14, (long) publisher.getFlowControlSettings().getMaxOutstandingElementCount());
    assertEquals(LimitExceededBehavior.ThrowException, publisher.getFlowControlSettings().getLimitExceededBehavior());
    publisher.shutdown();
}
Also used : Builder(com.google.cloud.pubsub.spi.v1.Publisher.Builder) Test(org.junit.Test)

Aggregations

Builder (com.google.cloud.pubsub.spi.v1.Publisher.Builder)3 Test (org.junit.Test)3 PendingModifyAckDeadline (com.google.cloud.pubsub.spi.v1.MessageDispatcher.PendingModifyAckDeadline)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 StreamingPullRequest (com.google.pubsub.v1.StreamingPullRequest)1 ArrayList (java.util.ArrayList)1