public void Configure_WithBaseConfig_ConfigurationMerged()
        {
            var baseConfig = new KafkaClientConfig
            {
                BootstrapServers = "PLAINTEXT://tests",
                MessageMaxBytes  = 42
            };
            var builder = new KafkaProducerEndpointBuilder(baseConfig);

            builder
            .ProduceTo("some-topic")
            .Configure(
                config =>
            {
                config.ThrowIfNotAcknowledged = false;
                config.MessageTimeoutMs       = 42;
                config.MessageMaxBytes        = 4242;
            });
            var endpoint = builder.Build();

            endpoint.Configuration.BootstrapServers.Should().Be("PLAINTEXT://tests");
            endpoint.Configuration.ThrowIfNotAcknowledged.Should().BeFalse();
            endpoint.Configuration.MessageTimeoutMs.Should().Be(42);
            endpoint.Configuration.MessageMaxBytes.Should().Be(4242);
            baseConfig.MessageMaxBytes.Should().Be(42);
        }
        public void Configure_MultipleConfigurationActions_MergedConfigurationSet()
        {
            var builder = new KafkaProducerEndpointBuilder(
                new KafkaClientConfig
            {
                BootstrapServers = "PLAINTEXT://tests"
            });

            builder
            .ProduceTo("some-topic")
            .Configure(
                config =>
            {
                config.ThrowIfNotAcknowledged = false;
            })
            .Configure(
                config =>
            {
                config.MessageTimeoutMs = 42;
            });
            var endpoint = builder.Build();

            endpoint.Configuration.ThrowIfNotAcknowledged.Should().BeFalse();
            endpoint.Configuration.MessageTimeoutMs.Should().Be(42);
        }
Beispiel #3
0
        private static KafkaProducerEndpoint BuildEndpoint(
            Action <IKafkaProducerEndpointBuilder> builderAction,
            KafkaClientConfig?clientConfig)
        {
            var builder = new KafkaProducerEndpointBuilder(clientConfig);

            builderAction.Invoke(builder);

            return(builder.Build());
        }
        public void Build_WithoutBootstrapServer_ExceptionThrown()
        {
            var builder = new KafkaProducerEndpointBuilder();

            Action act = () =>
            {
                builder.ProduceTo("some-topic");
                builder.Build();
            };

            act.Should().ThrowExactly <EndpointConfigurationException>();
        }
        public void Build_WithoutTopicName_ExceptionThrown()
        {
            var builder = new KafkaProducerEndpointBuilder(
                new KafkaClientConfig
            {
                BootstrapServers = "PLAINTEXT://tests"
            });

            Action act = () => builder.Build();

            act.Should().ThrowExactly <EndpointConfigurationException>();
        }
        public void ProduceTo_TypedTopicNameFunction_TopicSet()
        {
            var builder = new KafkaProducerEndpointBuilder(
                new KafkaClientConfig
            {
                BootstrapServers = "PLAINTEXT://tests"
            });

            builder.ProduceTo <TestEventOne>(_ => "some-topic");
            var endpoint = builder.Build();

            endpoint.GetActualName(null !, null !).Should().Be("some-topic");
            endpoint.GetPartition(null !, null !).Should().Be(Partition.Any);
        }
        public void ProduceTo_TopicNameAndPartition_TopicAndPartitionSet()
        {
            var builder = new KafkaProducerEndpointBuilder(
                new KafkaClientConfig
            {
                BootstrapServers = "PLAINTEXT://tests"
            });

            builder.ProduceTo("some-topic", 42);
            var endpoint = builder.Build();

            endpoint.GetActualName(null !, null !).Should().Be("some-topic");
            endpoint.GetPartition(null !, null !).Should().Be(new Partition(42));
        }
        public void ProduceTo_TypedTopicNameFormatAndPartitionFunction_TopicAndPartitionSet()
        {
            var builder = new KafkaProducerEndpointBuilder(
                new KafkaClientConfig
            {
                BootstrapServers = "PLAINTEXT://tests"
            });

            builder.ProduceTo <TestEventOne>("some-topic-{0}", _ => new[] { "123" }, _ => 42);
            var endpoint = builder.Build();

            endpoint.GetActualName(null !, null !).Should().Be("some-topic-123");
            endpoint.GetPartition(null !, null !).Should().Be(new Partition(42));
        }
        public void ProduceTo_TopicNameFormat_TopicSet()
        {
            var builder = new KafkaProducerEndpointBuilder(
                new KafkaClientConfig
            {
                BootstrapServers = "PLAINTEXT://tests"
            });

            builder.ProduceTo("some-topic-{0}", _ => new[] { "123" });
            var endpoint = builder.Build();

            endpoint.GetActualName(null !, null !).Should().Be("some-topic-123");
            endpoint.GetPartition(null !, null !).Should().Be(Partition.Any);
        }
        public void ProduceTo_TypedTopicNameAndPartitionFunctionsWithServiceProvider_TopicAndPartitionSet()
        {
            var builder = new KafkaProducerEndpointBuilder(
                new KafkaClientConfig
            {
                BootstrapServers = "PLAINTEXT://tests"
            });

            builder.ProduceTo <TestEventOne>((_, _) => "some-topic", (_, _) => 42);
            var endpoint = builder.Build();

            endpoint.GetActualName(null !, null !).Should().Be("some-topic");
            endpoint.GetPartition(null !, null !).Should().Be(new Partition(42));
        }
