Search in sources :

Example 21 with DefaultSubscriberFactory

use of com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory in project spring-cloud-gcp by GoogleCloudPlatform.

the class GcpPubSubAutoConfigurationTests method retrySettings_selectiveConfigurationSet.

@Test
void retrySettings_selectiveConfigurationSet() {
    ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpPubSubAutoConfiguration.class)).withPropertyValues("spring.cloud.gcp.pubsub.subscription.subscription-name.retry.total-timeout-seconds=1", "spring.cloud.gcp.pubsub.subscription.subscription-name.retry.initial-retry-delay-seconds=2", "spring.cloud.gcp.pubsub.subscription.subscription-name.retry.retry-delay-multiplier=3", "spring.cloud.gcp.pubsub.subscription.subscription-name.retry.max-retry-delay-seconds=4", "spring.cloud.gcp.pubsub.subscription.subscription-name.retry.max-attempts=5", "spring.cloud.gcp.pubsub.subscription.subscription-name.retry.jittered=true", "spring.cloud.gcp.pubsub.subscription.subscription-name.retry.initial-rpc-timeout-seconds=6", "spring.cloud.gcp.pubsub.subscription.subscription-name.retry.rpc-timeout-multiplier=7", "spring.cloud.gcp.pubsub.subscription.subscription-name.retry.max-rpc-timeout-seconds=8").withUserConfiguration(TestConfig.class);
    contextRunner.run(ctx -> {
        GcpPubSubProperties gcpPubSubProperties = ctx.getBean(GcpPubSubProperties.class);
        GcpProjectIdProvider projectIdProvider = ctx.getBean(GcpProjectIdProvider.class);
        PubSubConfiguration.Retry retrySettings = gcpPubSubProperties.computeSubscriberRetrySettings("subscription-name", projectIdProvider.getProjectId());
        assertThat(retrySettings.getTotalTimeoutSeconds()).isEqualTo(1L);
        assertThat(retrySettings.getInitialRetryDelaySeconds()).isEqualTo(2L);
        assertThat(retrySettings.getRetryDelayMultiplier()).isEqualTo(3);
        assertThat(retrySettings.getMaxRetryDelaySeconds()).isEqualTo(4);
        assertThat(retrySettings.getMaxAttempts()).isEqualTo(5);
        assertThat(retrySettings.getJittered()).isTrue();
        assertThat(retrySettings.getInitialRpcTimeoutSeconds()).isEqualTo(6);
        assertThat(retrySettings.getRpcTimeoutMultiplier()).isEqualTo(7);
        assertThat(retrySettings.getMaxRpcTimeoutSeconds()).isEqualTo(8);
        assertThat(gcpPubSubProperties.getFullyQualifiedSubscriberProperties()).hasSize(1);
        assertThat(gcpPubSubProperties.getFullyQualifiedSubscriberProperties()).containsKey(ProjectSubscriptionName.parse("projects/fake project/subscriptions/subscription-name"));
        DefaultSubscriberFactory subscriberFactory = ctx.getBean("defaultSubscriberFactory", DefaultSubscriberFactory.class);
        RetrySettings expectedRetrySettings = RetrySettings.newBuilder().setTotalTimeout(Duration.ofSeconds(1L)).setInitialRetryDelay(Duration.ofSeconds(2L)).setRetryDelayMultiplier(3).setMaxRetryDelay(Duration.ofSeconds(4L)).setMaxAttempts(5).setInitialRpcTimeout(Duration.ofSeconds(6L)).setRpcTimeoutMultiplier(7).setMaxRpcTimeout(Duration.ofSeconds(8L)).build();
        assertThat(subscriberFactory.getRetrySettings("subscription-name")).isEqualTo(expectedRetrySettings);
        assertThat(ctx.getBean("subscriberRetrySettings-projects/fake project/subscriptions/subscription-name", RetrySettings.class)).isEqualTo(expectedRetrySettings);
    });
}
Also used : RetrySettings(com.google.api.gax.retrying.RetrySettings) DefaultSubscriberFactory(com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) GcpProjectIdProvider(com.google.cloud.spring.core.GcpProjectIdProvider) PubSubConfiguration(com.google.cloud.spring.pubsub.core.PubSubConfiguration) Test(org.junit.jupiter.api.Test)

