Search in sources :

Example 6 with DefaultSubscriberFactory

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

the class GcpPubSubAutoConfigurationTests method customRetrySettingsUsedWhenProvided.

@Test
void customRetrySettingsUsedWhenProvided() {
    RetrySettings retrySettings = mock(RetrySettings.class);
    ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpPubSubAutoConfiguration.class)).withUserConfiguration(TestConfig.class).withBean("subscriberRetrySettings", RetrySettings.class, () -> retrySettings);
    contextRunner.run(ctx -> {
        DefaultSubscriberFactory subscriberFactory = ctx.getBean("defaultSubscriberFactory", DefaultSubscriberFactory.class);
        assertThat(subscriberFactory.getRetrySettings("name")).isSameAs(retrySettings);
    });
}
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) Test(org.junit.jupiter.api.Test)

Example 7 with DefaultSubscriberFactory

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

the class GcpPubSubAutoConfigurationTests method customExecutorProviderUsedWhenProvided.

@Test
void customExecutorProviderUsedWhenProvided() {
    ExecutorProvider executorProvider = mock(ExecutorProvider.class);
    ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpPubSubAutoConfiguration.class)).withUserConfiguration(TestConfig.class).withBean("subscriberExecutorProvider", ExecutorProvider.class, () -> executorProvider);
    contextRunner.run(ctx -> {
        DefaultSubscriberFactory factory = ctx.getBean("defaultSubscriberFactory", DefaultSubscriberFactory.class);
        assertThat(factory.getExecutorProvider("name")).isSameAs(executorProvider);
        assertThat(ctx.containsBean("globalSubscriberExecutorProvider")).isFalse();
        assertThat(ctx.containsBean("subscriberExecutorProvider-name")).isFalse();
    });
}
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 8 with DefaultSubscriberFactory

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

the class GcpPubSubAutoConfigurationTests method retryableCodes_default.

@Test
void retryableCodes_default() {
    ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpPubSubAutoConfiguration.class)).withUserConfiguration(TestConfig.class);
    contextRunner.run(ctx -> {
        DefaultSubscriberFactory defaultSubscriberFactory = ctx.getBean("defaultSubscriberFactory", DefaultSubscriberFactory.class);
        assertThat(FieldUtils.readField(defaultSubscriberFactory, "retryableCodes", true)).isNull();
        assertThat(defaultSubscriberFactory.getRetryableCodes("subscription-name")).isNull();
    });
}
Also used : DefaultSubscriberFactory(com.google.cloud.spring.pubsub.support.DefaultSubscriberFactory) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) Test(org.junit.jupiter.api.Test)

Example 9 with DefaultSubscriberFactory

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

the class GcpPubSubAutoConfigurationTests method retrySettings_globalAndSelectiveConfigurationSet_selectiveTakesPrecedence.

