Path: redb-route/tests/redb.Route.Tests.Kafka/KafkaListSendTests.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/KafkaListSendTests.cs ---- using System.Text; using Confluent.Kafka; using Confluent.Kafka.Admin; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Kafka; namespace redb.Route.Tests.Kafka; /// /// A list body at the Kafka producer, as camel-kafka sends an Iterable body: each element of a list of IMessage is its /// own record with its own value, headers and key, none of the exchange's headers; each element of a plain list is a /// record with the exchange's headers and key; a failure in the middle stops the list and the records sent stay. /// [Trait("Category", "Integration")] public sealed class KafkaListSendTests : IAsyncLifetime { private const string BootstrapServers = "localhost:29092,localhost:29094,localhost:29096"; public Task InitializeAsync() => KafkaReadiness.GroupCoordinatorAsync(); public Task DisposeAsync() => Task.CompletedTask; private static async Task CreateTopic(string name, int partitions = 1) { var topic = $"{name}-{Guid.NewGuid():N}"; using var admin = new AdminClientBuilder(new AdminClientConfig { BootstrapServers = BootstrapServers }).Build(); await admin.CreateTopicsAsync([new TopicSpecification { Name = topic, NumPartitions = partitions, ReplicationFactor = 1 }]); return topic; } private static async Task StartProducer(string topic) { var uri = EndpointUriParser.Parse($"kafka://{topic}?brokers={BootstrapServers}"); var producer = (KafkaProducer)((KafkaEndpoint)new KafkaComponent().CreateEndpoint(uri)).CreateProducer(); await producer.Start(); return producer; } private static List> Read(string topic, int expected) { var config = new ConsumerConfig { BootstrapServers = BootstrapServers, GroupId = $"verify-{Guid.NewGuid():N}", AutoOffsetReset = AutoOffsetReset.Earliest, }; using var consumer = new ConsumerBuilder(config).Build(); consumer.Subscribe(topic); var records = new List>(); var deadline = DateTime.UtcNow + (expected == 0 ? TimeSpan.FromSeconds(5) : TimeSpan.FromSeconds(60)); while (DateTime.UtcNow < deadline && (expected == 0 || records.Count < expected)) { var result = consumer.Consume(TimeSpan.FromMilliseconds(250)); if (result?.Message is not null) records.Add(result); } consumer.Close(); return records; } private static string? Header(ConsumeResult record, string name) => record.Message.Headers.TryGetLastBytes(name, out var bytes) ? Encoding.UTF8.GetString(bytes) : null; private static string Value(ConsumeResult record) => Encoding.UTF8.GetString(record.Message.Value); [Fact] public async Task Each_message_of_a_list_is_its_own_record_with_its_own_value_headers_and_key() { var topic = await CreateTopic("list-messages", partitions: 3); var producer = await StartProducer(topic); var parent = new Exchange(new Message(new List { Element("a", "key-a", "rec-a"), Element("b", "key-b", "rec-b"), Element("c", "key-a", "rec-c"), })); parent.In.Headers["X-Parent"] = "must-not-travel"; await producer.Process(parent); await producer.Stop(); var records = Read(topic, 3).OrderBy(r => Value(r)).ToList(); records.Select(Value).Should().Equal("a", "b", "c"); records.Select(r => r.Message.Key).Should().Equal("key-a", "key-b", "key-a"); records.Select(r => Header(r, "X-Record")).Should().Equal("rec-a", "rec-b", "rec-c"); records.Should().AllSatisfy(r => Header(r, "X-Parent").Should().BeNull()); records[0].Partition.Should().Be(records[2].Partition, "the same key goes to the same partition"); } [Fact] public async Task Each_value_of_a_plain_list_is_a_record_with_the_exchanges_headers_and_key() { var topic = await CreateTopic("list-plain"); var producer = await StartProducer(topic); var parent = new Exchange(new Message(new List { "x", "y" })); parent.In.Headers["X-Parent"] = "p"; parent.In.Headers[KafkaHeaders.Key] = "parent-key"; await producer.Process(parent); await producer.Stop(); var records = Read(topic, 2); records.Select(Value).Should().Equal("x", "y"); records.Should().AllSatisfy(r => { r.Message.Key.Should().Be("parent-key"); Header(r, "X-Parent").Should().Be("p"); }); } [Fact] public async Task A_failure_in_the_middle_stops_the_list_and_the_records_sent_stay() { var topic = await CreateTopic("list-fail"); var producer = await StartProducer(topic); var tooLarge = new Message(new byte[2_000_000]); var parent = new Exchange(new Message(new List { new Message("first"), tooLarge, new Message("third") })); var act = () => producer.Process(parent); await act.Should().ThrowAsync(); await producer.Stop(); Read(topic, 0).Select(Value).Should().Equal("first"); } private static Message Element(string value, string key, string header) { var message = new Message(value); message.Headers[KafkaHeaders.Key] = key; message.Headers["X-Record"] = header; return message; } }