Example 22 with DefaultSubscriberFactory

use of com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory in project spring-cloud-gcp by GoogleCloudPlatform.

the class GcpPubSubAutoConfigurationTests method subscriberExecutorProvider_globalAndDifferentSelectiveConfigurationSet_onlyGlobalCreated.

@Test
void subscriberExecutorProvider_globalAndDifferentSelectiveConfigurationSet_onlyGlobalCreated() {
    ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpPubSubAutoConfiguration.class)).withPropertyValues("spring.cloud.gcp.pubsub.subscriber.executor-threads=5", "spring.cloud.gcp.pubsub.subscription.subscription-name.parallel-pull-count=3").withUserConfiguration(TestConfig.class);
    contextRunner.run(ctx -> {
        DefaultSubscriberFactory factory = (DefaultSubscriberFactory) ctx.getBean("defaultSubscriberFactory");
        // Verify that global executor provider is created and used
        ExecutorProvider globalExecutorProvider = (ExecutorProvider) ctx.getBean("globalSubscriberExecutorProvider");
        assertThat(ctx.containsBean("subscriberExecutorProvider-subscription-name")).isFalse();
        assertThat(factory.getGlobalExecutorProvider()).isSameAs(globalExecutorProvider);
    });
}
Also used : DefaultSubscriberFactory(com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) ExecutorProvider(com.google.api.gax.core.ExecutorProvider) Test(org.junit.jupiter.api.Test)

Example 23 with DefaultSubscriberFactory

use of com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory in project spring-cloud-gcp by GoogleCloudPlatform.

the class GcpPubSubAutoConfigurationTests method flowControlSettings_globalAndSelectiveConfigurationSet_selectiveTakesPrecedence.

