Path: redb-route/tests/redb.Route.Tests.Kafka/KafkaPartitionKeyTests.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Kafka/KafkaPartitionKeyTests.cs ---- using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Kafka; namespace redb.Route.Tests.Kafka; /// /// The partition of a sent record, by Apache Camel's rule: the endpoint's partitionKey takes precedence over the /// per-message header; without either Kafka's partitioner places the record by its /// key. A value that is not a partition number is refused; one name for each thing (partitionKey for a producer, /// partitionNumber for a consumer). /// public sealed class KafkaPartitionKeyTests { private static KafkaEndpoint Endpoint(string query) => (KafkaEndpoint)new KafkaComponent().CreateEndpoint(EndpointUriParser.Parse($"kafka://orders?brokers=x:9092&{query}")); private static int? Resolve(string query, object? header = null, bool withHeader = true) { var endpoint = Endpoint(query); var producer = (KafkaProducer)endpoint.CreateProducer(); var exchange = new Exchange(new Message("x")); if (withHeader) exchange.In.Headers[KafkaHeaders.PartitionKey] = header; return producer.ResolvePartition(exchange); } [Fact] public void The_endpoint_option_takes_precedence_over_the_header_as_in_camel() => Resolve("partitionKey=1", header: 4).Should().Be(1); [Fact] public void Without_the_option_the_header_decides() => Resolve("acks=all", header: 4).Should().Be(4); [Fact] public void Without_either_there_is_no_partition_and_the_partitioner_decides_by_key() => Resolve("acks=all", withHeader: false).Should().BeNull(); [Theory] [InlineData("partitionKey=2", null, 2)] [InlineData("partitionKey=2", 3, 2)] [InlineData("acks=all", 3, 3)] public void A_plain_list_element_takes_the_endpoint_option_else_the_exchange_header_unlike_camel( string query, object? header, int expected) { // camel-kafka sends a plain (non-Message) element with no partition; ours keeps the endpoint's or the exchange's // (owner's decision 2026-11-19): an endpoint that names a partition means it for every record. var producer = (KafkaProducer)Endpoint(query).CreateProducer(); var exchange = new Exchange(new Message(new List { "a", "b" })); if (header is not null) exchange.In.Headers[KafkaHeaders.PartitionKey] = header; producer.ResolvePartition(ListBody.ElementExchange(exchange, "a")).Should().Be(expected); } [Theory] [InlineData(3, 3)] [InlineData(3L, 3)] [InlineData((short)2, 2)] [InlineData((byte)1, 1)] [InlineData("5", 5)] [InlineData(" 0 ", 0)] public void The_header_takes_an_integer_or_a_whole_number_text(object value, int partition) => KafkaPartitions.FromHeader(value).Should().Be(partition); [Theory] [InlineData(-1)] [InlineData("-2")] [InlineData("two")] [InlineData("1.5")] [InlineData(1.5)] [InlineData("")] [InlineData(null)] [InlineData(3_000_000_000L)] public void A_header_that_is_not_a_partition_number_is_refused_naming_it(object? value) { var act = () => KafkaPartitions.FromHeader(value); act.Should().Throw() .WithMessage($"The {KafkaHeaders.PartitionKey} header must be a partition number, 0 or more*Nothing was sent.*"); } [Fact] public void A_negative_partitionKey_option_is_refused() { var act = () => Endpoint("partitionKey=-1"); act.Should().Throw().WithMessage("*partitionKey must be a partition number, 0 or more*"); } [Fact] public void A_producer_refuses_partitionNumber_naming_partitionKey() { var act = () => Endpoint("partitionNumber=2").CreateProducer(); act.Should().Throw().WithMessage("*'partitionNumber' names the partition a consumer reads*'partitionKey'*"); } [Fact] public void A_consumer_refuses_partitionKey_naming_partitionNumber() { var act = () => Endpoint("groupId=g&partitionKey=2").CreateConsumer(Substitute.For()); act.Should().Throw().WithMessage("*'partitionKey' names the partition a producer sends to*'partitionNumber'*"); } [Fact] public void The_builder_writes_partitionKey_for_a_producer_and_partitionNumber_for_a_consumer() { redb.Route.Kafka.Kafka.Topic("orders").PartitionKey(2).Build().Should().Contain("partitionKey=2").And.NotContain("partitionNumber"); redb.Route.Kafka.Kafka.Topic("orders").PartitionNumber(1).Build().Should().Contain("partitionNumber=1"); } }