Path: redb-route/tests/redb.Route.Tests.Kafka/KafkaFaultStand.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/KafkaFaultStand.cs ---- using System.Text; using Confluent.Kafka; using Confluent.Kafka.Admin; using redb.Route.Tests.Conformance; namespace redb.Route.Tests.Kafka; /// /// The Kafka stand of the fault runs: one KRaft node (controller and broker) at localhost:39092 with host networking, /// started by tests/docker/run-fault-suite.sh --broker kafka, reached through Confluent.Kafka directly, not /// through the connector. Its data stays in the container, so topics and committed offsets outlive a stop and a start. /// The three-node cluster of the compose file does not fit: stopping one node of three tests a leader change, not a /// lost connection. Its container, when names it, is broken with ; /// the broker is ready when it answers an API versions request. /// public sealed class KafkaFaultStand : IConnectorStand { /// The variable that names the Kafka container a test may stop and restart. public const string ContainerVariable = "REDB_TEST_KAFKA_CONTAINER"; /// The bootstrap server of the stand. public const string Brokers = "localhost:39092"; /// The stand of this process. public static KafkaFaultStand Instance { get; } = new(); private KafkaFaultStand() { } /// public BrokerFault? Fault { get; } = BrokerFault.FromEnvironment(ContainerVariable, "/opt/kafka/bin/kafka-broker-api-versions.sh", "--bootstrap-server", Brokers); /// public TimeSpan Wait { get; } = BrokerFault.DefaultWait; /// Creates a topic with one partition and waits until it has a leader. public static async Task CreateTopicAsync(string prefix) { var topic = $"{prefix}-{Guid.NewGuid():N}"; using var admin = new AdminClientBuilder(new AdminClientConfig { BootstrapServers = Brokers }).Build(); await admin.CreateTopicsAsync([new TopicSpecification { Name = topic, NumPartitions = 1, ReplicationFactor = 1 }]); var deadline = DateTime.UtcNow + BrokerFault.DefaultWait; while (true) { var metadata = admin.GetMetadata(topic, TimeSpan.FromSeconds(10)).Topics.SingleOrDefault(t => t.Topic == topic); if (metadata is { Error.IsError: false } && metadata.Partitions.Count == 1 && metadata.Partitions[0].Leader >= 0) return topic; if (DateTime.UtcNow > deadline) throw new TimeoutException($"topic {topic} has no leader after {BrokerFault.DefaultWait}."); await Task.Delay(200); } } /// public async Task PutAsync(string destination, string body, CancellationToken ct = default) { using var producer = new ProducerBuilder(new ProducerConfig { BootstrapServers = Brokers, MessageTimeoutMs = 60000, }).Build(); await producer.ProduceAsync(destination, new Message { Value = body }, ct); } /// /// The consumer group a route on reads with in the conformance cases: Kafka keeps the records, /// so "taken off the queue" is "committed by this group", and reads as this group too. /// public static string GroupOf(string topic) => $"conformance-{topic}"; /// /// /// Reads as the group of from its committed offsets (the start of the topic when it has none), /// committed transactions only, and commits what it read. /// public Task> DrainAsync(string destination, TimeSpan wait, CancellationToken ct = default) => Task.Run>(() => { using var consumer = new ConsumerBuilder(new ConsumerConfig { BootstrapServers = Brokers, GroupId = GroupOf(destination), AutoOffsetReset = AutoOffsetReset.Earliest, IsolationLevel = IsolationLevel.ReadCommitted, EnableAutoCommit = false, }).Build(); consumer.Subscribe(destination); var bodies = new List(); var deadline = DateTime.UtcNow + wait; while (DateTime.UtcNow < deadline && !ct.IsCancellationRequested) { var result = consumer.Consume(TimeSpan.FromMilliseconds(200)); if (result?.Message is null) { if (bodies.Count > 0) break; continue; } bodies.Add(result.Message.Value is null ? "" : Encoding.UTF8.GetString(result.Message.Value)); consumer.Commit(result); } consumer.Close(); return bodies; }, ct); }