Path: redb-route/tests/redb.Route.Tests.Kafka/KafkaFactoryPrecedenceTests.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/KafkaFactoryPrecedenceTests.cs ---- using NSubstitute; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Kafka; namespace redb.Route.Tests.Kafka; /// /// The options sweep. The factory path used to resolve /// configuration asymmetrically: brokers from the URI were honored, SASL/SSL from the URI were /// silently swallowed, the endpoint's DEFAULTS for autoOffsetReset/retries stomped the factory's /// explicit settings. The family rule (TcpConnectionFactory, WsConnectionFactory, /// HttpConnectionFactory) is one sentence: a parameter the URI actually supplies wins, everything /// else comes from the factory. /// public sealed class KafkaFactoryPrecedenceTests { private static KafkaEndpointOptions BindOptions(string query, out Dictionary supplied) { supplied = query.Split('&', StringSplitOptions.RemoveEmptyEntries) .Select(p => p.Split('=', 2)) .ToDictionary(p => p[0], p => p[1], StringComparer.OrdinalIgnoreCase); var options = new KafkaEndpointOptions(); options.BindFromUri(supplied); return options; } private static KafkaConnectionFactory Factory() => new() { Brokers = "factory:9092", SecurityProtocol = "SaslSsl", SaslMechanism = "ScramSha256", SaslUsername = "factory-user", SaslPassword = "factory-pass", AutoOffsetReset = "Earliest", Retries = 7, Acks = "All", }; [Fact] public void UriSasl_OverridesTheFactory() { var options = BindOptions("saslUsername=uri-user&saslPassword=uri-pass", out var supplied); var config = options.BuildConsumerConfig(Factory(), supplied); config.SaslUsername.Should().Be("uri-user", "SASL from the URI was silently swallowed by the factory, though brokers from the same URI was taken"); config.SaslPassword.Should().Be("uri-pass"); config.SaslMechanism.Should().Be(Confluent.Kafka.SaslMechanism.ScramSha256, "what the URI does not set stays the factory's"); } [Fact] public void FactoryAutoOffsetReset_SurvivesTheEndpointDefault() { var options = BindOptions("groupId=g", out var supplied); var config = options.BuildConsumerConfig(Factory(), supplied); config.AutoOffsetReset.Should().Be(Confluent.Kafka.AutoOffsetReset.Earliest, "the endpoint default (Latest) overwrote the factory's explicit setting"); } [Fact] public void FactoryRetries_SurviveTheEndpointDefault() { var options = BindOptions("", out var supplied); var config = options.BuildProducerConfig(Factory(), supplied); config.MessageSendMaxRetries.Should().Be(7, "the endpoint default (3) overwrote the factory's explicit setting"); } [Fact] public void UriAcks_OverridesTheFactory() { var options = BindOptions("acks=leader", out var supplied); var config = options.BuildProducerConfig(Factory(), supplied); config.Acks.Should().Be(Confluent.Kafka.Acks.Leader, "acks from the URI was not applied at all on the factory path"); } [Fact] public void FactoryAcks_SurviveWhenUriIsSilent() { var options = BindOptions("", out var supplied); var config = options.BuildProducerConfig(Factory(), supplied); config.Acks.Should().Be(Confluent.Kafka.Acks.All); } // ── Hybrid configurations through the component ────────── [Fact] public void FactorySaslCredentials_SatisfyAUriMechanism() { // The RequireSaslCredentials error text offers "inline or via a named connectionFactory", // but the component used to copy only Brokers from the factory before Validate() - so the // second path it advertised was a lie: URI mechanism + factory credentials always threw. var ctx = new RouteContext(); ctx.AddComponent(new KafkaComponent()); ctx.AddToRegistry("cf", Factory()); var act = () => ctx.GetEndpoint( "kafka://orders?connectionFactory=cf&saslMechanism=ScramSha512&securityProtocol=SaslSsl&groupId=g"); act.Should().NotThrow("the factory's credentials are the second legal path from the error text"); } [Fact] public void FactoryGroupId_IsUsed_WhenTheUriHasNone() { // KafkaConnectionFactory.GroupId documented "Default consumer group ID", but nothing ever // read it: the endpoint demanded a URI groupId before the factory value could apply - // a dead option left behind by the dead-options sweep itself. var ctx = new RouteContext(); ctx.AddComponent(new KafkaComponent()); var factory = Factory(); factory.GroupId = "cf-group"; ctx.AddToRegistry("cf", factory); var endpoint = ctx.GetEndpoint("kafka://orders?connectionFactory=cf"); var act = () => endpoint.CreateConsumer(Substitute.For()); act.Should().NotThrow("the factory's GroupId must work as the default"); } [Fact] public void UriIdempotence_WithFactoryAcksLeader_FailsInTheConnectorsWords() { // Validate() checked enableIdempotence against the endpoint's // acks only, so a factory's acks=leader reached librdkafka, which refused the client with its own error. var options = BindOptions("connectionFactory=cf&enableIdempotence=true", out var supplied); var factory = Factory(); factory.Acks = "Leader"; var act = () => options.BuildProducerConfig(factory, supplied); act.Should().Throw() .WithMessage("*'enableIdempotence=true' requires 'acks=all'*connection factory 'cf'*Leader*"); } [Fact] public void FactoryAcksLeader_WithoutExplicitIdempotence_StaysNonIdempotent() { // The check above is for an explicit enableIdempotence=true only: unset, idempotence follows the effective acks. var options = BindOptions("connectionFactory=cf", out var supplied); var factory = Factory(); factory.Acks = "Leader"; var config = options.BuildProducerConfig(factory, supplied); config.Acks.Should().Be(Confluent.Kafka.Acks.Leader); config.EnableIdempotence.Should().BeFalse(); } }