@Test
void retrySettings_globalAndSelectiveConfigurationSet_selectiveTakesPrecedence() {
    ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpPubSubAutoConfiguration.class)).withPropertyValues("spring.cloud.gcp.pubsub.subscriber.retry.total-timeout-seconds=10", "spring.cloud.gcp.pubsub.subscriber.retry.initial-retry-delay-seconds=10", "spring.cloud.gcp.pubsub.subscriber.retry.retry-delay-multiplier=10", "spring.cloud.gcp.pubsub.subscriber.retry.max-retry-delay-seconds=10", "spring.cloud.gcp.pubsub.subscriber.retry.max-attempts=10", "spring.cloud.gcp.pubsub.subscriber.retry.initial-rpc-timeout-seconds=10", "spring.cloud.gcp.pubsub.subscriber.retry.rpc-timeout-multiplier=10", "spring.cloud.gcp.pubsub.subscriber.retry.max-rpc-timeout-seconds=10", "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.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);
        // Validate settings for subscribers that have subscription-specific retry settings
        // property set
        PubSubConfiguration.Retry retrySettings = gcpPubSubProperties.computeSubscriberRetrySettings("subscription-name", projectIdProvider.getProjectId());
        assertThat(gcpPubSubProperties.getFullyQualifiedSubscriberProperties()).containsKey(ProjectSubscriptionName.parse("projects/fake project/subscriptions/subscription-name"));
        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.getInitialRpcTimeoutSeconds()).isEqualTo(6);
        assertThat(retrySettings.getRpcTimeoutMultiplier()).isEqualTo(7);
        assertThat(retrySettings.getMaxRpcTimeoutSeconds()).isEqualTo(8);
        // Validate settings for subscribers that do **not** have subscription-specific retry
        // settings
        // property set
        PubSubConfiguration.Retry retrySettingsForOtherSubscriber = gcpPubSubProperties.getSubscriber("other", projectIdProvider.getProjectId()).getRetry();
        assertThat(retrySettingsForOtherSubscriber.getTotalTimeoutSeconds()).isEqualTo(10L);
        assertThat(retrySettingsForOtherSubscriber.getInitialRetryDelaySeconds()).isEqualTo(10L);
        assertThat(retrySettingsForOtherSubscriber.getRetryDelayMultiplier()).isEqualTo(10);
        assertThat(retrySettingsForOtherSubscriber.getMaxRetryDelaySeconds()).isEqualTo(10);
        assertThat(retrySettingsForOtherSubscriber.getMaxAttempts()).isEqualTo(10);
        assertThat(retrySettingsForOtherSubscriber.getInitialRpcTimeoutSeconds()).isEqualTo(10);
        assertThat(retrySettingsForOtherSubscriber.getRpcTimeoutMultiplier()).isEqualTo(10);
        assertThat(retrySettingsForOtherSubscriber.getMaxRpcTimeoutSeconds()).isEqualTo(10);
        assertThat(gcpPubSubProperties.getFullyQualifiedSubscriberProperties()).hasSize(1);
        assertThat(gcpPubSubProperties.getFullyQualifiedSubscriberProperties()).containsKey(ProjectSubscriptionName.parse("projects/fake project/subscriptions/subscription-name"));
        // Verify that beans for selective and global retry settings are created. Also
        // verify that selective retry setting takes precedence.
        DefaultSubscriberFactory subscriberFactory = ctx.getBean("defaultSubscriberFactory", DefaultSubscriberFactory.class);
        RetrySettings expectedRetrySettingsForSubscriptionName = 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(8)).build();
        RetrySettings expectedRetrySettingsForOther = RetrySettings.newBuilder().setTotalTimeout(Duration.ofSeconds(10L)).setInitialRetryDelay(Duration.ofSeconds(10L)).setRetryDelayMultiplier(10).setMaxRetryDelay(Duration.ofSeconds(10L)).setMaxAttempts(10).setInitialRpcTimeout(Duration.ofSeconds(10L)).setRpcTimeoutMultiplier(10).setMaxRpcTimeout(Duration.ofSeconds(10)).build();
        assertThat(subscriberFactory.getRetrySettings("subscription-name")).isEqualTo(expectedRetrySettingsForSubscriptionName);
        assertThat(ctx.getBean("subscriberRetrySettings-projects/fake project/subscriptions/subscription-name", RetrySettings.class)).isEqualTo(expectedRetrySettingsForSubscriptionName);
        assertThat(subscriberFactory.getRetrySettings("other")).isEqualTo(expectedRetrySettingsForOther);
        assertThat(ctx.getBean("globalSubscriberRetrySettings", RetrySettings.class)).isEqualTo(expectedRetrySettingsForOther);
    });
}
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 10 with DefaultSubscriberFactory

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

the class GcpPubSubAutoConfigurationTests method retrySettings_subsetOfProperties_pickGlobalWhenSelectiveNotSpecified.

@Test
void retrySettings_subsetOfProperties_pickGlobalWhenSelectiveNotSpecified() {
    ApplicationContextRunner contextRunner = new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GcpPubSubAutoConfiguration.class)).withPropertyValues("spring.cloud.gcp.pubsub.subscriber.retry.total-timeout-seconds=10", "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.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 retry = gcpPubSubProperties.computeSubscriberRetrySettings("subscription-name", projectIdProvider.getProjectId());
        assertThat(retry.getTotalTimeoutSeconds()).isEqualTo(10L);
        assertThat(retry.getInitialRetryDelaySeconds()).isEqualTo(2L);
        assertThat(retry.getRetryDelayMultiplier()).isEqualTo(3);
        assertThat(retry.getMaxRetryDelaySeconds()).isEqualTo(4L);
        assertThat(retry.getMaxAttempts()).isEqualTo(5);
        assertThat(retry.getInitialRpcTimeoutSeconds()).isEqualTo(6L);
        assertThat(retry.getRpcTimeoutMultiplier()).isEqualTo(7);
        assertThat(retry.getMaxRpcTimeoutSeconds()).isEqualTo(8L);
        // Verify that beans for selective and global retry settings are created.
        DefaultSubscriberFactory subscriberFactory = ctx.getBean("defaultSubscriberFactory", DefaultSubscriberFactory.class);
        RetrySettings expectedRetrySettingsForSubscriptionName = RetrySettings.newBuilder().setTotalTimeout(Duration.ofSeconds(10L)).setInitialRetryDelay(Duration.ofSeconds(2L)).setRetryDelayMultiplier(3).setMaxRetryDelay(Duration.ofSeconds(4L)).setMaxAttempts(5).setInitialRpcTimeout(Duration.ofSeconds(6L)).setRpcTimeoutMultiplier(7).setMaxRpcTimeout(Duration.ofSeconds(8L)).build();
        RetrySettings expectedGlobalRetrySettings = RetrySettings.newBuilder().setTotalTimeout(Duration.ofSeconds(10L)).build();
        assertThat(subscriberFactory.getRetrySettings("subscription-name")).isEqualTo(expectedRetrySettingsForSubscriptionName);
        assertThat(ctx.getBean("subscriberRetrySettings-projects/fake project/subscriptions/subscription-name", RetrySettings.class)).isEqualTo(expectedRetrySettingsForSubscriptionName);
        assertThat(ctx.getBean("globalSubscriberRetrySettings", RetrySettings.class)).isEqualTo(expectedGlobalRetrySettings);
    });
}
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)

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