Beispiel #11
0
        /// <summary>
        ///     Adds a <see cref="MoveMessageErrorPolicy" /> that moves the messages that fail to be processed to the
        ///     configured endpoint.
        /// </summary>
        /// <param name="builder">
        ///     The <see cref="IErrorPolicyChainBuilder" />.
        /// </param>
        /// <param name="endpointBuilderAction">
        ///     An <see cref="Action{T}" /> that takes the <see cref="IKafkaProducerEndpointBuilder" /> and configures
        ///     it.
        /// </param>
        /// <param name="policyConfigurationAction">
        ///     The (optional) additional configuration.
        /// </param>
        /// <returns>
        ///     The <see cref="IErrorPolicyChainBuilder" /> so that additional calls can be chained.
        /// </returns>
        public static IErrorPolicyChainBuilder ThenMoveToKafkaTopic(
            this IErrorPolicyChainBuilder builder,
            Action <IKafkaProducerEndpointBuilder> endpointBuilderAction,
            Action <MoveMessageErrorPolicy>?policyConfigurationAction = null)
        {
            Check.NotNull(builder, nameof(builder));
            Check.NotNull(endpointBuilderAction, nameof(endpointBuilderAction));

            var kafkaClientConfig =
                ((builder as ErrorPolicyChainBuilder)?.EndpointsConfigurationBuilder as KafkaEndpointsConfigurationBuilder)
                ?.ClientConfig;

            var endpointBuilder = new KafkaProducerEndpointBuilder(kafkaClientConfig);

            endpointBuilderAction(endpointBuilder);

            return(builder.ThenMove(endpointBuilder.Build(), policyConfigurationAction));
        }
        public void UseEndpointNameResolver_TopicAndPartitionSet()
        {
            var serviceProvider = Substitute.For <IServiceProvider>();

            serviceProvider.GetService(typeof(TestEndpointNameResolver))
            .Returns(new TestEndpointNameResolver());

            var builder = new KafkaProducerEndpointBuilder(
                new KafkaClientConfig
            {
                BootstrapServers = "PLAINTEXT://tests"
            });

            builder.UseEndpointNameResolver <TestEndpointNameResolver>();
            var endpoint = builder.Build();

            endpoint.GetActualName(null !, serviceProvider).Should().Be("some-topic");
            endpoint.GetPartition(null !, serviceProvider).Should().Be(new Partition(42));
        }