@Test
void flowControlSettings_globalAndSelectiveConfigurationSet_selectiveTakesPrecedence() {
    ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpPubSubAutoConfiguration.class)).withPropertyValues("spring.cloud.gcp.pubsub.subscriber.flow-control.max-outstanding-element-Count=10", "spring.cloud.gcp.pubsub.subscriber.flow-control.max-outstanding-request-Bytes=10", "spring.cloud.gcp.pubsub.subscriber.flow-control.limit-exceeded-behavior=Block", "spring.cloud.gcp.pubsub.subscription.subscription-name.flow-control.max-outstanding-element-Count=11", "spring.cloud.gcp.pubsub.subscription.subscription-name.flow-control.max-outstanding-request-Bytes=12", "spring.cloud.gcp.pubsub.subscription.subscription-name.flow-control.limit-exceeded-behavior=Ignore").withUserConfiguration(TestConfig.class);
    contextRunner.run(ctx -> {
        GcpPubSubProperties gcpPubSubProperties = ctx.getBean(GcpPubSubProperties.class);
        GcpProjectIdProvider projectIdProvider = ctx.getBean(GcpProjectIdProvider.class);
        // Validate settings for subscribers that have subscription-specific flow control settings
        // property set
        PubSubConfiguration.FlowControl flowControl = gcpPubSubProperties.computeSubscriberFlowControlSettings(ProjectSubscriptionName.of(projectIdProvider.getProjectId(), "subscription-name"));
        assertThat(flowControl.getMaxOutstandingElementCount()).isEqualTo(11L);
        assertThat(flowControl.getMaxOutstandingRequestBytes()).isEqualTo(12L);
        assertThat(flowControl.getLimitExceededBehavior()).isEqualTo(FlowController.LimitExceededBehavior.Ignore);
        // Validate settings for subscribers that do not have subscription-specific flow control
        // settings property set
        PubSubConfiguration.FlowControl flowControlForOtherSubscriber = gcpPubSubProperties.computeSubscriberFlowControlSettings(ProjectSubscriptionName.of(projectIdProvider.getProjectId(), "other"));
        assertThat(flowControlForOtherSubscriber.getMaxOutstandingElementCount()).isEqualTo(10L);
        assertThat(flowControlForOtherSubscriber.getMaxOutstandingRequestBytes()).isEqualTo(10L);
        assertThat(flowControlForOtherSubscriber.getLimitExceededBehavior()).isEqualTo(FlowController.LimitExceededBehavior.Block);
        // Change in behavior: calculated properties don't get stored
        assertThat(gcpPubSubProperties.getFullyQualifiedSubscriberProperties()).hasSize(1);
        assertThat(gcpPubSubProperties.getFullyQualifiedSubscriberProperties()).containsKey(ProjectSubscriptionName.parse("projects/fake project/subscriptions/subscription-name"));
        // Verify that beans for selective and global flow control settings are created.
        DefaultSubscriberFactory subscriberFactory = ctx.getBean("defaultSubscriberFactory", DefaultSubscriberFactory.class);
        FlowControlSettings expectedFlowControlForSubscriptionName = FlowControlSettings.newBuilder().setMaxOutstandingElementCount(11L).setMaxOutstandingRequestBytes(12L).setLimitExceededBehavior(FlowController.LimitExceededBehavior.Ignore).build();
        FlowControlSettings expectedFlowControlForOther = FlowControlSettings.newBuilder().setMaxOutstandingElementCount(10L).setMaxOutstandingRequestBytes(10L).setLimitExceededBehavior(FlowController.LimitExceededBehavior.Block).build();
        assertThat(subscriberFactory.getFlowControlSettings("subscription-name")).isEqualTo(expectedFlowControlForSubscriptionName);
        assertThat(ctx.getBean("subscriberFlowControlSettings-projects/fake project/subscriptions/subscription-name", FlowControlSettings.class)).isEqualTo(expectedFlowControlForSubscriptionName);
        assertThat(subscriberFactory.getFlowControlSettings("other")).isEqualTo(expectedFlowControlForOther);
        assertThat(ctx.getBean("globalSubscriberFlowControlSettings", FlowControlSettings.class)).isEqualTo(expectedFlowControlForOther);
    });
}
Also used : FlowControlSettings(com.google.api.gax.batching.FlowControlSettings) DefaultSubscriberFactory(com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) GcpProjectIdProvider(com.google.cloud.spring.core.GcpProjectIdProvider) PubSubConfiguration(com.google.cloud.spring.pubsub.core.PubSubConfiguration) Test(org.junit.jupiter.api.Test)

Example 24 with DefaultSubscriberFactory

use of com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory in project spring-cloud-gcp by GoogleCloudPlatform.

the class GcpPubSubAutoConfigurationTests method flowControlSettings_subsetOfProperties_pickGlobalWhenSelectiveNotSpecified.

@Test
void flowControlSettings_subsetOfProperties_pickGlobalWhenSelectiveNotSpecified() {
    ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpPubSubAutoConfiguration.class)).withPropertyValues("spring.cloud.gcp.pubsub.subscriber.flow-control.max-outstanding-element-Count=11", "spring.cloud.gcp.pubsub.subscription.subscription-name.flow-control.max-outstanding-request-Bytes=12", "spring.cloud.gcp.pubsub.subscription.subscription-name.flow-control.limit-exceeded-behavior=Ignore", "spring.cloud.gcp.pubsub.subscription.subscription-name.parallel-pull-count=2").withUserConfiguration(TestConfig.class);
    contextRunner.run(ctx -> {
        GcpPubSubProperties gcpPubSubProperties = ctx.getBean(GcpPubSubProperties.class);
        GcpProjectIdProvider projectIdProvider = ctx.getBean(GcpProjectIdProvider.class);
        PubSubConfiguration.FlowControl flowControl = gcpPubSubProperties.computeSubscriberFlowControlSettings(ProjectSubscriptionName.of(projectIdProvider.getProjectId(), "subscription-name"));
        assertThat(flowControl.getMaxOutstandingElementCount()).isEqualTo(11L);
        assertThat(flowControl.getMaxOutstandingRequestBytes()).isEqualTo(12L);
        assertThat(flowControl.getLimitExceededBehavior()).isEqualTo(FlowController.LimitExceededBehavior.Ignore);
        // Verify that beans for selective and global flow control settings are created.
        DefaultSubscriberFactory subscriberFactory = ctx.getBean("defaultSubscriberFactory", DefaultSubscriberFactory.class);
        FlowControlSettings expectedFlowControlForSubscriptionName = FlowControlSettings.newBuilder().setMaxOutstandingElementCount(11L).setMaxOutstandingRequestBytes(12L).setLimitExceededBehavior(FlowController.LimitExceededBehavior.Ignore).build();
        FlowControlSettings expectedGlobalSettings = FlowControlSettings.newBuilder().setMaxOutstandingElementCount(11L).build();
        assertThat(subscriberFactory.getFlowControlSettings("subscription-name")).isEqualTo(expectedFlowControlForSubscriptionName);
        assertThat(ctx.getBean("subscriberFlowControlSettings-projects/fake project/subscriptions/subscription-name", FlowControlSettings.class)).isEqualTo(expectedFlowControlForSubscriptionName);
        assertThat(ctx.getBean("globalSubscriberFlowControlSettings", FlowControlSettings.class)).isEqualTo(expectedGlobalSettings);
    });
}
Also used : FlowControlSettings(com.google.api.gax.batching.FlowControlSettings) DefaultSubscriberFactory(com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) GcpProjectIdProvider(com.google.cloud.spring.core.GcpProjectIdProvider) PubSubConfiguration(com.google.cloud.spring.pubsub.core.PubSubConfiguration) Test(org.junit.jupiter.api.Test)

Example 25 with DefaultSubscriberFactory

use of com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory in project spring-cloud-gcp by GoogleCloudPlatform.

the class GcpPubSubAutoConfigurationTests method retryableCodes_many.

@Test
void retryableCodes_many() {
    ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpPubSubAutoConfiguration.class)).withUserConfiguration(TestConfig.class).withPropertyValues("spring.cloud.gcp.pubsub.subscriber.retryableCodes=UNKNOWN,ABORTED,UNAVAILABLE,INTERNAL");
    contextRunner.run(ctx -> {
        GcpPubSubProperties properties = ctx.getBean(GcpPubSubProperties.class);
        GcpProjectIdProvider projectIdProvider = ctx.getBean(GcpProjectIdProvider.class);
        DefaultSubscriberFactory defaultSubscriberFactory = ctx.getBean("defaultSubscriberFactory", DefaultSubscriberFactory.class);
        Code[] expectedRetryableCodes = new Code[] { Code.UNKNOWN, Code.ABORTED, Code.UNAVAILABLE, Code.INTERNAL };
        assertThat(properties.getSubscriber().getRetryableCodes()).isEqualTo(expectedRetryableCodes);
        assertThat(properties.computeRetryableCodes("subscription-name", projectIdProvider.getProjectId())).isEqualTo(expectedRetryableCodes);
        assertThat(defaultSubscriberFactory.getRetryableCodes("subscription-name")).isEqualTo(expectedRetryableCodes);
    });
}
Also used : DefaultSubscriberFactory(com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) GcpProjectIdProvider(com.google.cloud.spring.core.GcpProjectIdProvider) Code(com.google.api.gax.rpc.StatusCode.Code) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultSubscriberFactory (com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory)26 Test (org.junit.jupiter.api.Test)25 ApplicationContextRunner (org.springframework.boot.test.context.runner.ApplicationContextRunner)25 GcpProjectIdProvider (com.google.cloud.spring.core.GcpProjectIdProvider)12 PubSubConfiguration (com.google.cloud.spring.pubsub.core.PubSubConfiguration)10 FlowControlSettings (com.google.api.gax.batching.FlowControlSettings)6 RetrySettings (com.google.api.gax.retrying.RetrySettings)6 ExecutorProvider (com.google.api.gax.core.ExecutorProvider)5 Code (com.google.api.gax.rpc.StatusCode.Code)1 Subscriber (com.google.cloud.pubsub.v1.Subscriber)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1