Skip to main content

confluent_kafka/consumer/
consumer_config.rs

1// Copyright 2025 Confluent Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Configuration for the Kafka Consumer.
16//!
17//! Corresponds to `org.apache.kafka.clients.consumer.ConsumerConfig`: the keys
18//! are defined on a `ConfigDef` in Java's order (`ConsumerConfig.CONFIG`)
19//! and parsed and validated by it, `postProcessParsedConfig` runs Java's steps
20//! (`ConsumerConfig#postProcessParsedConfig`), and the configuration answers Java's
21//! `AbstractConfig` getters.
22//!
23//! Per `consumer-threading.md` §20 only the KIP-848 `consumer` group protocol
24//! is implemented. The default still matches Java (`classic`), so users opt
25//! in explicitly; `KafkaConsumer::new` rejects `classic`.
26//!
27//! Not defined, and so reported by [`ConsumerConfig::unused`] and the
28//! client's [`ConsumerConfig::log_unused`] line when set:
29//!
30//! - `partition.assignment.strategy`, `key.deserializer`, `value.deserializer`,
31//!   `metric.reporters` and `interceptor.classes`: lists of classes, which a
32//!   configuration never takes (CLAUDE.md §2), and the client-side assignors
33//!   are out of scope (`consumer-threading.md` §20). Setting
34//!   `partition.assignment.strategy` with `group.protocol=consumer` is still
35//!   rejected, as Java rejects it.
36//! - `security.providers`: Java's JCA security providers have no counterpart.
37//! - `config.providers`: config providers are not translated, so the key is
38//!   left undefined and a value set for it is reported as unused, rather than
39//!   accepted and ignored.
40//! - `enable.metrics.push`: KIP-714 client telemetry is not implemented, so
41//!   the key is left undefined and a value set for it is reported as unused,
42//!   rather than accepted and ignored.
43//!
44//! `AbstractConfig`'s `getPassword`, `typeOf`, `originals*()` and `values*()`
45//! are not translated yet: each needs a public type the crate does not have
46//! (`Password`, `ConfigDef.Type`, Java's `RecordingMap`). See `CHANGELOG.md`.
47
48use std::collections::{HashMap, HashSet};
49use std::hash::{Hash, Hasher};
50use std::sync::LazyLock;
51use std::sync::atomic::{self, AtomicI32};
52
53use log::warn;
54
55use crate::common::Error;
56use crate::common::config::abstract_config::{AbstractConfig, ConfigMap, ValuesReader, string_originals};
57use crate::common::config::config_def::{
58    CaseInsensitiveValidString, ConfigDef, DefaultValue, Importance, LambdaValidator, NonEmptyString, ParsedValue,
59    Range, Type, ValidList, ValidString, as_string, config_error,
60};
61use crate::common::config::{SaslConfigs, SslConfigs};
62use crate::common::errors::InvalidConfigurationError;
63use crate::common::requests::JoinGroupRequest;
64use crate::common::security::auth::SecurityProtocol;
65use crate::common::utils::Utils;
66use crate::consumer::internals::{AutoOffsetResetStrategy, auto_offset_reset_strategy};
67use crate::{ClientDnsLookup, CommonClientConfigs};
68
69/// Process-wide counter for deriving a default `client.id`.
70///
71/// Corresponds to Java's `static AtomicInteger CONSUMER_CLIENT_ID_SEQUENCE`
72/// (`org.apache.kafka.clients.consumer.ConsumerConfig#CONSUMER_CLIENT_ID_SEQUENCE`), which starts at 1. Crate-level (not per-config)
73/// to match Java's static scope, so successive consumers in one process get
74/// distinct ids.
75static CONSUMER_CLIENT_ID_SEQUENCE: AtomicI32 = AtomicI32::new(1);
76
77/// Java's `ConsumerConfig.CLASSIC_PROTOCOL_UNSUPPORTED_CONFIGS`: the keys not
78/// supported for the `CLASSIC` protocol.
79const CLASSIC_PROTOCOL_UNSUPPORTED_CONFIGS: [&str; 3] = [
80    ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG,
81    ConsumerConfig::SHARE_ACKNOWLEDGEMENT_MODE_CONFIG,
82    ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG,
83];
84
85/// Java's `ConsumerConfig.CONSUMER_PROTOCOL_UNSUPPORTED_CONFIGS`: the keys
86/// not supported for the `CONSUMER` protocol.
87const CONSUMER_PROTOCOL_UNSUPPORTED_CONFIGS: [&str; 5] = [
88    ConsumerConfig::PARTITION_ASSIGNMENT_STRATEGY_CONFIG,
89    ConsumerConfig::HEARTBEAT_INTERVAL_MS_CONFIG,
90    ConsumerConfig::SESSION_TIMEOUT_MS_CONFIG,
91    ConsumerConfig::SHARE_ACKNOWLEDGEMENT_MODE_CONFIG,
92    ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG,
93];
94
95/// Java's `ConsumerConfig.CONFIG`, built in its static initializer.
96static CONFIG: LazyLock<ConfigDef> = LazyLock::new(|| {
97    ConfigDef::new()
98        .define_with_validator(
99            ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG,
100            Type::List,
101            ConfigDef::NO_DEFAULT_VALUE,
102            ValidList::any_non_duplicate_values(false, false),
103            Importance::High,
104        )
105        .define_with_validator(
106            ConsumerConfig::CLIENT_DNS_LOOKUP_CONFIG,
107            Type::String,
108            ClientDnsLookup::UseAllDnsIps.config_value(),
109            ValidString::r#in(&[
110                ClientDnsLookup::UseAllDnsIps.config_value(),
111                ClientDnsLookup::ResolveCanonicalBootstrapServersOnly.config_value(),
112            ]),
113            Importance::Medium,
114        )
115        .define_with_validator(
116            ConsumerConfig::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG,
117            Type::Long,
118            CommonClientConfigs::DEFAULT_BOOTSTRAP_RESOLVE_TIMEOUT_MS,
119            Range::at_least(0_i64),
120            Importance::High,
121        )
122        .define(
123            ConsumerConfig::GROUP_ID_CONFIG,
124            Type::String,
125            DefaultValue::Null,
126            Importance::High,
127        )
128        .define_with_validator(
129            ConsumerConfig::GROUP_INSTANCE_ID_CONFIG,
130            Type::String,
131            DefaultValue::Null,
132            NonEmptyString,
133            Importance::Medium,
134        )
135        .define(ConsumerConfig::SESSION_TIMEOUT_MS_CONFIG, Type::Int, 45000, Importance::High)
136        .define(ConsumerConfig::HEARTBEAT_INTERVAL_MS_CONFIG, Type::Int, 3000, Importance::High)
137        // `partition.assignment.strategy` is a list of classes (CLAUDE.md §2)
138        // and the client-side assignors are out of scope: not defined.
139        .define_with_validator(
140            ConsumerConfig::METADATA_MAX_AGE_CONFIG,
141            Type::Long,
142            5 * 60 * 1000_i64,
143            Range::at_least(0),
144            Importance::Low,
145        )
146        .define(
147            ConsumerConfig::ENABLE_AUTO_COMMIT_CONFIG,
148            Type::Boolean,
149            true,
150            Importance::Medium,
151        )
152        .define_with_validator(
153            ConsumerConfig::AUTO_COMMIT_INTERVAL_MS_CONFIG,
154            Type::Int,
155            5000,
156            Range::at_least(0),
157            Importance::Low,
158        )
159        .define(ConsumerConfig::CLIENT_ID_CONFIG, Type::String, "", Importance::Low)
160        .define(
161            ConsumerConfig::CLIENT_RACK_CONFIG,
162            Type::String,
163            ConsumerConfig::DEFAULT_CLIENT_RACK,
164            Importance::Low,
165        )
166        .define_with_validator(
167            ConsumerConfig::MAX_PARTITION_FETCH_BYTES_CONFIG,
168            Type::Int,
169            ConsumerConfig::DEFAULT_MAX_PARTITION_FETCH_BYTES,
170            Range::at_least(0),
171            Importance::High,
172        )
173        .define_with_validator(
174            ConsumerConfig::SEND_BUFFER_CONFIG,
175            Type::Int,
176            128 * 1024,
177            Range::at_least(CommonClientConfigs::SEND_BUFFER_LOWER_BOUND),
178            Importance::Medium,
179        )
180        .define_with_validator(
181            ConsumerConfig::RECEIVE_BUFFER_CONFIG,
182            Type::Int,
183            64 * 1024,
184            Range::at_least(CommonClientConfigs::RECEIVE_BUFFER_LOWER_BOUND),
185            Importance::Medium,
186        )
187        .define_with_validator(
188            ConsumerConfig::FETCH_MIN_BYTES_CONFIG,
189            Type::Int,
190            ConsumerConfig::DEFAULT_FETCH_MIN_BYTES,
191            Range::at_least(0),
192            Importance::High,
193        )
194        .define_with_validator(
195            ConsumerConfig::FETCH_MAX_BYTES_CONFIG,
196            Type::Int,
197            ConsumerConfig::DEFAULT_FETCH_MAX_BYTES,
198            Range::at_least(0),
199            Importance::Medium,
200        )
201        .define_with_validator(
202            ConsumerConfig::FETCH_MAX_WAIT_MS_CONFIG,
203            Type::Int,
204            ConsumerConfig::DEFAULT_FETCH_MAX_WAIT_MS,
205            Range::at_least(0),
206            Importance::Low,
207        )
208        .define_with_validator(
209            ConsumerConfig::RECONNECT_BACKOFF_MS_CONFIG,
210            Type::Long,
211            50_i64,
212            Range::at_least(0_i64),
213            Importance::Low,
214        )
215        .define_with_validator(
216            ConsumerConfig::RECONNECT_BACKOFF_MAX_MS_CONFIG,
217            Type::Long,
218            1000_i64,
219            Range::at_least(0_i64),
220            Importance::Low,
221        )
222        .define_with_validator(
223            ConsumerConfig::RETRY_BACKOFF_MS_CONFIG,
224            Type::Long,
225            CommonClientConfigs::DEFAULT_RETRY_BACKOFF_MS,
226            Range::at_least(0_i64),
227            Importance::Low,
228        )
229        .define_with_validator(
230            ConsumerConfig::RETRY_BACKOFF_MAX_MS_CONFIG,
231            Type::Long,
232            CommonClientConfigs::DEFAULT_RETRY_BACKOFF_MAX_MS,
233            Range::at_least(0_i64),
234            Importance::Low,
235        )
236        // Java defines `enable.metrics.push` here;
237        // it is left undefined until KIP-714 client telemetry is implemented.
238        .define_with_validator(
239            ConsumerConfig::AUTO_OFFSET_RESET_CONFIG,
240            Type::String,
241            AutoOffsetResetStrategy::LATEST.name(),
242            auto_offset_reset_strategy::Validator,
243            Importance::Medium,
244        )
245        .define(ConsumerConfig::CHECK_CRCS_CONFIG, Type::Boolean, true, Importance::Low)
246        .define_with_validator(
247            ConsumerConfig::METRICS_SAMPLE_WINDOW_MS_CONFIG,
248            Type::Long,
249            30000_i64,
250            Range::at_least(0),
251            Importance::Low,
252        )
253        .define_with_validator(
254            ConsumerConfig::METRICS_NUM_SAMPLES_CONFIG,
255            Type::Int,
256            2,
257            Range::at_least(1),
258            Importance::Low,
259        )
260        .define_with_validator(
261            ConsumerConfig::METRICS_RECORDING_LEVEL_CONFIG,
262            Type::String,
263            "INFO",
264            ValidString::r#in(&["INFO", "DEBUG", "TRACE"]),
265            Importance::Low,
266        )
267        // `metric.reporters`, `key.deserializer` and `value.deserializer` are
268        // classes (CLAUDE.md §2): not defined.
269        .define_with_validator(
270            ConsumerConfig::REQUEST_TIMEOUT_MS_CONFIG,
271            Type::Int,
272            30000,
273            Range::at_least(0),
274            Importance::Medium,
275        )
276        .define_with_validator(
277            ConsumerConfig::DEFAULT_API_TIMEOUT_MS_CONFIG,
278            Type::Int,
279            60000,
280            Range::at_least(0),
281            Importance::Medium,
282        )
283        .define(
284            ConsumerConfig::SOCKET_CONNECTION_SETUP_TIMEOUT_MS_CONFIG,
285            Type::Long,
286            CommonClientConfigs::DEFAULT_SOCKET_CONNECTION_SETUP_TIMEOUT_MS,
287            Importance::Medium,
288        )
289        .define(
290            ConsumerConfig::SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS_CONFIG,
291            Type::Long,
292            CommonClientConfigs::DEFAULT_SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS,
293            Importance::Medium,
294        )
295        .define(
296            ConsumerConfig::CONNECTIONS_MAX_IDLE_MS_CONFIG,
297            Type::Long,
298            9 * 60 * 1000_i64,
299            Importance::Medium,
300        )
301        // `interceptor.classes` is a list of classes (CLAUDE.md §2): not defined.
302        .define_with_validator(
303            ConsumerConfig::MAX_POLL_RECORDS_CONFIG,
304            Type::Int,
305            ConsumerConfig::DEFAULT_MAX_POLL_RECORDS,
306            Range::at_least(1),
307            Importance::Medium,
308        )
309        .define_with_validator(
310            ConsumerConfig::MAX_POLL_INTERVAL_MS_CONFIG,
311            Type::Int,
312            300000,
313            Range::at_least(1),
314            Importance::Medium,
315        )
316        .define(
317            ConsumerConfig::EXCLUDE_INTERNAL_TOPICS_CONFIG,
318            Type::Boolean,
319            ConsumerConfig::DEFAULT_EXCLUDE_INTERNAL_TOPICS,
320            Importance::Medium,
321        )
322        .define_internal(
323            ConsumerConfig::THROW_ON_FETCH_STABLE_OFFSET_UNSUPPORTED,
324            Type::Boolean,
325            false,
326            Importance::Low,
327        )
328        .define_with_validator(
329            ConsumerConfig::ISOLATION_LEVEL_CONFIG,
330            Type::String,
331            ConsumerConfig::DEFAULT_ISOLATION_LEVEL,
332            ValidString::r#in(&["read_committed", "read_uncommitted"]),
333            Importance::Medium,
334        )
335        .define(
336            ConsumerConfig::ALLOW_AUTO_CREATE_TOPICS_CONFIG,
337            Type::Boolean,
338            ConsumerConfig::DEFAULT_ALLOW_AUTO_CREATE_TOPICS,
339            Importance::Medium,
340        )
341        .define_with_validator(
342            ConsumerConfig::GROUP_PROTOCOL_CONFIG,
343            Type::String,
344            ConsumerConfig::DEFAULT_GROUP_PROTOCOL,
345            CaseInsensitiveValidString::r#in(&["CLASSIC", "CONSUMER"]),
346            Importance::High,
347        )
348        .define(
349            ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG,
350            Type::String,
351            DefaultValue::Null,
352            Importance::Medium,
353        )
354        // security support; `security.providers` (JCA providers) is not defined.
355        .define_with_validator(
356            CommonClientConfigs::SECURITY_PROTOCOL_CONFIG,
357            Type::String,
358            CommonClientConfigs::DEFAULT_SECURITY_PROTOCOL,
359            CaseInsensitiveValidString::r#in(&SecurityProtocol::names()),
360            Importance::Medium,
361        )
362        .with_client_ssl_support()
363        .with_client_sasl_support()
364        .define_with_validator(
365            CommonClientConfigs::METADATA_RECOVERY_STRATEGY_CONFIG,
366            Type::String,
367            CommonClientConfigs::DEFAULT_METADATA_RECOVERY_STRATEGY,
368            CaseInsensitiveValidString::r#in(&["NONE", "REBOOTSTRAP"]),
369            Importance::Low,
370        )
371        .define_with_validator(
372            CommonClientConfigs::METADATA_RECOVERY_REBOOTSTRAP_TRIGGER_MS_CONFIG,
373            Type::Long,
374            CommonClientConfigs::DEFAULT_METADATA_RECOVERY_REBOOTSTRAP_TRIGGER_MS,
375            Range::at_least(0),
376            Importance::Low,
377        )
378        .define(
379            CommonClientConfigs::METADATA_CLUSTER_CHECK_ENABLE_CONFIG,
380            Type::Boolean,
381            true,
382            Importance::Low,
383        )
384        // Java validates the two share keys with `ShareAcknowledgementMode.Validator`
385        // and `ShareAcquireMode.Validator`, translated below. The share consumer
386        // is out of scope (`consumer-threading.md` §20), so a valid non-blank
387        // value is then rejected under either group protocol by
388        // `checkUnsupportedConfigsPostProcess`, which skips a blank value: the
389        // validators are what reject a blank one, as in Java.
390        .define_with_validator(
391            ConsumerConfig::SHARE_ACKNOWLEDGEMENT_MODE_CONFIG,
392            Type::String,
393            "implicit",
394            share_acknowledgement_mode_validator(),
395            Importance::Medium,
396        )
397        // Java's `DEFAULT_SHARE_ACQUIRE_MODE` is `ShareAcquireMode.BATCH_OPTIMIZED.name()`,
398        // the enum constant's name, not its lower-case `toString()`.
399        .define_with_validator(
400            ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG,
401            Type::String,
402            "BATCH_OPTIMIZED",
403            share_acquire_mode_validator(),
404            Importance::Medium,
405        )
406    // Java ends the chain with `config.providers`; it is left undefined until config providers are translated.
407});
408
409/// Java's `ShareAcknowledgementMode.Validator`: the value must be one of
410/// `implicit` or `explicit`, matched exactly (`ShareAcknowledgementMode.fromString`
411/// checks it against `Utils.enumOptions`, the constants' lower-case
412/// `toString()`).
413fn share_acknowledgement_mode_validator() -> LambdaValidator {
414    LambdaValidator::with(
415        |name, value| {
416            let acknowledgement_mode = as_string(value)?;
417            if acknowledgement_mode.is_some_and(|mode| ["implicit", "explicit"].contains(&mode)) {
418                return Ok(());
419            }
420            Err(config_error(
421                name,
422                value,
423                format!(
424                    "Invalid value `{}` for configuration {name}. The value must either be 'implicit' or 'explicit'.",
425                    acknowledgement_mode.unwrap_or("null")
426                ),
427            ))
428        },
429        || "[implicit, explicit]".to_owned(),
430    )
431}
432
433/// Java's `ShareAcquireMode.Validator`: the value must name one of the
434/// `ShareAcquireMode` constants, case-insensitively (`ShareAcquireMode.of`
435/// upper-cases it before `valueOf`).
436fn share_acquire_mode_validator() -> LambdaValidator {
437    LambdaValidator::with(
438        |name, value| {
439            let acquire_mode = as_string(value)?;
440            if acquire_mode
441                .is_some_and(|mode| ["BATCH_OPTIMIZED", "RECORD_LIMIT"].contains(&mode.to_uppercase().as_str()))
442            {
443                return Ok(());
444            }
445            Err(config_error(
446                name,
447                value,
448                format!(
449                    "Invalid value `{}` for configuration {name}. The value must either be 'batch_optimized' or 'record_limit'.",
450                    acquire_mode.unwrap_or("null")
451                ),
452            ))
453        },
454        || "[batch_optimized, record_limit]".to_owned(),
455    )
456}
457
458/// The consumer configuration keys
459// Documentation for the underlying keys can be found in the Kafka
460// documentation (http://kafka.apache.org/documentation.html#consumerconfigs).
461//
462// As in Java, the configuration is built from the property map by
463// `ConsumerConfig::new`, which parses every key to its `ConfigDef` type,
464// applies its validator and runs Java's `postProcessParsedConfig`. Values are
465// read back with Java's `AbstractConfig` getters (`get_int`, `get_string`,
466// ...). Keys it does not define are accepted, as Java accepts them; the
467// consumer reports them, with the defined keys it does not read, in one line
468// when it is built (`log_unused`).
469//
470// Equality and hashing are Java's `equals` / `hashCode`: two configurations
471// are equal when they were built from equal properties.
472//
473// `Debug` is derived: every secret the typed fields hold is a `Password`
474// inside `sasl_config` / `ssl_config`, which renders as `[hidden]`, and
475// `AbstractConfig`'s `Debug` renders the properties as their key set only: the
476// map also holds keys this client does not define, and Java never prints a raw
477// original value.
478#[derive(Debug)]
479#[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfig")]
480pub struct ConsumerConfig {
481    /// The originals, the parsed values and the keys used so far.
482    pub(crate) config: AbstractConfig,
483    /// The keys the typed fields below were read from, which the consumer
484    /// marks used when it is built from this configuration (`ValuesReader`).
485    pub(crate) read_keys: Vec<&'static str>,
486
487    // --- Group ---
488    /// `group.id` — the consumer group identifier. `None` means no group.
489    pub(crate) group_id: Option<String>,
490    /// `group.instance.id` — static membership identifier.
491    pub(crate) group_instance_id: Option<String>,
492    /// `group.protocol` — `"classic"` (Java default) or `"consumer"` (KIP-848).
493    pub(crate) group_protocol: String,
494    /// `group.remote.assignor` — name of the server-side assignor (KIP-848).
495    pub(crate) group_remote_assignor: Option<String>,
496
497    // --- Polling ---
498    /// `max.poll.records` — the maximum number of records returned in a
499    /// single `poll()` call.
500    pub(crate) max_poll_records: i32,
501    /// `max.poll.interval.ms` — the maximum interval between `poll()` calls.
502    pub(crate) max_poll_interval_ms: i32,
503
504    // --- Connection ---
505    /// `bootstrap.servers` — initial connection list.
506    pub(crate) bootstrap_servers: Vec<String>,
507    /// `client.dns.lookup` — DNS lookup behavior.
508    pub(crate) client_dns_lookup: ClientDnsLookup,
509    /// `client.id` — the client identifier, derived by `new` when unset.
510    pub(crate) client_id: String,
511    /// `client.rack` — the rack identifier for rack-aware fetching.
512    pub(crate) client_rack: String,
513
514    // --- Commit ---
515    /// `enable.auto.commit` — periodically commit offsets in the background.
516    pub(crate) enable_auto_commit: bool,
517    /// `auto.commit.interval.ms` — frequency of auto-commits.
518    pub(crate) auto_commit_interval_ms: i32,
519
520    // --- Offsets ---
521    /// `auto.offset.reset` — the reset strategy when no offset is found.
522    pub(crate) auto_offset_reset: String,
523
524    // --- Fetching ---
525    /// `fetch.min.bytes` — minimum bytes the server should return.
526    pub(crate) fetch_min_bytes: i32,
527    /// `fetch.max.bytes` — maximum bytes the server should return.
528    pub(crate) fetch_max_bytes: i32,
529    /// `fetch.max.wait.ms` — maximum wait time on the server.
530    pub(crate) fetch_max_wait_ms: i32,
531    /// `max.partition.fetch.bytes` — per-partition fetch size cap.
532    pub(crate) max_partition_fetch_bytes: i32,
533    /// `check.crcs` — automatically check CRC32 of consumed records.
534    pub(crate) check_crcs: bool,
535
536    // --- Buffers / Sockets ---
537    /// `send.buffer.bytes` — TCP send buffer size.
538    pub(crate) send_buffer_bytes: i32,
539    /// `receive.buffer.bytes` — TCP receive buffer size.
540    pub(crate) receive_buffer_bytes: i32,
541    /// `socket.connection.setup.timeout.ms`
542    pub(crate) socket_connection_setup_timeout_ms: i64,
543    /// `socket.connection.setup.timeout.max.ms`
544    pub(crate) socket_connection_setup_timeout_max_ms: i64,
545    /// `connections.max.idle.ms`
546    pub(crate) connections_max_idle_ms: i64,
547
548    // --- Reconnect / Retry ---
549    /// `reconnect.backoff.ms`
550    pub(crate) reconnect_backoff_ms: i64,
551    /// `reconnect.backoff.max.ms`
552    pub(crate) reconnect_backoff_max_ms: i64,
553    /// `retry.backoff.ms`
554    pub(crate) retry_backoff_ms: i64,
555    /// `retry.backoff.max.ms`
556    pub(crate) retry_backoff_max_ms: i64,
557    /// `bootstrap.resolve.timeout.ms` (KIP-909)
558    pub(crate) bootstrap_resolve_timeout_ms: i64,
559
560    // --- Timeouts ---
561    /// `request.timeout.ms`
562    pub(crate) request_timeout_ms: i32,
563    /// `default.api.timeout.ms`
564    pub(crate) default_api_timeout_ms: i32,
565
566    // --- Metadata ---
567    /// `metadata.max.age.ms`
568    pub(crate) metadata_max_age_ms: i64,
569    /// `metadata.recovery.strategy`
570    pub(crate) metadata_recovery_strategy: String,
571    /// `metadata.recovery.rebootstrap.trigger.ms`
572    pub(crate) metadata_recovery_rebootstrap_trigger_ms: i64,
573    /// `metadata.cluster.check.enable` (KIP-1242)
574    pub(crate) metadata_cluster_check_enable: bool,
575
576    // --- Misc / Behavior ---
577    /// `exclude.internal.topics`
578    pub(crate) exclude_internal_topics: bool,
579    /// `internal.throw.on.fetch.stable.offset.unsupported`
580    pub(crate) throw_on_fetch_stable_offset_unsupported: bool,
581    /// `isolation.level`
582    pub(crate) isolation_level: String,
583    /// `allow.auto.create.topics`
584    pub(crate) allow_auto_create_topics: bool,
585
586    // --- Metrics ---
587    /// `metrics.sample.window.ms`
588    pub(crate) metrics_sample_window_ms: i64,
589    /// `metrics.num.samples`
590    pub(crate) metrics_num_samples: i32,
591    /// `metrics.recording.level`
592    pub(crate) metrics_recording_level: String,
593
594    // --- Security ---
595    /// `security.protocol` - Protocol used to communicate with brokers.
596    /// Default: `SecurityProtocol::Plaintext`.
597    pub(crate) security_protocol: SecurityProtocol,
598
599    /// SASL configuration (mechanism, JAAS config, credentials).
600    pub(crate) sasl_config: SaslConfigs,
601
602    /// SSL/TLS configuration.
603    pub(crate) ssl_config: SslConfigs,
604}
605
606impl PartialEq for ConsumerConfig {
607    // Java's `AbstractConfig.equals`: the originals are equal.
608    fn eq(&self, other: &Self) -> bool {
609        self.config == other.config
610    }
611}
612
613impl Eq for ConsumerConfig {}
614
615impl Hash for ConsumerConfig {
616    // Java's `AbstractConfig.hashCode`, over the originals.
617    fn hash<H: Hasher>(&self, state: &mut H) {
618        self.config.hash(state);
619    }
620}
621
622impl ConsumerConfig {
623    /// A configuration holding every key's `ConfigDef` default, for the
624    /// crate's own tests, which then write the fields they need.
625    ///
626    /// It is parsed from `bootstrap.servers` alone, without
627    /// `postProcessParsedConfig`, so `client.id` stays empty and
628    /// `enable.auto.commit` keeps its `true` default; `bootstrap_servers` is
629    /// then emptied.
630    ///
631    /// Crate-private, and not `Default`: Java builds a configuration only
632    /// through its constructor, and a public default would hand out one that
633    /// skipped `new`'s validation, including the required `bootstrap.servers`.
634    #[cfg(test)]
635    pub(crate) fn defaults() -> Self {
636        let props = HashMap::from([(Self::BOOTSTRAP_SERVERS_CONFIG.to_string(), "localhost:9092".to_string())]);
637        let config =
638            AbstractConfig::new(&CONFIG, string_originals(&props), false).expect("the ConfigDef defaults are valid");
639        let mut defaults = Self::from_abstract(config).expect("the ConfigDef defaults are valid");
640        defaults.bootstrap_servers = Vec::new();
641        defaults
642    }
643
644    /// Reads the typed fields from the parsed values, recording the keys read
645    /// in `read_keys`.
646    fn from_abstract(config: AbstractConfig) -> Result<Self, Error> {
647        let mut values = ValuesReader::new(&config);
648        let bootstrap_servers = values.list(Self::BOOTSTRAP_SERVERS_CONFIG)?.to_vec();
649        let client_dns_lookup = ClientDnsLookup::parse_config_value(values.string(Self::CLIENT_DNS_LOOKUP_CONFIG)?)?;
650        let client_id = values.string(Self::CLIENT_ID_CONFIG)?.to_string();
651        let client_rack = values.string(Self::CLIENT_RACK_CONFIG)?.to_string();
652        // An empty `group.id` is kept, not coerced to `None`: Java's `ConfigDef`
653        // does not coerce `""` to null, and
654        // `AsyncKafkaConsumer.initializeGroupMetadata` rejects it
655        // (`org.apache.kafka.clients.consumer.internals.AsyncKafkaConsumer#initializeGroupMetadata`).
656        // Coercing it here would turn
657        // that configuration error into a groupless consumer.
658        let group_id = values.string_or_null(Self::GROUP_ID_CONFIG)?.map(str::to_string);
659        let group_instance_id = values.string_or_null(Self::GROUP_INSTANCE_ID_CONFIG)?.map(str::to_string);
660        let group_protocol = values.string(Self::GROUP_PROTOCOL_CONFIG)?.to_string();
661        let group_remote_assignor = values
662            .string_or_null(Self::GROUP_REMOTE_ASSIGNOR_CONFIG)?
663            .filter(|assignor| !assignor.is_empty())
664            .map(str::to_string);
665        let max_poll_records = values.int(Self::MAX_POLL_RECORDS_CONFIG)?;
666        let max_poll_interval_ms = values.int(Self::MAX_POLL_INTERVAL_MS_CONFIG)?;
667        let enable_auto_commit = values.boolean(Self::ENABLE_AUTO_COMMIT_CONFIG)?;
668        let auto_commit_interval_ms = values.int(Self::AUTO_COMMIT_INTERVAL_MS_CONFIG)?;
669        let auto_offset_reset = values.string(Self::AUTO_OFFSET_RESET_CONFIG)?.to_string();
670        let fetch_min_bytes = values.int(Self::FETCH_MIN_BYTES_CONFIG)?;
671        let fetch_max_bytes = values.int(Self::FETCH_MAX_BYTES_CONFIG)?;
672        let fetch_max_wait_ms = values.int(Self::FETCH_MAX_WAIT_MS_CONFIG)?;
673        let max_partition_fetch_bytes = values.int(Self::MAX_PARTITION_FETCH_BYTES_CONFIG)?;
674        let check_crcs = values.boolean(Self::CHECK_CRCS_CONFIG)?;
675        let send_buffer_bytes = values.int(Self::SEND_BUFFER_CONFIG)?;
676        let receive_buffer_bytes = values.int(Self::RECEIVE_BUFFER_CONFIG)?;
677        let socket_connection_setup_timeout_ms = values.long(Self::SOCKET_CONNECTION_SETUP_TIMEOUT_MS_CONFIG)?;
678        let socket_connection_setup_timeout_max_ms =
679            values.long(Self::SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS_CONFIG)?;
680        let connections_max_idle_ms = values.long(Self::CONNECTIONS_MAX_IDLE_MS_CONFIG)?;
681        let reconnect_backoff_ms = values.long(Self::RECONNECT_BACKOFF_MS_CONFIG)?;
682        let reconnect_backoff_max_ms = values.long(Self::RECONNECT_BACKOFF_MAX_MS_CONFIG)?;
683        let retry_backoff_ms = values.long(Self::RETRY_BACKOFF_MS_CONFIG)?;
684        let retry_backoff_max_ms = values.long(Self::RETRY_BACKOFF_MAX_MS_CONFIG)?;
685        let bootstrap_resolve_timeout_ms = values.long(Self::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG)?;
686        let request_timeout_ms = values.int(Self::REQUEST_TIMEOUT_MS_CONFIG)?;
687        let default_api_timeout_ms = values.int(Self::DEFAULT_API_TIMEOUT_MS_CONFIG)?;
688        let metadata_max_age_ms = values.long(Self::METADATA_MAX_AGE_CONFIG)?;
689        let metadata_recovery_strategy = values
690            .string(CommonClientConfigs::METADATA_RECOVERY_STRATEGY_CONFIG)?
691            .to_string();
692        let metadata_recovery_rebootstrap_trigger_ms =
693            values.long(CommonClientConfigs::METADATA_RECOVERY_REBOOTSTRAP_TRIGGER_MS_CONFIG)?;
694        let metadata_cluster_check_enable =
695            values.boolean(CommonClientConfigs::METADATA_CLUSTER_CHECK_ENABLE_CONFIG)?;
696        let exclude_internal_topics = values.boolean(Self::EXCLUDE_INTERNAL_TOPICS_CONFIG)?;
697        let throw_on_fetch_stable_offset_unsupported =
698            values.boolean(Self::THROW_ON_FETCH_STABLE_OFFSET_UNSUPPORTED)?;
699        let isolation_level = values.string(Self::ISOLATION_LEVEL_CONFIG)?.to_string();
700        let allow_auto_create_topics = values.boolean(Self::ALLOW_AUTO_CREATE_TOPICS_CONFIG)?;
701        let metrics_sample_window_ms = values.long(Self::METRICS_SAMPLE_WINDOW_MS_CONFIG)?;
702        let metrics_num_samples = values.int(Self::METRICS_NUM_SAMPLES_CONFIG)?;
703        let metrics_recording_level = values.string(Self::METRICS_RECORDING_LEVEL_CONFIG)?.to_string();
704        let protocol = values.string(CommonClientConfigs::SECURITY_PROTOCOL_CONFIG)?;
705        // The validator accepted `protocol` case-insensitively.
706        let security_protocol = SecurityProtocol::for_name(protocol)
707            .ok_or_else(|| Error::local_illegal_state(format!("Unknown security protocol {protocol}")))?;
708
709        // The `ssl.*` and `sasl.*` keys are read by Java's channel builder only
710        // for a protocol that uses them (`ChannelBuilders.clientChannelBuilder`),
711        // so only then are they marked used.
712        let mut ssl_values = ValuesReader::new(&config);
713        let ssl_config = SslConfigs::from_values(&mut ssl_values)?;
714        if matches!(security_protocol, SecurityProtocol::Ssl | SecurityProtocol::SaslSsl) {
715            values.append(ssl_values);
716        }
717        let mut sasl_values = ValuesReader::new(&config);
718        let sasl_config = SaslConfigs::from_values(&mut sasl_values)?;
719        if matches!(security_protocol, SecurityProtocol::SaslPlaintext | SecurityProtocol::SaslSsl) {
720            values.append(sasl_values);
721        }
722        let read_keys = values.into_read_keys();
723
724        Ok(Self {
725            config,
726            read_keys,
727            group_id,
728            group_instance_id,
729            group_protocol,
730            group_remote_assignor,
731            max_poll_records,
732            max_poll_interval_ms,
733            bootstrap_servers,
734            client_dns_lookup,
735            client_id,
736            client_rack,
737            enable_auto_commit,
738            auto_commit_interval_ms,
739            auto_offset_reset,
740            fetch_min_bytes,
741            fetch_max_bytes,
742            fetch_max_wait_ms,
743            max_partition_fetch_bytes,
744            check_crcs,
745            send_buffer_bytes,
746            receive_buffer_bytes,
747            socket_connection_setup_timeout_ms,
748            socket_connection_setup_timeout_max_ms,
749            connections_max_idle_ms,
750            reconnect_backoff_ms,
751            reconnect_backoff_max_ms,
752            retry_backoff_ms,
753            retry_backoff_max_ms,
754            bootstrap_resolve_timeout_ms,
755            request_timeout_ms,
756            default_api_timeout_ms,
757            metadata_max_age_ms,
758            metadata_recovery_strategy,
759            metadata_recovery_rebootstrap_trigger_ms,
760            metadata_cluster_check_enable,
761            exclude_internal_topics,
762            throw_on_fetch_stable_offset_unsupported,
763            isolation_level,
764            allow_auto_create_topics,
765            metrics_sample_window_ms,
766            metrics_num_samples,
767            metrics_recording_level,
768            security_protocol,
769            sasl_config,
770            ssl_config,
771        })
772    }
773}
774
775impl ConsumerConfig {
776    // --- Config key constants (matching Java string keys exactly) ---
777
778    /// Config key: `group.id`.
779    pub const GROUP_ID_CONFIG: &'static str = "group.id";
780    /// Config key: `group.instance.id`.
781    pub const GROUP_INSTANCE_ID_CONFIG: &'static str = "group.instance.id";
782    /// Config key: `group.protocol`.
783    pub const GROUP_PROTOCOL_CONFIG: &'static str = "group.protocol";
784    /// Default value of `group.protocol`. Matches Java's `DEFAULT_GROUP_PROTOCOL`.
785    pub const DEFAULT_GROUP_PROTOCOL: &'static str = "classic";
786    /// Config key: `group.remote.assignor`.
787    pub const GROUP_REMOTE_ASSIGNOR_CONFIG: &'static str = "group.remote.assignor";
788
789    /// Config key: `max.poll.records`.
790    pub const MAX_POLL_RECORDS_CONFIG: &'static str = "max.poll.records";
791    /// Default value of `max.poll.records`.
792    pub const DEFAULT_MAX_POLL_RECORDS: i32 = 500;
793    /// Config key: `max.poll.interval.ms`.
794    pub const MAX_POLL_INTERVAL_MS_CONFIG: &'static str = "max.poll.interval.ms";
795    /// Config key: `session.timeout.ms`.
796    pub const SESSION_TIMEOUT_MS_CONFIG: &'static str = "session.timeout.ms";
797    /// Config key: `heartbeat.interval.ms`.
798    pub const HEARTBEAT_INTERVAL_MS_CONFIG: &'static str = "heartbeat.interval.ms";
799
800    /// Config key: `bootstrap.servers`.
801    pub const BOOTSTRAP_SERVERS_CONFIG: &'static str = "bootstrap.servers";
802    /// Config key: `client.dns.lookup`. Java's `ConsumerConfig` declares it as its
803    /// own public alias of `CommonClientConfigs.CLIENT_DNS_LOOKUP_CONFIG`.
804    pub const CLIENT_DNS_LOOKUP_CONFIG: &'static str = CommonClientConfigs::CLIENT_DNS_LOOKUP_CONFIG;
805    /// Config key: `client.id`.
806    pub const CLIENT_ID_CONFIG: &'static str = "client.id";
807    /// Config key: `client.rack`.
808    pub const CLIENT_RACK_CONFIG: &'static str = crate::CommonClientConfigs::CLIENT_RACK_CONFIG;
809    /// Default value of `client.rack`: no rack.
810    pub const DEFAULT_CLIENT_RACK: &'static str = crate::CommonClientConfigs::DEFAULT_CLIENT_RACK;
811
812    /// Config key: `enable.auto.commit`.
813    pub const ENABLE_AUTO_COMMIT_CONFIG: &'static str = "enable.auto.commit";
814    /// Config key: `auto.commit.interval.ms`.
815    pub const AUTO_COMMIT_INTERVAL_MS_CONFIG: &'static str = "auto.commit.interval.ms";
816
817    /// Config key: `partition.assignment.strategy`.
818    pub const PARTITION_ASSIGNMENT_STRATEGY_CONFIG: &'static str = "partition.assignment.strategy";
819    /// Config key: `auto.offset.reset`.
820    ///
821    /// What to do when there is no initial offset in Kafka or if the current
822    /// offset does not exist any more on the server (e.g. because that data has
823    /// been deleted):
824    ///
825    /// * `earliest`: automatically reset the offset to the earliest offset
826    /// * `latest`: automatically reset the offset to the latest offset
827    /// * `by_duration:<duration>`: automatically reset the offset to a
828    ///   configured `<duration>` from the current timestamp. `<duration>` must be
829    ///   specified in ISO8601 format (`PnDTnHnMn.nS`). Negative duration is not
830    ///   allowed.
831    /// * `none`: return an error to the consumer if no previous offset is found
832    ///   for the consumer's group
833    /// * anything else: return an error to the consumer.
834    ///
835    /// Note that increasing a topic's partition count while this config is set
836    /// to `latest` may cause silent message loss: producers may begin appending
837    /// records to a newly created partition before the consumer discovers it,
838    /// and `latest` resets the position to the log end offset, skipping any
839    /// records produced during that discovery gap.
840    ///
841    /// To avoid this, prefer `by_duration:<duration>`. When a partition has no
842    /// committed offset, `by_duration` determines the starting position by
843    /// issuing a `ListOffsets` lookup for `now() - duration`. If the target
844    /// timestamp is earlier than the partition's creation time, the lookup
845    /// returns the partition's start offset, ensuring that records produced
846    /// during the discovery window are still consumed. Size the duration to
847    /// cover the worst-case partition-discovery latency for the group protocol
848    /// in use:
849    ///
850    /// * With the `consumer` group protocol (KIP-848), newly assigned
851    ///   partitions are pushed on the next group heartbeat, so a value at least
852    ///   as large as `group.consumer.heartbeat.interval.ms` (server default
853    ///   5000 ms) is sufficient, for example `by_duration:PT5S`.
854    /// * With the `classic` group protocol, new partitions are discovered
855    ///   through periodic metadata refresh and a subsequent rebalance, so the
856    ///   duration must exceed `metadata.max.age.ms` (client default 300000 ms)
857    ///   plus the rebalance time, for example `by_duration:PT6M`.
858    ///
859    /// Consumers with a valid committed offset are unaffected. The reset
860    /// applies only to partitions whose offset is missing or out of range, so
861    /// `by_duration` does not force existing consumers to replay historical
862    /// data on restart.
863    ///
864    /// (Text of Java's `ConsumerConfig.AUTO_OFFSET_RESET_DOC`,
865    /// as rewritten by KAFKA-20539. This client
866    /// implements only the `consumer` group protocol; the `classic` item is kept
867    /// because the setting itself is protocol-independent.)
868    pub const AUTO_OFFSET_RESET_CONFIG: &'static str = "auto.offset.reset";
869
870    /// Config key: `fetch.min.bytes`.
871    pub const FETCH_MIN_BYTES_CONFIG: &'static str = "fetch.min.bytes";
872    /// Default value of `fetch.min.bytes`.
873    pub const DEFAULT_FETCH_MIN_BYTES: i32 = 1;
874    /// Config key: `fetch.max.bytes`.
875    pub const FETCH_MAX_BYTES_CONFIG: &'static str = "fetch.max.bytes";
876    /// Default value of `fetch.max.bytes`.
877    pub const DEFAULT_FETCH_MAX_BYTES: i32 = 50 * 1024 * 1024;
878    /// Config key: `fetch.max.wait.ms`.
879    pub const FETCH_MAX_WAIT_MS_CONFIG: &'static str = "fetch.max.wait.ms";
880    /// Default value of `fetch.max.wait.ms`.
881    pub const DEFAULT_FETCH_MAX_WAIT_MS: i32 = 500;
882    /// Config key: `max.partition.fetch.bytes`.
883    pub const MAX_PARTITION_FETCH_BYTES_CONFIG: &'static str = "max.partition.fetch.bytes";
884    /// Default value of `max.partition.fetch.bytes`.
885    pub const DEFAULT_MAX_PARTITION_FETCH_BYTES: i32 = 1024 * 1024;
886    /// Config key: `check.crcs`.
887    pub const CHECK_CRCS_CONFIG: &'static str = "check.crcs";
888
889    /// Config key: `send.buffer.bytes`.
890    pub const SEND_BUFFER_CONFIG: &'static str = "send.buffer.bytes";
891    /// Config key: `receive.buffer.bytes`.
892    pub const RECEIVE_BUFFER_CONFIG: &'static str = "receive.buffer.bytes";
893    /// Config key: `socket.connection.setup.timeout.ms`.
894    pub const SOCKET_CONNECTION_SETUP_TIMEOUT_MS_CONFIG: &'static str = "socket.connection.setup.timeout.ms";
895    /// Config key: `socket.connection.setup.timeout.max.ms`.
896    pub const SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS_CONFIG: &'static str = "socket.connection.setup.timeout.max.ms";
897    /// Config key: `connections.max.idle.ms`.
898    pub const CONNECTIONS_MAX_IDLE_MS_CONFIG: &'static str = "connections.max.idle.ms";
899
900    /// Config key: `reconnect.backoff.ms`.
901    pub const RECONNECT_BACKOFF_MS_CONFIG: &'static str = "reconnect.backoff.ms";
902    /// Config key: `reconnect.backoff.max.ms`.
903    pub const RECONNECT_BACKOFF_MAX_MS_CONFIG: &'static str = "reconnect.backoff.max.ms";
904    /// Config key: `retry.backoff.ms`.
905    pub const RETRY_BACKOFF_MS_CONFIG: &'static str = "retry.backoff.ms";
906    /// Config key: `retry.backoff.max.ms`.
907    pub const RETRY_BACKOFF_MAX_MS_CONFIG: &'static str = "retry.backoff.max.ms";
908    /// Config key: `bootstrap.resolve.timeout.ms` (KIP-909): selects the
909    /// client's bootstrap DNS resolution mode. `0` (the default) resolves
910    /// `bootstrap.servers` synchronously when the consumer is created, and a
911    /// resolution failure fails creation with a config error. A positive value
912    /// resolves asynchronously, retrying for at most this long before every
913    /// subsequent call fails with an unrecoverable
914    /// [`BootstrapResolutionError`](crate::common::errors::BootstrapResolutionError);
915    /// the consumer must then be closed and re-created. Setting a positive
916    /// value enables an evolving feature whose compatibility may be broken in a
917    /// minor release.
918    pub const BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG: &'static str =
919        CommonClientConfigs::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG;
920
921    /// Config key: `request.timeout.ms`.
922    pub const REQUEST_TIMEOUT_MS_CONFIG: &'static str = "request.timeout.ms";
923    /// Config key: `default.api.timeout.ms`.
924    pub const DEFAULT_API_TIMEOUT_MS_CONFIG: &'static str = "default.api.timeout.ms";
925
926    /// Config key: `metadata.max.age.ms`.
927    pub const METADATA_MAX_AGE_CONFIG: &'static str = "metadata.max.age.ms";
928
929    /// Config key: `exclude.internal.topics`.
930    pub const EXCLUDE_INTERNAL_TOPICS_CONFIG: &'static str = "exclude.internal.topics";
931    /// Default value of `exclude.internal.topics`.
932    pub const DEFAULT_EXCLUDE_INTERNAL_TOPICS: bool = true;
933    /// Config key: `internal.throw.on.fetch.stable.offset.unsupported`.
934    ///
935    /// Package-private in Java, like every
936    /// `internal.*` key: Java may remove it in a minor release.
937    pub(crate) const THROW_ON_FETCH_STABLE_OFFSET_UNSUPPORTED: &'static str =
938        "internal.throw.on.fetch.stable.offset.unsupported";
939    /// Config key: `isolation.level`.
940    pub const ISOLATION_LEVEL_CONFIG: &'static str = "isolation.level";
941    /// Default for `isolation.level`: `read_committed`, so a consumer returns
942    /// only the transactional records that were committed.
943    ///
944    /// WARNING: Java's default is `read_uncommitted`. `read_committed` is
945    /// librdkafka's default, which users of the other Confluent clients
946    /// expect. Set `isolation.level=read_uncommitted` for Java's behaviour.
947    pub const DEFAULT_ISOLATION_LEVEL: &'static str = "read_committed";
948    /// Config key: `allow.auto.create.topics`.
949    pub const ALLOW_AUTO_CREATE_TOPICS_CONFIG: &'static str = "allow.auto.create.topics";
950    /// Default value of `allow.auto.create.topics`.
951    pub const DEFAULT_ALLOW_AUTO_CREATE_TOPICS: bool = true;
952
953    /// Config key: `metrics.sample.window.ms`.
954    pub const METRICS_SAMPLE_WINDOW_MS_CONFIG: &'static str = "metrics.sample.window.ms";
955    /// Config key: `metrics.num.samples`.
956    pub const METRICS_NUM_SAMPLES_CONFIG: &'static str = "metrics.num.samples";
957    /// Config key: `metrics.recording.level`.
958    pub const METRICS_RECORDING_LEVEL_CONFIG: &'static str = "metrics.recording.level";
959
960    /// Config key: `share.acknowledgement.mode`.
961    pub const SHARE_ACKNOWLEDGEMENT_MODE_CONFIG: &'static str = "share.acknowledgement.mode";
962    /// Config key: `share.acquire.mode`.
963    pub const SHARE_ACQUIRE_MODE_CONFIG: &'static str = "share.acquire.mode";
964
965    /// Constructs a new ConsumerConfig with the given properties.
966    ///
967    /// # Arguments
968    ///
969    /// - `props` - The consumer configuration properties
970    ///
971    /// # Errors
972    ///
973    /// - `Error::is_config_error` if a value is invalid, the settings
974    ///   conflict, or `sasl.mechanism` is not `PLAIN` under a SASL
975    ///   `security.protocol`.
976    /// - `Error::is_invalid_configuration_error` if `enable.auto.commit` is
977    ///   `true` without a `group.id`.
978    ///
979    /// # Translation notes
980    ///
981    /// WARNING: `sasl.mechanism` must be `PLAIN`.
982    // Every value is parsed to its `ConfigDef` type and checked by its
983    // validator, then Java's `postProcessParsedConfig` runs:
984    // `postValidateSaslMechanismConfig`, `warnDisablingExponentialBackoff`,
985    // `postProcessReconnectBackoffConfigs`, `maybeOverrideClientId` (an unset
986    // or empty `client.id` becomes
987    // `consumer-<group.id>-<group.instance.id or a process-wide sequence>`),
988    // `maybeOverrideEnableAutoCommit` (`enable.auto.commit` defaults to `false`
989    // without a `group.id`), `checkUnsupportedConfigsPostProcess` and the
990    // `connections.max.idle.ms` warning.
991    //
992    // The config errors: a value fails to parse or validate, with
993    // `ConfigDef`'s message; `bootstrap.servers` is missing; a SASL
994    // `security.protocol` has an empty `sasl.mechanism`; a key the
995    // `group.protocol` does not support is set (`group.remote.assignor`,
996    // `share.acknowledgement.mode` or `share.acquire.mode` with `classic`;
997    // `partition.assignment.strategy`, `heartbeat.interval.ms`,
998    // `session.timeout.ms` or the two share keys with `consumer`). An invalid
999    // `group.instance.id` fails with `JoinGroupRequest.validateGroupInstanceId`'s
1000    // error when `client.id` is derived from it.
1001    //
1002    // A SASL `security.protocol` with a `sasl.mechanism` other than `PLAIN`
1003    // fails with a config error: Java accepts any mechanism name, but this
1004    // client implements only PLAIN, and a client built with another one
1005    // would fail only later, at authentication.
1006    #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfig#ConsumerConfig(Map)")]
1007    pub fn new(props: &HashMap<String, String>) -> Result<Self, Error> {
1008        Self::build(string_originals(props), None)
1009    }
1010
1011    /// The configuration of one more client given this one: the same
1012    /// properties and this configuration's resolved `client.id`, with its
1013    /// own record of the keys used, as [`Self::new`] leaves it.
1014    ///
1015    /// Java hands each client the `ConsumerConfig` it is given, which took
1016    /// its one number from the `client.id` sequence when it was built
1017    /// (`org.apache.kafka.clients.consumer.internals.AsyncKafkaConsumer`'s
1018    /// constructor reads it), so clients given one
1019    /// configuration share its id. Building the configuration again would
1020    /// take a second number; this copy does not. It is not `Clone`
1021    /// (CLAUDE.md §3).
1022    ///
1023    /// Java's client logs nothing about the configuration it is given, so the
1024    /// copy logs none of the warnings [`Self::new`] logs: it makes the same
1025    /// parse and the same reads, marking the same keys used and failing the
1026    /// same way, but logs neither the parse's warning that a list's duplicate
1027    /// values are removed, nor the post-processing's warnings (the reconnect
1028    /// and retry backoff, the connection setup timeout,
1029    /// `connections.max.idle.ms` below `max.poll.interval.ms`). The client given the copy logs its
1030    /// one unused-keys line
1031    /// ([`Self::log_unused`]), as Java's client constructor does.
1032    ///
1033    /// # Errors
1034    ///
1035    /// Fails as [`Self::new`] does; this configuration's properties already
1036    /// passed it, so it does not in practice.
1037    #[cfg_attr(
1038        not(any(feature = "ffi", test)),
1039        expect(dead_code, reason = "only the C config handle copies a configuration")
1040    )]
1041    pub(crate) fn copy_for_client(&self) -> Result<Self, Error> {
1042        Self::build(self.config.originals_map().clone(), Some(&self.client_id))
1043    }
1044
1045    /// Java's `ConsumerConfig(Map)` without its logging: parses `originals`
1046    /// and post-processes them, `resolved_client_id` standing in for the
1047    /// sequence number `client.id` would take. With a `resolved_client_id`
1048    /// (a copy) nothing is logged: not the parse's duplicate-values warning,
1049    /// not the post-processing's lines.
1050    fn build(originals: ConfigMap, resolved_client_id: Option<&str>) -> Result<Self, Error> {
1051        let log_warnings = resolved_client_id.is_none();
1052        let config = AbstractConfig::with_hooks_and_log_warnings(
1053            &CONFIG,
1054            originals,
1055            false,
1056            log_warnings,
1057            "ConsumerConfig",
1058            |originals| originals,
1059            |config| Self::post_process_parsed_config(config, resolved_client_id),
1060        )?;
1061        Self::from_abstract(config)
1062    }
1063
1064    /// Java's `ConsumerConfig.postProcessParsedConfig`: the values that
1065    /// replace the parsed ones. A `resolved_client_id` is the id a derived
1066    /// `client.id` takes, in place of a new one; it marks a copy for one more
1067    /// client ([`Self::copy_for_client`]), whose reads are the same but which
1068    /// logs none of the warnings: its original logged them once.
1069    fn post_process_parsed_config(
1070        config: &AbstractConfig,
1071        resolved_client_id: Option<&str>,
1072    ) -> Result<ConfigMap, Error> {
1073        let log_warnings = resolved_client_id.is_none();
1074        CommonClientConfigs::post_validate_sasl_mechanism_config(config)?;
1075        CommonClientConfigs::post_validate_supported_sasl_mechanism(config)?;
1076        CommonClientConfigs::warn_disabling_exponential_backoff(config, log_warnings)?;
1077        let mut refined_configs =
1078            CommonClientConfigs::post_process_reconnect_backoff_configs(config, config.parsed_values(), log_warnings);
1079        Self::maybe_override_client_id_with_configs(config, &mut refined_configs, resolved_client_id)?;
1080        Self::maybe_override_enable_auto_commit(config, &mut refined_configs)?;
1081        Self::check_unsupported_configs_post_process(config)?;
1082        Self::warn_if_connections_max_idle_ms_lower_than_max_poll_interval_ms(config, log_warnings)?;
1083        Ok(refined_configs)
1084    }
1085
1086    /// Java's `warnIfConnectionsMaxIdleMsLowerThanMaxPollIntervalMs`. With
1087    /// `log_warnings` false it makes the same reads but logs nothing.
1088    fn warn_if_connections_max_idle_ms_lower_than_max_poll_interval_ms(
1089        config: &AbstractConfig,
1090        log_warnings: bool,
1091    ) -> Result<(), Error> {
1092        let group_protocol = config.get_string(Self::GROUP_PROTOCOL_CONFIG)?;
1093        if !group_protocol.is_some_and(|protocol| protocol.eq_ignore_ascii_case("CLASSIC")) {
1094            return Ok(());
1095        }
1096        let connections_max_idle_ms = config.get_long(Self::CONNECTIONS_MAX_IDLE_MS_CONFIG)?.unwrap_or_default();
1097        let max_poll_interval_ms = config.get_int(Self::MAX_POLL_INTERVAL_MS_CONFIG)?.unwrap_or_default();
1098        if log_warnings && connections_max_idle_ms >= 0 && connections_max_idle_ms < i64::from(max_poll_interval_ms) {
1099            warn!(
1100                "Configuration '{}' with value '{}' is lower than configuration '{}' with value '{}'. This may cause \
1101                 the connection to the group coordinator to be closed during an ongoing rebalance, which can \
1102                 prolong or disrupt group rejoin.",
1103                Self::CONNECTIONS_MAX_IDLE_MS_CONFIG,
1104                connections_max_idle_ms,
1105                Self::MAX_POLL_INTERVAL_MS_CONFIG,
1106                max_poll_interval_ms
1107            );
1108        }
1109        Ok(())
1110    }
1111
1112    /// Java's private `maybeOverrideClientId(Map)`: when `client.id` is unset
1113    /// or empty, derives `consumer-<group.id>-<group.instance.id>` for a static
1114    /// member, otherwise `consumer-<group.id>-<n>` from the process-wide
1115    /// sequence, and puts it in `configs`.
1116    ///
1117    /// Java formats a missing `group.id` with `String.format("%s", null)`,
1118    /// which yields the literal `null`; that is preserved (`consumer-null-1`) so
1119    /// the wire `client_id` matches a Java consumer with the same
1120    /// configuration.
1121    ///
1122    /// A `resolved_client_id` (the id of the configuration this one copies)
1123    /// is put in `configs` instead of a derived one, after the same reads and
1124    /// checks.
1125    fn maybe_override_client_id_with_configs(
1126        config: &AbstractConfig,
1127        configs: &mut ConfigMap,
1128        resolved_client_id: Option<&str>,
1129    ) -> Result<(), Error> {
1130        let client_id = config.get_string(Self::CLIENT_ID_CONFIG)?;
1131        if client_id.is_none_or(str::is_empty) {
1132            let group_id = config.get_string(Self::GROUP_ID_CONFIG)?;
1133            let group_instance_id = config.get_string(Self::GROUP_INSTANCE_ID_CONFIG)?;
1134            if let Some(group_instance_id) = group_instance_id {
1135                JoinGroupRequest::validate_group_instance_id(group_instance_id)?;
1136            }
1137            let generated_client_id = match resolved_client_id {
1138                Some(resolved_client_id) => resolved_client_id.to_string(),
1139                None => {
1140                    let group_instance_id_part = match group_instance_id {
1141                        Some(group_instance_id) => group_instance_id.to_string(),
1142                        None => CONSUMER_CLIENT_ID_SEQUENCE.fetch_add(1, atomic::Ordering::Relaxed).to_string(),
1143                    };
1144                    format!("consumer-{}-{group_instance_id_part}", group_id.unwrap_or("null"))
1145                },
1146            };
1147            configs.insert(
1148                Self::CLIENT_ID_CONFIG.to_string(),
1149                Some(ParsedValue::String(generated_client_id)),
1150            );
1151        }
1152        Ok(())
1153    }
1154
1155    /// Java's `maybeOverrideEnableAutoCommit`: without a `group.id`,
1156    /// `enable.auto.commit` becomes `false` unless it was set, and setting it
1157    /// to `true` is an error.
1158    fn maybe_override_enable_auto_commit(config: &AbstractConfig, configs: &mut ConfigMap) -> Result<(), Error> {
1159        let group_id = config.get_string(Self::GROUP_ID_CONFIG)?;
1160        let auto_commit_set = config.originals_map().contains_key(Self::ENABLE_AUTO_COMMIT_CONFIG);
1161        let enable_auto_commit = if auto_commit_set {
1162            config.get_boolean(Self::ENABLE_AUTO_COMMIT_CONFIG)?.unwrap_or_default()
1163        } else {
1164            false
1165        };
1166        if group_id.is_none() {
1167            // overwrite in case of default group id where the config is not explicitly provided
1168            if !auto_commit_set {
1169                configs.insert(Self::ENABLE_AUTO_COMMIT_CONFIG.to_string(), Some(ParsedValue::Boolean(false)));
1170            } else if enable_auto_commit {
1171                return Err(Error::InvalidConfiguration(InvalidConfigurationError::new(format!(
1172                    "{} cannot be set to true when default group id (null) is used.",
1173                    Self::ENABLE_AUTO_COMMIT_CONFIG
1174                ))));
1175            }
1176        }
1177        Ok(())
1178    }
1179
1180    /// Java's `checkUnsupportedConfigsPostProcess`: the keys the configured
1181    /// `group.protocol` does not support must not be set.
1182    fn check_unsupported_configs_post_process(config: &AbstractConfig) -> Result<(), Error> {
1183        let group_protocol = config.get_string(Self::GROUP_PROTOCOL_CONFIG)?.unwrap_or_default();
1184        if group_protocol.eq_ignore_ascii_case("CLASSIC") {
1185            Self::check_unsupported_configs(config, "CLASSIC", &CLASSIC_PROTOCOL_UNSUPPORTED_CONFIGS)
1186        } else if group_protocol.eq_ignore_ascii_case("CONSUMER") {
1187            Self::check_unsupported_configs(config, "CONSUMER", &CONSUMER_PROTOCOL_UNSUPPORTED_CONFIGS)
1188        } else {
1189            Ok(())
1190        }
1191    }
1192
1193    /// Java's private `checkUnsupportedConfigsPostProcess(GroupProtocol, List)`.
1194    fn check_unsupported_configs(
1195        config: &AbstractConfig,
1196        group_protocol: &str,
1197        unsupported_configs: &[&str],
1198    ) -> Result<(), Error> {
1199        let mut invalid_configs = Vec::new();
1200        for &config_name in unsupported_configs {
1201            // Java reads the key through `originals()`, a `RecordingMap`
1202            // whose `get` marks it used.
1203            config.ignore(config_name);
1204            if let Some(Some(value)) = config.originals_map().get(config_name)
1205                && !Utils::is_blank(Some(&value.to_string()))
1206            {
1207                invalid_configs.push(config_name);
1208            }
1209        }
1210        if invalid_configs.is_empty() {
1211            Ok(())
1212        } else {
1213            Err(Error::config_message(format!(
1214                "{} cannot be set when {}={group_protocol}",
1215                invalid_configs.join(", "),
1216                Self::GROUP_PROTOCOL_CONFIG
1217            )))
1218        }
1219    }
1220
1221    /// The `SHORT` configuration `key`. Reading a key marks it used
1222    /// ([`Self::unused`]).
1223    ///
1224    /// # Arguments
1225    ///
1226    /// - `key` - the configuration name
1227    ///
1228    /// # Returns
1229    ///
1230    /// The value, `None` for Java's `null`.
1231    ///
1232    /// # Errors
1233    ///
1234    /// - `Error::is_config_error` with Java's `Unknown configuration '<key>'`
1235    ///   if `key` is not a configuration this class defines.
1236    /// - `Error::is_local_illegal_argument_error` if `key` is a configuration
1237    ///   of another type, where Java's cast fails with a `ClassCastException`.
1238    #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getShort")]
1239    pub fn get_short(&self, key: &str) -> Result<Option<i16>, Error> {
1240        self.config.get_short(key)
1241    }
1242
1243    /// The `INT` configuration `key`. Reading a key marks it used
1244    /// ([`Self::unused`]).
1245    ///
1246    /// # Arguments
1247    ///
1248    /// - `key` - the configuration name
1249    ///
1250    /// # Returns
1251    ///
1252    /// The value, `None` for Java's `null`.
1253    ///
1254    /// # Errors
1255    ///
1256    /// - `Error::is_config_error` with Java's `Unknown configuration '<key>'`
1257    ///   if `key` is not a configuration this class defines.
1258    /// - `Error::is_local_illegal_argument_error` if `key` is a configuration
1259    ///   of another type, where Java's cast fails with a `ClassCastException`.
1260    #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getInt")]
1261    pub fn get_int(&self, key: &str) -> Result<Option<i32>, Error> {
1262        self.config.get_int(key)
1263    }
1264
1265    /// The `LONG` configuration `key`. Reading a key marks it used
1266    /// ([`Self::unused`]).
1267    ///
1268    /// # Arguments
1269    ///
1270    /// - `key` - the configuration name
1271    ///
1272    /// # Returns
1273    ///
1274    /// The value, `None` for Java's `null`.
1275    ///
1276    /// # Errors
1277    ///
1278    /// - `Error::is_config_error` with Java's `Unknown configuration '<key>'`
1279    ///   if `key` is not a configuration this class defines.
1280    /// - `Error::is_local_illegal_argument_error` if `key` is a configuration
1281    ///   of another type, where Java's cast fails with a `ClassCastException`.
1282    #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getLong")]
1283    pub fn get_long(&self, key: &str) -> Result<Option<i64>, Error> {
1284        self.config.get_long(key)
1285    }
1286
1287    /// The `DOUBLE` configuration `key`. Reading a key marks it used
1288    /// ([`Self::unused`]).
1289    ///
1290    /// # Arguments
1291    ///
1292    /// - `key` - the configuration name
1293    ///
1294    /// # Returns
1295    ///
1296    /// The value, `None` for Java's `null`.
1297    ///
1298    /// # Errors
1299    ///
1300    /// - `Error::is_config_error` with Java's `Unknown configuration '<key>'`
1301    ///   if `key` is not a configuration this class defines.
1302    /// - `Error::is_local_illegal_argument_error` if `key` is a configuration
1303    ///   of another type, where Java's cast fails with a `ClassCastException`.
1304    #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getDouble")]
1305    pub fn get_double(&self, key: &str) -> Result<Option<f64>, Error> {
1306        self.config.get_double(key)
1307    }
1308
1309    /// The `LIST` configuration `key`, borrowed from the parsed values.
1310    /// Reading a key marks it used ([`Self::unused`]).
1311    ///
1312    /// # Arguments
1313    ///
1314    /// - `key` - the configuration name
1315    ///
1316    /// # Returns
1317    ///
1318    /// The value, `None` for Java's `null`.
1319    ///
1320    /// # Errors
1321    ///
1322    /// - `Error::is_config_error` with Java's `Unknown configuration '<key>'`
1323    ///   if `key` is not a configuration this class defines.
1324    /// - `Error::is_local_illegal_argument_error` if `key` is a configuration
1325    ///   of another type, where Java's cast fails with a `ClassCastException`.
1326    #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getList")]
1327    pub fn get_list(&self, key: &str) -> Result<Option<&[String]>, Error> {
1328        self.config.get_list(key)
1329    }
1330
1331    /// The `BOOLEAN` configuration `key`. Reading a key marks it used
1332    /// ([`Self::unused`]).
1333    ///
1334    /// `enable.auto.commit` answers the value `postProcessParsedConfig`
1335    /// settled on: `false` without a `group.id` when it was not set.
1336    ///
1337    /// # Arguments
1338    ///
1339    /// - `key` - the configuration name
1340    ///
1341    /// # Returns
1342    ///
1343    /// The value, `None` for Java's `null`.
1344    ///
1345    /// # Errors
1346    ///
1347    /// - `Error::is_config_error` with Java's `Unknown configuration '<key>'`
1348    ///   if `key` is not a configuration this class defines.
1349    /// - `Error::is_local_illegal_argument_error` if `key` is a configuration
1350    ///   of another type, where Java's cast fails with a `ClassCastException`.
1351    #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getBoolean")]
1352    pub fn get_boolean(&self, key: &str) -> Result<Option<bool>, Error> {
1353        self.config.get_boolean(key)
1354    }
1355
1356    /// The `STRING` configuration `key`, trimmed. Reading a key marks it used
1357    /// ([`Self::unused`]).
1358    ///
1359    /// `client.id` answers the resolved id: when the property is unset, `new`
1360    /// derives it the way Java's `maybeOverrideClientId` does, so this is the
1361    /// id the consumer reports to the brokers. `security.protocol` answers the
1362    /// value as written, trimmed (`"sasl_ssl"`), as Java's does: its validator
1363    /// accepts it case-insensitively without changing it. A `PASSWORD`
1364    /// configuration that is not set answers `None`, as Java's `null` default
1365    /// casts to `String`.
1366    ///
1367    /// # Arguments
1368    ///
1369    /// - `key` - the configuration name
1370    ///
1371    /// # Returns
1372    ///
1373    /// The value, `None` when it has no value (Java's `null`, e.g. an unset
1374    /// `group.id`).
1375    ///
1376    /// # Errors
1377    ///
1378    /// - `Error::is_config_error` with Java's `Unknown configuration '<key>'`
1379    ///   if `key` is not a configuration this class defines.
1380    /// - `Error::is_local_illegal_argument_error` if `key` is a configuration
1381    ///   of another type, where Java's cast fails with a `ClassCastException`.
1382    #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getString")]
1383    pub fn get_string(&self, key: &str) -> Result<Option<&str>, Error> {
1384        self.config.get_string(key)
1385    }
1386
1387    /// Returns the keys of the properties this configuration was built from
1388    /// that were never read: through a getter, or by the consumer built from
1389    /// it.
1390    #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#unused")]
1391    pub fn unused(&self) -> HashSet<String> {
1392        self.config.unused()
1393    }
1394
1395    /// Marks the configuration `key` used without reading it.
1396    ///
1397    /// # Arguments
1398    ///
1399    /// - `key` - the configuration name
1400    #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#ignore")]
1401    pub fn ignore(&self, key: &str) {
1402        self.config.ignore(key);
1403    }
1404
1405    /// Info level log for any unused configurations
1406    // One line, `These configurations '[a, b]' were supplied but are not used
1407    // yet.`, and nothing when every key was used.
1408    #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#logUnused")]
1409    pub fn log_unused(&self) {
1410        self.config.log_unused();
1411    }
1412
1413    /// The names of the configurations this class defines.
1414    ///
1415    /// # Returns
1416    ///
1417    /// The set of configuration keys
1418    #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfig#configNames")]
1419    pub fn config_names() -> HashSet<String> {
1420        CONFIG.names()
1421    }
1422
1423    /// Marks used the keys the typed settings were read from, as the consumer
1424    /// does where Java's reads them through the getters.
1425    pub(crate) fn mark_read_keys_used(&self) {
1426        for key in &self.read_keys {
1427            self.config.ignore(key);
1428        }
1429    }
1430
1431    /// Java's `logAll()`, which Java runs when the consumer builds the
1432    /// configuration with `doLog = true`: every parsed value at INFO.
1433    pub(crate) fn log_values(&self) {
1434        self.config.log_all("ConsumerConfig");
1435    }
1436
1437    /// `bootstrap.servers`.
1438    pub(crate) fn bootstrap_servers(&self) -> &[String] {
1439        &self.bootstrap_servers
1440    }
1441    /// `client.dns.lookup`.
1442    pub(crate) fn client_dns_lookup(&self) -> ClientDnsLookup {
1443        self.client_dns_lookup
1444    }
1445    /// `client.id`.
1446    pub(crate) fn client_id(&self) -> &str {
1447        &self.client_id
1448    }
1449    /// `client.rack`, empty when unset ([`Self::DEFAULT_CLIENT_RACK`]).
1450    pub(crate) fn client_rack(&self) -> &str {
1451        &self.client_rack
1452    }
1453    /// `group.id`, if any.
1454    pub(crate) fn group_id(&self) -> Option<&str> {
1455        self.group_id.as_deref()
1456    }
1457    /// `group.instance.id`, if any.
1458    pub(crate) fn group_instance_id(&self) -> Option<&str> {
1459        self.group_instance_id.as_deref()
1460    }
1461    /// `group.protocol`.
1462    pub(crate) fn group_protocol(&self) -> &str {
1463        &self.group_protocol
1464    }
1465    /// `group.remote.assignor`, if any.
1466    pub(crate) fn group_remote_assignor(&self) -> Option<&str> {
1467        self.group_remote_assignor.as_deref()
1468    }
1469    /// `max.poll.interval.ms`.
1470    pub(crate) fn max_poll_interval_ms(&self) -> i32 {
1471        self.max_poll_interval_ms
1472    }
1473    /// `enable.auto.commit`.
1474    pub(crate) fn enable_auto_commit(&self) -> bool {
1475        self.enable_auto_commit
1476    }
1477    /// `auto.commit.interval.ms`.
1478    pub(crate) fn auto_commit_interval_ms(&self) -> i32 {
1479        self.auto_commit_interval_ms
1480    }
1481    /// `auto.offset.reset`.
1482    pub(crate) fn auto_offset_reset(&self) -> &str {
1483        &self.auto_offset_reset
1484    }
1485    /// `metadata.recovery.strategy`.
1486    pub(crate) fn metadata_recovery_strategy(&self) -> &str {
1487        &self.metadata_recovery_strategy
1488    }
1489    /// `internal.throw.on.fetch.stable.offset.unsupported`.
1490    pub(crate) fn throw_on_fetch_stable_offset_unsupported(&self) -> bool {
1491        self.throw_on_fetch_stable_offset_unsupported
1492    }
1493    /// `request.timeout.ms`.
1494    pub(crate) fn request_timeout_ms(&self) -> i32 {
1495        self.request_timeout_ms
1496    }
1497    /// `retry.backoff.ms`.
1498    pub(crate) fn retry_backoff_ms(&self) -> i64 {
1499        self.retry_backoff_ms
1500    }
1501    /// `retry.backoff.max.ms`.
1502    pub(crate) fn retry_backoff_max_ms(&self) -> i64 {
1503        self.retry_backoff_max_ms
1504    }
1505    /// `bootstrap.resolve.timeout.ms` (KIP-909); see
1506    /// [`Self::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG`].
1507    pub(crate) fn bootstrap_resolve_timeout_ms(&self) -> i64 {
1508        self.bootstrap_resolve_timeout_ms
1509    }
1510
1511    /// Derives `client.id` on a configuration whose `client.id` is empty.
1512    ///
1513    /// `new` already derives it (`maybeOverrideClientId`, run by
1514    /// `postProcessParsedConfig`), so this only acts on a configuration built
1515    /// by the crate's tests from [`Self::defaults`], with the same derivation.
1516    #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfig#maybeOverrideClientId")]
1517    pub(crate) fn maybe_override_client_id(&mut self) -> Result<(), Error> {
1518        if !self.client_id.is_empty() {
1519            return Ok(());
1520        }
1521        if let Some(group_instance_id) = &self.group_instance_id {
1522            JoinGroupRequest::validate_group_instance_id(group_instance_id)?;
1523        }
1524        let group_id = self.group_id.as_deref().unwrap_or("null");
1525        let group_instance_id_part = match &self.group_instance_id {
1526            Some(group_instance_id) => group_instance_id.clone(),
1527            None => CONSUMER_CLIENT_ID_SEQUENCE.fetch_add(1, atomic::Ordering::Relaxed).to_string(),
1528        };
1529        self.client_id = format!("consumer-{group_id}-{group_instance_id_part}");
1530        Ok(())
1531    }
1532}
1533
1534#[cfg(test)]
1535mod tests {
1536    /// `postValidateSaslMechanismConfig` runs when the config is built, so an unusable
1537    /// `sasl.mechanism` fails here and not later, when a channel builder is created.
1538    #[test]
1539    fn test_sasl_mechanism_is_validated_when_the_config_is_built() {
1540        let build = |protocol: &str, mechanism: &str| {
1541            let props = std::collections::HashMap::from([
1542                ("bootstrap.servers".to_string(), "localhost:9092".to_string()),
1543                ("security.protocol".to_string(), protocol.to_string()),
1544                ("sasl.mechanism".to_string(), mechanism.to_string()),
1545            ]);
1546            ConsumerConfig::new(&props)
1547        };
1548
1549        let err = build("SASL_PLAINTEXT", "SCRAM-SHA-256").expect_err("SCRAM is not implemented");
1550        assert!(err.is_config_error(), "must be a ConfigError: {err:?}");
1551        assert_eq!(
1552            err.message(),
1553            "Invalid value SCRAM-SHA-256 for configuration sasl.mechanism: only PLAIN is supported by this client"
1554        );
1555
1556        let err = build("SASL_SSL", "").expect_err("an empty mechanism is invalid with SASL");
1557        assert!(err.is_config_error(), "must be a ConfigError: {err:?}");
1558        assert!(
1559            err.message().contains("mechanism must be non-null and non-empty string"),
1560            "got: {}",
1561            err.message()
1562        );
1563
1564        build("SASL_PLAINTEXT", "PLAIN").expect("PLAIN is implemented");
1565        build("PLAINTEXT", "SCRAM-SHA-256").expect("the mechanism only matters with SASL");
1566    }
1567
1568    use super::*;
1569    use crate::common::config::types::Password;
1570    use crate::test_log_capture::capture_logs;
1571    use log::Level;
1572
1573    /// Props holding only `bootstrap.servers`, which the `ConfigDef` defines
1574    /// with `NO_DEFAULT_VALUE` and so every valid config must carry.
1575    fn base_props() -> HashMap<String, String> {
1576        HashMap::from([("bootstrap.servers".to_string(), "localhost:9092".to_string())])
1577    }
1578
1579    #[test]
1580    fn test_default_values() {
1581        let c = ConsumerConfig::defaults();
1582        assert_eq!(c.max_poll_records, 500);
1583        assert_eq!(c.max_poll_interval_ms(), 300_000);
1584        assert_eq!(c.get_int(ConsumerConfig::SESSION_TIMEOUT_MS_CONFIG).unwrap(), Some(45_000));
1585        assert_eq!(c.get_int(ConsumerConfig::HEARTBEAT_INTERVAL_MS_CONFIG).unwrap(), Some(3_000));
1586        assert!(c.enable_auto_commit());
1587        assert_eq!(c.auto_commit_interval_ms(), 5_000);
1588        assert_eq!(c.auto_offset_reset(), "latest");
1589        assert_eq!(c.group_protocol(), "classic");
1590        assert_eq!(c.group_id(), None);
1591        assert_eq!(c.security_protocol, SecurityProtocol::Plaintext);
1592        assert_eq!(c.metadata_recovery_strategy(), "rebootstrap");
1593    }
1594
1595    #[test]
1596    fn test_new_basic() {
1597        let mut props = base_props();
1598        props.insert("bootstrap.servers".to_string(), "host1:9092,host2:9093".to_string());
1599        props.insert("group.id".to_string(), "g".to_string());
1600        props.insert("client.id".to_string(), "my-consumer".to_string());
1601        props.insert("max.poll.records".to_string(), "100".to_string());
1602        let c = ConsumerConfig::new(&props).unwrap();
1603        assert_eq!(c.bootstrap_servers(), &["host1:9092".to_string(), "host2:9093".to_string()]);
1604        assert_eq!(c.group_id(), Some("g"));
1605        assert_eq!(c.client_id(), "my-consumer");
1606        assert_eq!(c.max_poll_records, 100);
1607    }
1608
1609    #[test]
1610    fn test_new_unknown_key_ignored() {
1611        let mut props = base_props();
1612        props.insert("unknown.key".to_string(), "value".to_string());
1613        props.insert("bootstrap.servers".to_string(), "localhost:9092".to_string());
1614        let c = ConsumerConfig::new(&props).unwrap();
1615        assert_eq!(c.bootstrap_servers(), &["localhost:9092".to_string()]);
1616    }
1617
1618    /// Boolean config values are parsed case-insensitively after trimming, and an
1619    /// unparseable value is rejected with Java's message and the original,
1620    /// untrimmed value. Mirrors `ConfigDef.parseType(BOOLEAN)`
1621    /// (`org.apache.kafka.common.config.ConfigDef#parseType`): `equalsIgnoreCase("true")` / `("false")`, else
1622    /// `ConfigException(name, value, "Expected value to be either true or false")`.
1623    #[test]
1624    fn test_parse_bool_matches_java_boolean() {
1625        let props_with = |raw: &str| {
1626            // A `group.id`, without which `enable.auto.commit=true` is rejected.
1627            HashMap::from([
1628                ("bootstrap.servers".to_string(), "localhost:9092".to_string()),
1629                ("group.id".to_string(), "g".to_string()),
1630                ("enable.auto.commit".to_string(), raw.to_string()),
1631            ])
1632        };
1633
1634        // Accepted regardless of case, and after trimming surrounding whitespace.
1635        for raw in ["true", "True", "TRUE"] {
1636            let c = ConsumerConfig::new(&props_with(raw))
1637                .unwrap_or_else(|e| panic!("enable.auto.commit={raw} should be valid: {e:?}"));
1638            assert!(c.enable_auto_commit(), "for enable.auto.commit={raw}");
1639        }
1640        for raw in ["false", "False", " false "] {
1641            let c = ConsumerConfig::new(&props_with(raw))
1642                .unwrap_or_else(|e| panic!("enable.auto.commit={raw} should be valid: {e:?}"));
1643            assert!(!c.enable_auto_commit(), "for enable.auto.commit={raw}");
1644        }
1645
1646        // Rejected: a non-boolean string. Java prints the original (untrimmed) value.
1647        let error =
1648            ConsumerConfig::new(&props_with("yes")).expect_err("a non-boolean enable.auto.commit must be rejected");
1649        assert_eq!(
1650            error.message(),
1651            "Invalid value yes for configuration enable.auto.commit: Expected value to be either true or false",
1652        );
1653        assert!(matches!(error, Error::Config(_)), "expected Error::Config, got {error:?}");
1654        assert!(error.is_kafka_error());
1655    }
1656
1657    /// `metrics.num.samples` is `atLeast(1)` (Java ConsumerConfig). A value
1658    /// below 1 is rejected with a message asserting the bound.
1659    #[test]
1660    fn test_metrics_num_samples_validator() {
1661        // Valid: >= 1.
1662        let mut props = base_props();
1663        props.insert("metrics.num.samples".to_string(), "3".to_string());
1664        let c = ConsumerConfig::new(&props).unwrap();
1665        assert_eq!(c.metrics_num_samples, 3);
1666
1667        // Invalid: 0 (< 1).
1668        let mut props = base_props();
1669        props.insert("metrics.num.samples".to_string(), "0".to_string());
1670        let err = ConsumerConfig::new(&props).unwrap_err();
1671        let msg = err.to_string();
1672        assert!(
1673            msg.contains("metrics.num.samples") && msg.contains("at least 1"),
1674            "unexpected message: {msg}"
1675        );
1676
1677        // Invalid: negative.
1678        let mut props = base_props();
1679        props.insert("metrics.num.samples".to_string(), "-1".to_string());
1680        assert!(ConsumerConfig::new(&props).is_err());
1681    }
1682
1683    /// `metadata.cluster.check.enable` (KIP-1242): `Type.BOOLEAN`, default `true`.
1684    #[test]
1685    fn test_metadata_cluster_check_enable() {
1686        assert!(ConsumerConfig::new(&base_props()).unwrap().metadata_cluster_check_enable);
1687
1688        let mut props = base_props();
1689        props.insert(
1690            CommonClientConfigs::METADATA_CLUSTER_CHECK_ENABLE_CONFIG.to_string(),
1691            "false".to_string(),
1692        );
1693        assert!(!ConsumerConfig::new(&props).unwrap().metadata_cluster_check_enable);
1694
1695        props.insert(
1696            CommonClientConfigs::METADATA_CLUSTER_CHECK_ENABLE_CONFIG.to_string(),
1697            "maybe".to_string(),
1698        );
1699        assert_eq!(
1700            ConsumerConfig::new(&props).unwrap_err().message(),
1701            "Invalid value maybe for configuration metadata.cluster.check.enable: Expected value to be either true or false"
1702        );
1703    }
1704
1705    /// `metadata.recovery.rebootstrap.trigger.ms` is `atLeast(0)` (Java's
1706    /// `ConsumerConfig.CONFIG`): 0 is accepted, -1 is rejected with
1707    /// Java's `ConfigDef.Range.atLeast` message.
1708    #[test]
1709    fn test_metadata_recovery_rebootstrap_trigger_ms_validator() {
1710        let mut props = base_props();
1711        props.insert("metadata.recovery.rebootstrap.trigger.ms".to_string(), "0".to_string());
1712        let c = ConsumerConfig::new(&props).unwrap();
1713        assert_eq!(c.metadata_recovery_rebootstrap_trigger_ms, 0);
1714
1715        let mut props = base_props();
1716        props.insert("metadata.recovery.rebootstrap.trigger.ms".to_string(), "-1".to_string());
1717        let err = ConsumerConfig::new(&props).unwrap_err();
1718        let Error::Config(config_error) = err else {
1719            panic!("expected a config error, got {err:?}");
1720        };
1721        assert_eq!(
1722            config_error.message(),
1723            "Invalid value -1 for configuration metadata.recovery.rebootstrap.trigger.ms: \
1724             Value must be at least 0"
1725        );
1726    }
1727
1728    /// `request.timeout.ms` and `default.api.timeout.ms` are `atLeast(0)`
1729    /// (Java `org.apache.kafka.clients.consumer.ConsumerConfig#CONFIG`): 0 is
1730    /// accepted and a negative value is rejected when the config is built. A negative value would
1731    /// otherwise reach `CompletableEvent::calculate_deadline_ms`, which maps a
1732    /// negative timeout to "no deadline", so every default-timeout API and
1733    /// `close()` could wait forever.
1734    #[test]
1735    fn test_request_and_default_api_timeout_ms_validators() {
1736        for key in [
1737            ConsumerConfig::REQUEST_TIMEOUT_MS_CONFIG,
1738            ConsumerConfig::DEFAULT_API_TIMEOUT_MS_CONFIG,
1739        ] {
1740            let mut props = base_props();
1741            props.insert(key.to_string(), "0".to_string());
1742            let c = ConsumerConfig::new(&props).unwrap();
1743            if key == ConsumerConfig::REQUEST_TIMEOUT_MS_CONFIG {
1744                assert_eq!(c.request_timeout_ms, 0);
1745            } else {
1746                assert_eq!(c.default_api_timeout_ms, 0);
1747            }
1748
1749            let mut props = base_props();
1750            props.insert(key.to_string(), "-1".to_string());
1751            let err = ConsumerConfig::new(&props).unwrap_err();
1752            let Error::Config(config_error) = err else {
1753                panic!("expected a config error for {key}, got {err:?}");
1754            };
1755            assert_eq!(
1756                config_error.message(),
1757                format!("Invalid value -1 for configuration {key}: Value must be at least 0")
1758            );
1759        }
1760    }
1761
1762    /// `bootstrap.resolve.timeout.ms` (KIP-909): `Type.LONG`, default `0`
1763    /// (synchronous resolution), `atLeast(0L)` since KAFKA-20939.
1764    #[test]
1765    fn test_bootstrap_resolve_timeout_ms() {
1766        assert_eq!(
1767            ConsumerConfig::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG,
1768            "bootstrap.resolve.timeout.ms"
1769        );
1770        let mut props = HashMap::from([("bootstrap.servers".to_string(), "localhost:9092".to_string())]);
1771        assert_eq!(ConsumerConfig::new(&props).unwrap().bootstrap_resolve_timeout_ms(), 0);
1772
1773        props.insert("bootstrap.resolve.timeout.ms".to_string(), "3000".to_string());
1774        assert_eq!(ConsumerConfig::new(&props).unwrap().bootstrap_resolve_timeout_ms(), 3000);
1775
1776        props.insert("bootstrap.resolve.timeout.ms".to_string(), "-1".to_string());
1777        match ConsumerConfig::new(&props) {
1778            Err(Error::Config(e)) => assert_eq!(
1779                e.message(),
1780                "Invalid value -1 for configuration bootstrap.resolve.timeout.ms: Value must be at least 0"
1781            ),
1782            other => panic!("expected a config error, got {other:?}"),
1783        }
1784    }
1785
1786    /// `metrics.sample.window.ms` is `atLeast(0)` (Java ConsumerConfig). A
1787    /// negative value is rejected with a message asserting the bound.
1788    #[test]
1789    fn test_metrics_sample_window_ms_validator() {
1790        // Valid: >= 0 (0 is allowed).
1791        let mut props = base_props();
1792        props.insert("metrics.sample.window.ms".to_string(), "0".to_string());
1793        let c = ConsumerConfig::new(&props).unwrap();
1794        assert_eq!(c.metrics_sample_window_ms, 0);
1795
1796        let mut props = base_props();
1797        props.insert("metrics.sample.window.ms".to_string(), "60000".to_string());
1798        let c = ConsumerConfig::new(&props).unwrap();
1799        assert_eq!(c.metrics_sample_window_ms, 60_000);
1800
1801        // Invalid: negative.
1802        let mut props = base_props();
1803        props.insert("metrics.sample.window.ms".to_string(), "-1".to_string());
1804        let err = ConsumerConfig::new(&props).unwrap_err();
1805        let msg = err.to_string();
1806        assert!(
1807            msg.contains("metrics.sample.window.ms") && msg.contains("at least 0"),
1808            "unexpected message: {msg}"
1809        );
1810    }
1811
1812    /// `metrics.recording.level` accepts exactly `INFO`/`DEBUG`/`TRACE`
1813    /// (case-sensitive, mirroring Java `ConfigDef.ValidString.in`). Any other
1814    /// value — including lower/mixed case — is rejected with the Java
1815    /// `ConfigException` wording.
1816    #[test]
1817    fn test_metrics_recording_level_validator() {
1818        for level in ["INFO", "DEBUG", "TRACE"] {
1819            let mut props = base_props();
1820            props.insert("metrics.recording.level".to_string(), level.to_string());
1821            let c = ConsumerConfig::new(&props).unwrap();
1822            assert_eq!(c.metrics_recording_level, level);
1823        }
1824
1825        // Lowercase is rejected (case-sensitive membership check).
1826        let mut props = base_props();
1827        props.insert("metrics.recording.level".to_string(), "debug".to_string());
1828        let err = ConsumerConfig::new(&props).unwrap_err();
1829        assert!(
1830            err.to_string().ends_with(
1831                "Invalid value debug for configuration metrics.recording.level: \
1832                 String must be one of: INFO, DEBUG, TRACE"
1833            ),
1834            "unexpected message: {err}"
1835        );
1836
1837        // A wholly unknown value is rejected too.
1838        let mut props = base_props();
1839        props.insert("metrics.recording.level".to_string(), "bogus".to_string());
1840        assert!(ConsumerConfig::new(&props).is_err());
1841    }
1842
1843    /// Each of the four `security.protocol` values parses to the right enum.
1844    #[test]
1845    fn test_security_protocol_all_values() {
1846        for (input, expected) in [
1847            ("PLAINTEXT", SecurityProtocol::Plaintext),
1848            ("SSL", SecurityProtocol::Ssl),
1849            ("SASL_PLAINTEXT", SecurityProtocol::SaslPlaintext),
1850            ("SASL_SSL", SecurityProtocol::SaslSsl),
1851        ] {
1852            let mut props = base_props();
1853            props.insert("security.protocol".to_string(), input.to_string());
1854            props.insert("sasl.mechanism".to_string(), "PLAIN".to_string());
1855            let c = ConsumerConfig::new(&props).unwrap();
1856            assert_eq!(c.security_protocol, expected, "for input {input}");
1857        }
1858    }
1859
1860    /// `security.protocol` parsing is case-insensitive (mirrors Java/`for_name`).
1861    #[test]
1862    #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testCaseInsensitiveSecurityProtocol")]
1863    fn test_case_insensitive_security_protocol() {
1864        let mut props = base_props();
1865        props.insert("security.protocol".to_string(), "sasl_ssl".to_string());
1866        // Only `PLAIN` is accepted under a SASL protocol (`post_validate_supported_sasl_mechanism`).
1867        props.insert("sasl.mechanism".to_string(), "PLAIN".to_string());
1868        let c = ConsumerConfig::new(&props).unwrap();
1869        assert_eq!(c.security_protocol, SecurityProtocol::SaslSsl);
1870    }
1871
1872    /// Invalid `security.protocol` → `illegal_argument` with asserted message
1873    /// content (DoD §3): the config key, the bad value, and the valid names.
1874    #[test]
1875    #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testInvalidSecurityProtocol")]
1876    fn test_invalid_security_protocol() {
1877        let mut props = base_props();
1878        props.insert("security.protocol".to_string(), "abc".to_string());
1879        let err = ConsumerConfig::new(&props).unwrap_err();
1880        let msg = format!("{}", err);
1881        assert!(msg.contains("security.protocol"), "should contain config key, got: {msg}");
1882        assert!(msg.contains("abc"), "should contain the invalid value, got: {msg}");
1883        assert!(msg.contains("SASL_SSL"), "should list the valid protocol names, got: {msg}");
1884    }
1885
1886    /// `bootstrap.servers` has `NO_DEFAULT_VALUE`, so a config
1887    /// without it fails at parse time with `ConfigDef.parseValue`'s message.
1888    #[test]
1889    fn test_missing_bootstrap_servers_rejected_with_exact_message() {
1890        let err = ConsumerConfig::new(&HashMap::new()).unwrap_err();
1891        let Error::Config(config_err) = &err else {
1892            panic!("expected a config error, got: {err:?}");
1893        };
1894        assert_eq!(
1895            config_err.message(),
1896            "Missing required configuration \"bootstrap.servers\" which has no default value."
1897        );
1898    }
1899
1900    /// `client.dns.lookup` defaults to `use_all_dns_ips` and parses into the
1901    /// typed [`ClientDnsLookup`], as `ConsumerConfig`'s `ConfigDef` defines it.
1902    #[test]
1903    fn test_client_dns_lookup() {
1904        assert_eq!(ConsumerConfig::CLIENT_DNS_LOOKUP_CONFIG, "client.dns.lookup");
1905        assert_eq!(
1906            ConsumerConfig::new(&base_props()).unwrap().client_dns_lookup(),
1907            ClientDnsLookup::UseAllDnsIps
1908        );
1909
1910        let mut props = base_props();
1911        props.insert(
1912            CommonClientConfigs::CLIENT_DNS_LOOKUP_CONFIG.to_string(),
1913            "resolve_canonical_bootstrap_servers_only".to_string(),
1914        );
1915        assert_eq!(
1916            ConsumerConfig::new(&props).unwrap().client_dns_lookup(),
1917            ClientDnsLookup::ResolveCanonicalBootstrapServersOnly
1918        );
1919
1920        props.insert(CommonClientConfigs::CLIENT_DNS_LOOKUP_CONFIG.to_string(), "default".to_string());
1921        assert_eq!(
1922            ConsumerConfig::new(&props).unwrap_err().message(),
1923            "Invalid value default for configuration client.dns.lookup: String must be one of: \
1924             use_all_dns_ips, resolve_canonical_bootstrap_servers_only"
1925        );
1926    }
1927
1928    /// `sasl.mechanism` and `sasl.jaas.config` land on `sasl_config`.
1929    #[test]
1930    fn test_sasl_config_from_properties() {
1931        let mut props = base_props();
1932        props.insert("sasl.mechanism".to_string(), "PLAIN".to_string());
1933        props.insert(
1934            "sasl.jaas.config".to_string(),
1935            "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"alice\" password=\"secret\";"
1936                .to_string(),
1937        );
1938        let c = ConsumerConfig::new(&props).unwrap();
1939        assert_eq!(c.sasl_config.mechanism, "PLAIN");
1940        assert_eq!(c.sasl_config.resolve_username().as_deref(), Some("alice"));
1941        assert_eq!(c.sasl_config.resolve_password().as_deref(), Some("secret"));
1942    }
1943
1944    /// Rust-only (`CommonClientConfigs::post_validate_supported_sasl_mechanism`):
1945    /// a SASL `security.protocol` needs `sasl.mechanism=PLAIN`; any other
1946    /// mechanism, the `GSSAPI` default included, fails the build with
1947    /// `LocalUnsupportedOperationError`, while an empty one keeps Java's
1948    /// `ConfigError`. Under `PLAINTEXT` the mechanism is inert, as in Java.
1949    #[test]
1950    fn test_unsupported_sasl_mechanism_rejected() {
1951        let jaas = "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"u\" password=\"p\";";
1952        let build = |extra: &[(&str, &str)]| {
1953            let mut props = base_props();
1954            for (k, v) in extra {
1955                props.insert((*k).to_string(), (*v).to_string());
1956            }
1957            ConsumerConfig::new(&props)
1958        };
1959
1960        let err = build(&[
1961            ("security.protocol", "SASL_PLAINTEXT"),
1962            ("sasl.mechanism", "SCRAM-SHA-256"),
1963            ("sasl.jaas.config", jaas),
1964        ])
1965        .unwrap_err();
1966        assert!(err.is_config_error(), "{err:?}");
1967        assert_eq!(
1968            err.to_string(),
1969            "ConfigError: Invalid value SCRAM-SHA-256 for configuration sasl.mechanism: only PLAIN is supported by \
1970             this client"
1971        );
1972
1973        // The default mechanism is Java's `GSSAPI`.
1974        let err = build(&[("security.protocol", "SASL_SSL")]).unwrap_err();
1975        assert!(err.is_config_error(), "{err:?}");
1976        assert_eq!(
1977            err.to_string(),
1978            "ConfigError: Invalid value GSSAPI for configuration sasl.mechanism: only PLAIN is supported by this client"
1979        );
1980
1981        // Case-sensitive, as Java's mechanism names are.
1982        let err = build(&[("security.protocol", "SASL_PLAINTEXT"), ("sasl.mechanism", "plain")]).unwrap_err();
1983        assert!(err.is_config_error(), "{err:?}");
1984
1985        build(&[("security.protocol", "PLAINTEXT")]).unwrap();
1986        build(&[
1987            ("security.protocol", "SASL_PLAINTEXT"),
1988            ("sasl.mechanism", "PLAIN"),
1989            ("sasl.jaas.config", jaas),
1990        ])
1991        .unwrap();
1992
1993        // An empty mechanism fails Java's check first.
1994        let err = build(&[("security.protocol", "SASL_PLAINTEXT"), ("sasl.mechanism", "")]).unwrap_err();
1995        assert!(err.is_config_error(), "{err:?}");
1996        assert_eq!(
1997            err.to_string(),
1998            "ConfigError: Invalid value null for configuration sasl.mechanism: When the security.protocol \
1999             configuration enables SASL, mechanism must be non-null and non-empty string."
2000        );
2001    }
2002
2003    /// `{:?}` of a config built from every `Type.PASSWORD` key the client
2004    /// parses (`sasl.jaas.config` and the three SSL keys) renders none of their
2005    /// values. The derived `Debug` is safe because the embedded `SaslConfigs`
2006    /// and `SslConfigs` hold those values as `Password`s. The three SSL
2007    /// password keys this client does not define render as key names only.
2008    #[test]
2009    fn test_debug_redacts_password_configs() {
2010        let secrets = [
2011            (
2012                "sasl.jaas.config",
2013                "org.apache.kafka.common.security.plain.PlainLoginModule required \
2014                 username=\"jaas-user\" password=\"jaas-S3cr3t\";",
2015            ),
2016            ("ssl.truststore.certificates", "TRUSTSTORE-CERTIFICATES-PEM"),
2017            ("ssl.keystore.key", "KEYSTORE-KEY-PEM"),
2018            ("ssl.keystore.certificate.chain", "KEYSTORE-CERTIFICATE-CHAIN-PEM"),
2019        ];
2020        // Not defined (`SslConfigs` module docs), so held only in the originals.
2021        let undefined = [
2022            ("ssl.truststore.password", "truststore-S3cr3t"),
2023            ("ssl.keystore.password", "keystore-S3cr3t"),
2024            ("ssl.key.password", "key-S3cr3t"),
2025        ];
2026        let mut props: HashMap<String, String> = secrets
2027            .iter()
2028            .chain(&undefined)
2029            .map(|(key, value)| (key.to_string(), value.to_string()))
2030            .collect();
2031        props.insert("bootstrap.servers".to_string(), "visible-host:9092".to_string());
2032        props.insert("group.id".to_string(), "visible-group".to_string());
2033        props.insert("sasl.mechanism".to_string(), "PLAIN".to_string());
2034        props.insert("ssl.truststore.location".to_string(), "/visible/truststore.pem".to_string());
2035        let c = ConsumerConfig::new(&props).unwrap();
2036
2037        // The secrets did reach the config; they are only hidden from `Debug`.
2038        assert_eq!(c.sasl_config.resolve_password().as_deref(), Some("jaas-S3cr3t"));
2039        assert_eq!(
2040            c.ssl_config.keystore_key.as_ref().map(Password::value),
2041            Some("KEYSTORE-KEY-PEM")
2042        );
2043
2044        for rendered in [format!("{c:?}"), format!("{c:#?}")] {
2045            for (key, value) in secrets.iter().chain(&undefined) {
2046                assert!(!rendered.contains(value), "{key} leaked: {rendered}");
2047            }
2048            for fragment in ["jaas-S3cr3t", "jaas-user", "PlainLoginModule", "S3cr3t", "-PEM"] {
2049                assert!(!rendered.contains(fragment), "{fragment:?} leaked: {rendered}");
2050            }
2051            assert_eq!(rendered.matches(Password::HIDDEN).count(), secrets.len(), "{rendered}");
2052            for visible in [
2053                "visible-host:9092",
2054                "visible-group",
2055                "\"PLAIN\"",
2056                "/visible/truststore.pem",
2057            ] {
2058                assert!(rendered.contains(visible), "{visible} missing: {rendered}");
2059            }
2060        }
2061    }
2062
2063    /// Empty `sasl.jaas.config` → `None`.
2064    #[test]
2065    fn test_sasl_jaas_config_empty_is_none() {
2066        let mut props = base_props();
2067        props.insert("sasl.jaas.config".to_string(), String::new());
2068        let c = ConsumerConfig::new(&props).unwrap();
2069        assert_eq!(c.sasl_config.jaas_config, None);
2070    }
2071
2072    /// `ssl.*` keys land on `ssl_config` via the shared helper.
2073    #[test]
2074    fn test_ssl_config_from_properties() {
2075        let mut props = base_props();
2076        props.insert("ssl.truststore.location".to_string(), "/path/to/truststore.pem".to_string());
2077        props.insert("ssl.keystore.location".to_string(), "/path/to/keystore.pem".to_string());
2078        props.insert("ssl.endpoint.identification.algorithm".to_string(), String::new());
2079        let c = ConsumerConfig::new(&props).unwrap();
2080        assert_eq!(c.ssl_config.truststore_location.as_deref(), Some("/path/to/truststore.pem"));
2081        assert_eq!(c.ssl_config.keystore_location.as_deref(), Some("/path/to/keystore.pem"));
2082        assert_eq!(c.ssl_config.endpoint_identification_algorithm, "");
2083    }
2084
2085    /// Builder selection: a protocol with no certificate requirement
2086    /// (`SASL_PLAINTEXT` + PLAIN) builds `Ok`; `SASL_SSL` with no truststore
2087    /// content → error (mirrors `channel_builders` / `SslFactory` validation).
2088    ///
2089    /// The `SASL_SSL`-with-*valid*-truststore happy path requires a real
2090    /// parseable CA certificate, which is exercised in the Docker integration
2091    /// test `tests/integration/sasl_ssl_consumer_test.rs` (it has access to the
2092    /// `rcgen`/cluster-generated CA); here we cover the protocol-selection wiring
2093    /// and the missing-ssl error branch without filesystem/cert dependencies.
2094    #[test]
2095    fn test_builder_selection() {
2096        use crate::common::network::ChannelBuilders;
2097        use crate::common::utils::internals::LogContext;
2098
2099        // SASL_PLAINTEXT with PLAIN credentials → Ok (no cert needed).
2100        let mut props = base_props();
2101        props.insert("security.protocol".to_string(), "SASL_PLAINTEXT".to_string());
2102        props.insert("sasl.mechanism".to_string(), "PLAIN".to_string());
2103        props.insert(
2104            "sasl.jaas.config".to_string(),
2105            "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"admin\" password=\"admin-secret\";"
2106                .to_string(),
2107        );
2108        let c = ConsumerConfig::new(&props).unwrap();
2109        assert_eq!(c.security_protocol, SecurityProtocol::SaslPlaintext);
2110        let builder = ChannelBuilders::client_channel_builder(
2111            c.security_protocol,
2112            Some(&c.ssl_config),
2113            Some(&c.sasl_config),
2114            None,
2115            c.client_id(),
2116            LogContext::new("[test] ".to_string()),
2117        );
2118        assert!(builder.is_ok(), "SASL_PLAINTEXT with valid config should build");
2119
2120        // SASL_SSL with no ssl_config supplied at all → error: the
2121        // `channel_builders` SASL_SSL arm requires ssl_config to be present.
2122        let mut props = base_props();
2123        props.insert("security.protocol".to_string(), "SASL_SSL".to_string());
2124        props.insert("sasl.mechanism".to_string(), "PLAIN".to_string());
2125        props.insert(
2126            "sasl.jaas.config".to_string(),
2127            "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"admin\" password=\"admin-secret\";"
2128                .to_string(),
2129        );
2130        let c = ConsumerConfig::new(&props).unwrap();
2131        let builder = ChannelBuilders::client_channel_builder(
2132            c.security_protocol,
2133            None, // missing ssl_config content
2134            Some(&c.sasl_config),
2135            None,
2136            c.client_id(),
2137            LogContext::new("[test] ".to_string()),
2138        );
2139        let err = match builder {
2140            Ok(_) => panic!("SASL_SSL with no ssl_config should fail to build"),
2141            Err(e) => e,
2142        };
2143        let msg = format!("{}", err);
2144        // Without TLS in the build, SASL_SSL fails before its configs are read.
2145        let expected = if cfg!(feature = "rustls") {
2146            "ssl_config"
2147        } else {
2148            "the client was built without the `rustls` feature"
2149        };
2150        assert!(msg.contains(expected), "error should mention {expected}, got: {msg}");
2151    }
2152
2153    /// Java's `ConfigDef` does not coerce an empty `group.id` to null: it stays
2154    /// `""` and `AsyncKafkaConsumer.initializeGroupMetadata` rejects it
2155    /// (`org.apache.kafka.clients.consumer.internals.AsyncKafkaConsumer#initializeGroupMetadata`).
2156    /// Coercing it to `None` here would
2157    /// turn a hard configuration error into a groupless consumer, which then
2158    /// fails much later and much less legibly.
2159    #[test]
2160    fn test_new_keeps_an_empty_group_id() {
2161        let mut props = base_props();
2162        props.insert("bootstrap.servers".to_string(), "host1:9092".to_string());
2163        props.insert("group.id".to_string(), String::new());
2164        let c = ConsumerConfig::new(&props).unwrap();
2165        assert_eq!(c.group_id(), Some(""), "the empty string must survive to the constructor");
2166    }
2167
2168    /// An absent `group.id` is still `None` — the two cases stay distinct.
2169    #[test]
2170    fn test_new_absent_group_id_is_none() {
2171        let mut props = base_props();
2172        props.insert("bootstrap.servers".to_string(), "host1:9092".to_string());
2173        let c = ConsumerConfig::new(&props).unwrap();
2174        assert_eq!(c.group_id(), None);
2175    }
2176
2177    /// `bootstrap.servers` is a `Type.LIST`: `ConfigDef.parseType` trims the
2178    /// value and splits it on `\\s*,\\s*`, so whitespace around the commas
2179    /// and at the ends never reaches `ClientUtils.parseAndValidateAddresses`
2180    /// (which does not trim, and rejects it).
2181    #[test]
2182    fn test_bootstrap_servers_list_parsing() {
2183        for value in [
2184            "localhost:1,localhost:2",
2185            "localhost:1, localhost:2",
2186            " localhost:1 ,localhost:2 ",
2187        ] {
2188            let props = HashMap::from([("bootstrap.servers".to_string(), value.to_string())]);
2189            let config = ConsumerConfig::new(&props).unwrap();
2190            assert_eq!(
2191                config.bootstrap_servers(),
2192                ["localhost:1".to_string(), "localhost:2".to_string()],
2193                "{value:?}"
2194            );
2195            let addresses = crate::ClientUtils::parse_and_validate_addresses(
2196                config.bootstrap_servers(),
2197                config.client_dns_lookup(),
2198            )
2199            .unwrap();
2200            assert_eq!(addresses.len(), 2, "{value:?}");
2201        }
2202    }
2203
2204    /// `bootstrap.servers` is validated with Java's
2205    /// `ValidList.anyNonDuplicateValues(false, false)`: an empty
2206    /// element is rejected with `ConfigDef`'s exact message and duplicates are removed
2207    /// (single-message `ConfigException`, no `Invalid value` prefix). An empty list is rejected too.
2208    #[test]
2209    fn test_bootstrap_servers_valid_list() {
2210        let error_message = |value: &str| {
2211            let props = HashMap::from([("bootstrap.servers".to_string(), value.to_string())]);
2212            match ConsumerConfig::new(&props) {
2213                Err(Error::Config(e)) => e.message().to_string(),
2214                other => panic!("expected a ConfigError for {value:?}, got {other:?}"),
2215            }
2216        };
2217        for value in ["localhost:9092,,localhost:9093", "a:1, ,b:1", "a:1,"] {
2218            assert_eq!(
2219                error_message(value),
2220                "Configuration 'bootstrap.servers' values must not be empty.",
2221                "{value:?}"
2222            );
2223        }
2224        // `ConfigDef.parseValue` removes duplicates (with a warning) before validating.
2225        let props = HashMap::from([("bootstrap.servers".to_string(), "a:1,a:1".to_string())]);
2226        assert_eq!(ConsumerConfig::new(&props).unwrap().bootstrap_servers(), ["a:1".to_string()]);
2227        assert_eq!(
2228            error_message(",,"),
2229            "Configuration 'bootstrap.servers' values must not be empty."
2230        );
2231        for value in ["", "  "] {
2232            assert_eq!(
2233                error_message(value),
2234                "Configuration 'bootstrap.servers' must not be empty. Valid values include: any non-empty value",
2235                "{value:?}"
2236            );
2237        }
2238        let props = HashMap::from([("bootstrap.servers".to_string(), "a:1,b:1".to_string())]);
2239        assert_eq!(
2240            ConsumerConfig::new(&props).unwrap().bootstrap_servers(),
2241            ["a:1".to_string(), "b:1".to_string()]
2242        );
2243    }
2244
2245    /// Props with `bootstrap.servers` plus the given extra keys.
2246    fn props_with(extra: &[(&str, &str)]) -> HashMap<String, String> {
2247        let mut props = base_props();
2248        for (k, v) in extra {
2249            props.insert((*k).to_string(), (*v).to_string());
2250        }
2251        props
2252    }
2253
2254    /// Parses the `<n>` suffix of a generated `consumer-<group>-<n>` id.
2255    fn sequence_suffix<'a>(client_id: &'a str, prefix: &str) -> &'a str {
2256        client_id
2257            .strip_prefix(prefix)
2258            .unwrap_or_else(|| panic!("{client_id:?} does not start with {prefix:?}"))
2259    }
2260
2261    /// Without `client.id`, the id is `consumer-<group.id>-<n>` and every
2262    /// config draws a fresh `<n>` from the process-wide sequence.
2263    #[test]
2264    fn test_generated_client_id_uses_group_id_and_sequence() {
2265        let props = props_with(&[(ConsumerConfig::GROUP_ID_CONFIG, "test-group")]);
2266        let first = ConsumerConfig::new(&props).unwrap();
2267        let second = ConsumerConfig::new(&props).unwrap();
2268        let n1: i32 = sequence_suffix(first.client_id(), "consumer-test-group-").parse().unwrap();
2269        let n2: i32 = sequence_suffix(second.client_id(), "consumer-test-group-").parse().unwrap();
2270        assert!(n1 >= 1, "{}", first.client_id());
2271        assert_ne!(first.client_id(), second.client_id());
2272        // Other tests may draw from the shared sequence concurrently, so only
2273        // the direction is fixed, not the gap.
2274        assert!(n2 > n1, "{} then {}", first.client_id(), second.client_id());
2275    }
2276
2277    /// A copy keeps the configuration's resolved `client.id` and takes no
2278    /// sequence number, while the next configuration built takes the next
2279    /// one, as Java's next `new ConsumerConfig(props)` does. The copy's used
2280    /// set is its own, as `new` leaves it.
2281    #[test]
2282    fn test_copy_for_client_keeps_the_resolved_client_id() {
2283        let props = HashMap::from([
2284            (
2285                ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG.to_string(),
2286                "localhost:9092".to_string(),
2287            ),
2288            (ConsumerConfig::GROUP_ID_CONFIG.to_string(), "copy-group".to_string()),
2289        ]);
2290        let first = ConsumerConfig::new(&props).unwrap();
2291        let copy = first.copy_for_client().unwrap();
2292        let second_copy = first.copy_for_client().unwrap();
2293        let next = ConsumerConfig::new(&props).unwrap();
2294        assert_eq!(copy.client_id(), first.client_id());
2295        assert_eq!(second_copy.client_id(), first.client_id());
2296        assert_eq!(
2297            copy.config.get_string(ConsumerConfig::CLIENT_ID_CONFIG).unwrap(),
2298            Some(first.client_id())
2299        );
2300        let n1: i32 = sequence_suffix(first.client_id(), "consumer-copy-group-").parse().unwrap();
2301        let n2: i32 = sequence_suffix(next.client_id(), "consumer-copy-group-").parse().unwrap();
2302        // Other tests take numbers concurrently, so the next one is only later.
2303        assert!(n2 > n1, "{} then {}", first.client_id(), next.client_id());
2304        assert_eq!(copy.unused(), first.unused());
2305        first.config.ignore(ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG);
2306        assert!(copy.unused().contains(ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG));
2307
2308        let explicit = HashMap::from([
2309            (
2310                ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG.to_string(),
2311                "localhost:9092".to_string(),
2312            ),
2313            (ConsumerConfig::CLIENT_ID_CONFIG.to_string(), "mine".to_string()),
2314        ]);
2315        let config = ConsumerConfig::new(&explicit).unwrap();
2316        assert_eq!(config.copy_for_client().unwrap().client_id(), "mine");
2317    }
2318
2319    /// `new` logs each configuration warning once (the parse's duplicate
2320    /// values and the post-processing's); a copy
2321    /// for one more client logs none of them, as Java's client logs nothing
2322    /// about the config it is given, but makes the same reads, so its used set
2323    /// is `new`'s.
2324    #[test]
2325    fn test_copy_for_client_logs_no_configuration_warning() {
2326        let props = props_with(&[
2327            (ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG, "localhost:9092,localhost:9092"),
2328            (ConsumerConfig::GROUP_ID_CONFIG, "copy-log-group"),
2329            (ConsumerConfig::GROUP_PROTOCOL_CONFIG, "classic"),
2330            (ConsumerConfig::RETRY_BACKOFF_MS_CONFIG, "2000"),
2331            (ConsumerConfig::RETRY_BACKOFF_MAX_MS_CONFIG, "1000"),
2332            (ConsumerConfig::RECONNECT_BACKOFF_MS_CONFIG, "100"),
2333            (CommonClientConfigs::SOCKET_CONNECTION_SETUP_TIMEOUT_MS_CONFIG, "40000"),
2334            (CommonClientConfigs::SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS_CONFIG, "30000"),
2335            (ConsumerConfig::CONNECTIONS_MAX_IDLE_MS_CONFIG, "1000"),
2336            (ConsumerConfig::MAX_POLL_INTERVAL_MS_CONFIG, "300000"),
2337        ]);
2338        let (config, new_lines) = capture_logs(|| ConsumerConfig::new(&props).unwrap());
2339        let warnings = |lines: &[(Level, String)]| {
2340            lines
2341                .iter()
2342                .filter(|(level, _)| *level == Level::Warn)
2343                .map(|(_, line)| line.split(' ').take(3).collect::<Vec<_>>().join(" "))
2344                .collect::<Vec<_>>()
2345        };
2346        assert_eq!(
2347            warnings(&new_lines),
2348            [
2349                "Configuration key \"bootstrap.servers\"",
2350                "Configuration 'retry.backoff.ms' with",
2351                "Configuration 'socket.connection.setup.timeout.ms' with",
2352                "Disabling exponential reconnect",
2353                "Configuration 'connections.max.idle.ms' with",
2354            ],
2355            "{new_lines:?}"
2356        );
2357        let (copy, copy_lines) = capture_logs(|| config.copy_for_client().unwrap());
2358        assert!(copy_lines.is_empty(), "{copy_lines:?}");
2359        assert_eq!(copy.unused(), ConsumerConfig::new(&props).unwrap().unused());
2360    }
2361
2362    /// A static member uses its `group.instance.id` instead of the sequence.
2363    #[test]
2364    fn test_generated_client_id_uses_group_instance_id() {
2365        let props = props_with(&[
2366            (ConsumerConfig::GROUP_ID_CONFIG, "test-group"),
2367            (ConsumerConfig::GROUP_INSTANCE_ID_CONFIG, "inst"),
2368        ]);
2369        assert_eq!(ConsumerConfig::new(&props).unwrap().client_id(), "consumer-test-group-inst");
2370    }
2371
2372    /// An explicit `client.id` is never overridden.
2373    #[test]
2374    fn test_explicit_client_id_preserved() {
2375        let props = props_with(&[
2376            (ConsumerConfig::GROUP_ID_CONFIG, "test-group"),
2377            (ConsumerConfig::CLIENT_ID_CONFIG, "my-consumer"),
2378        ]);
2379        assert_eq!(ConsumerConfig::new(&props).unwrap().client_id(), "my-consumer");
2380    }
2381
2382    /// Java formats a missing `group.id` with `String.format("%s", null)`,
2383    /// yielding the literal `null`.
2384    #[test]
2385    fn test_generated_client_id_without_group_id() {
2386        let config = ConsumerConfig::new(&base_props()).unwrap();
2387        let n: i32 = sequence_suffix(config.client_id(), "consumer-null-").parse().unwrap();
2388        assert!(n >= 1, "{}", config.client_id());
2389    }
2390
2391    /// A `group.instance.id` that breaks the topic-name rules is rejected
2392    /// while deriving the client id, with `JoinGroupRequest`'s message.
2393    #[test]
2394    fn test_generated_client_id_rejects_invalid_group_instance_id() {
2395        let props = props_with(&[
2396            (ConsumerConfig::GROUP_ID_CONFIG, "test-group"),
2397            (ConsumerConfig::GROUP_INSTANCE_ID_CONFIG, "bad/id"),
2398        ]);
2399        let err = ConsumerConfig::new(&props).unwrap_err();
2400        assert!(matches!(err, Error::InvalidConfiguration(_)), "got {err:?}");
2401        assert!(err.is_kafka_error(), "got {err:?}");
2402        assert_eq!(
2403            err.message(),
2404            "Group instance id is invalid: 'bad/id' contains one or more characters other than ASCII \
2405             alphanumerics, '.', '_' and '-'"
2406        );
2407    }
2408
2409    /// With an explicit `client.id`, `ConsumerConfig` does not validate
2410    /// `group.instance.id` here; the consumer constructor does (Java
2411    /// `GroupRebalanceConfig`, see `AsyncKafkaConsumer::new`).
2412    #[test]
2413    fn test_explicit_client_id_skips_group_instance_id_validation() {
2414        let props = props_with(&[
2415            (ConsumerConfig::CLIENT_ID_CONFIG, "my-consumer"),
2416            (ConsumerConfig::GROUP_INSTANCE_ID_CONFIG, "bad/id"),
2417        ]);
2418        assert_eq!(ConsumerConfig::new(&props).unwrap().client_id(), "my-consumer");
2419    }
2420
2421    /// `ConfigDef.parseType` trims `client.id`, so a blank one generates an id
2422    /// and a padded one is kept without its padding.
2423    #[test]
2424    fn test_client_id_is_trimmed() {
2425        let blank = props_with(&[
2426            (ConsumerConfig::GROUP_ID_CONFIG, "test-group"),
2427            (ConsumerConfig::CLIENT_ID_CONFIG, " "),
2428        ]);
2429        let config = ConsumerConfig::new(&blank).unwrap();
2430        sequence_suffix(config.client_id(), "consumer-test-group-");
2431        let padded = props_with(&[(ConsumerConfig::CLIENT_ID_CONFIG, " my-consumer ")]);
2432        assert_eq!(ConsumerConfig::new(&padded).unwrap().client_id(), "my-consumer");
2433    }
2434
2435    /// `getString` answers a `STRING` key's value, `null` for an unset
2436    /// nullable one, the generated `client.id` when the property is unset, a
2437    /// cast failure for a key of another type, and `Unknown configuration` for
2438    /// a key the configuration does not hold.
2439    #[test]
2440    fn test_get_string() {
2441        let config = ConsumerConfig::new(&props_with(&[(ConsumerConfig::GROUP_ID_CONFIG, "test-group")])).unwrap();
2442        assert_eq!(Some("test-group"), config.get_string(ConsumerConfig::GROUP_ID_CONFIG).unwrap());
2443        assert_eq!(None, config.get_string(ConsumerConfig::GROUP_INSTANCE_ID_CONFIG).unwrap());
2444        let client_id = config.get_string(ConsumerConfig::CLIENT_ID_CONFIG).unwrap().unwrap();
2445        assert_eq!(config.client_id(), client_id);
2446        sequence_suffix(client_id, "consumer-test-group-");
2447
2448        // Every other `STRING` key, at its Java default.
2449        let defaults: &[(&str, Option<&str>)] = &[
2450            (ConsumerConfig::GROUP_PROTOCOL_CONFIG, Some("classic")),
2451            (ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG, None),
2452            (ConsumerConfig::CLIENT_DNS_LOOKUP_CONFIG, Some("use_all_dns_ips")),
2453            (ConsumerConfig::CLIENT_RACK_CONFIG, Some("")),
2454            (ConsumerConfig::AUTO_OFFSET_RESET_CONFIG, Some("latest")),
2455            (CommonClientConfigs::METADATA_RECOVERY_STRATEGY_CONFIG, Some("rebootstrap")),
2456            (
2457                ConsumerConfig::ISOLATION_LEVEL_CONFIG,
2458                Some(ConsumerConfig::DEFAULT_ISOLATION_LEVEL),
2459            ),
2460            (ConsumerConfig::METRICS_RECORDING_LEVEL_CONFIG, Some("INFO")),
2461            (ConsumerConfig::SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, Some("implicit")),
2462            (ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG, Some("BATCH_OPTIMIZED")),
2463            (CommonClientConfigs::SECURITY_PROTOCOL_CONFIG, Some("PLAINTEXT")),
2464            (SaslConfigs::SASL_MECHANISM, Some("GSSAPI")),
2465            (SslConfigs::SSL_TRUSTSTORE_LOCATION_CONFIG, None),
2466            (SslConfigs::SSL_TRUSTSTORE_TYPE_CONFIG, Some("PEM")),
2467            (SslConfigs::SSL_KEYSTORE_LOCATION_CONFIG, None),
2468            (SslConfigs::SSL_KEYSTORE_TYPE_CONFIG, Some("PEM")),
2469            (SslConfigs::SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, Some("https")),
2470        ];
2471        for (key, expected) in defaults {
2472            assert_eq!(*expected, config.get_string(key).unwrap(), "for {key}");
2473        }
2474
2475        // An unset `PASSWORD` key is Java's `null` default, which casts to
2476        // `String`; only a set one fails the cast (below).
2477        for key in [
2478            SaslConfigs::SASL_JAAS_CONFIG,
2479            SslConfigs::SSL_TRUSTSTORE_CERTIFICATES_CONFIG,
2480            SslConfigs::SSL_KEYSTORE_KEY_CONFIG,
2481            SslConfigs::SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG,
2482        ] {
2483            assert_eq!(None, config.get_string(key).unwrap(), "for {key}");
2484        }
2485
2486        // Every other `STRING` key, set (padded, as `getString` trims).
2487        let set: &[(&str, &str, &str)] = &[
2488            (ConsumerConfig::GROUP_PROTOCOL_CONFIG, "consumer", "consumer"),
2489            (ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG, " uniform ", "uniform"),
2490            (
2491                ConsumerConfig::CLIENT_DNS_LOOKUP_CONFIG,
2492                "resolve_canonical_bootstrap_servers_only",
2493                "resolve_canonical_bootstrap_servers_only",
2494            ),
2495            (ConsumerConfig::CLIENT_RACK_CONFIG, " rack-1 ", "rack-1"),
2496            (ConsumerConfig::AUTO_OFFSET_RESET_CONFIG, "earliest", "earliest"),
2497            (CommonClientConfigs::METADATA_RECOVERY_STRATEGY_CONFIG, "none", "none"),
2498            (ConsumerConfig::ISOLATION_LEVEL_CONFIG, "read_uncommitted", "read_uncommitted"),
2499            (ConsumerConfig::METRICS_RECORDING_LEVEL_CONFIG, "DEBUG", "DEBUG"),
2500            (CommonClientConfigs::SECURITY_PROTOCOL_CONFIG, " sasl_ssl ", "sasl_ssl"),
2501            (SaslConfigs::SASL_MECHANISM, " PLAIN ", "PLAIN"),
2502            (SslConfigs::SSL_TRUSTSTORE_LOCATION_CONFIG, " /trust.pem ", "/trust.pem"),
2503            (SslConfigs::SSL_TRUSTSTORE_TYPE_CONFIG, "JKS", "JKS"),
2504            (SslConfigs::SSL_KEYSTORE_LOCATION_CONFIG, "/key.pem", "/key.pem"),
2505            (SslConfigs::SSL_KEYSTORE_TYPE_CONFIG, "PKCS12", "PKCS12"),
2506            (SslConfigs::SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, "", ""),
2507        ];
2508        for (key, value, expected) in set {
2509            let mut extra = vec![(*key, *value)];
2510            // `group.remote.assignor` cannot be set with the `classic` protocol.
2511            if *key == ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG {
2512                extra.push((ConsumerConfig::GROUP_PROTOCOL_CONFIG, "consumer"));
2513            }
2514            // A SASL protocol accepts only the `PLAIN` mechanism.
2515            if *key == CommonClientConfigs::SECURITY_PROTOCOL_CONFIG {
2516                extra.push((SaslConfigs::SASL_MECHANISM, "PLAIN"));
2517            }
2518            let config = ConsumerConfig::new(&props_with(&extra)).unwrap();
2519            assert_eq!(Some(*expected), config.get_string(key).unwrap(), "for {key}={value:?}");
2520        }
2521
2522        // A key of another type fails the cast, outside the `KafkaException`
2523        // hierarchy as Java's `ClassCastException` is.
2524        let other_types: &[(&str, &str)] = &[
2525            (ConsumerConfig::MAX_POLL_RECORDS_CONFIG, "INT"),
2526            (ConsumerConfig::METADATA_MAX_AGE_CONFIG, "LONG"),
2527            (ConsumerConfig::ENABLE_AUTO_COMMIT_CONFIG, "BOOLEAN"),
2528            (ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG, "LIST"),
2529            (SslConfigs::SSL_KEYSTORE_KEY_CONFIG, "PASSWORD"),
2530            (SaslConfigs::SASL_JAAS_CONFIG, "PASSWORD"),
2531        ];
2532        let passwords = ConsumerConfig::new(&props_with(&[
2533            (SslConfigs::SSL_KEYSTORE_KEY_CONFIG, "secret"),
2534            (SaslConfigs::SASL_JAAS_CONFIG, "x required;"),
2535        ]))
2536        .unwrap();
2537        for (key, config_type) in other_types {
2538            let config = if *config_type == "PASSWORD" {
2539                &passwords
2540            } else {
2541                &config
2542            };
2543            match config.get_string(key) {
2544                Err(e @ Error::LocalIllegalArgument(_)) => {
2545                    assert_eq!(
2546                        format!("Configuration '{key}' is of type {config_type}, not STRING"),
2547                        e.message()
2548                    );
2549                    assert!(!e.is_kafka_error());
2550                },
2551                other => panic!("expected an IllegalArgumentError for {key}, got {other:?}"),
2552            }
2553        }
2554
2555        match config.get_string("no.such.key") {
2556            Err(Error::Config(e)) => assert_eq!("Unknown configuration 'no.such.key'", e.message()),
2557            other => panic!("expected a ConfigError, got {other:?}"),
2558        }
2559    }
2560
2561    /// `client.rack` is a `Type.STRING`, so `ConfigDef.parseType` trims it
2562    /// (Critic 96 F3); unset, it is `DEFAULT_CLIENT_RACK`, the empty string.
2563    #[test]
2564    fn test_client_rack_is_trimmed_and_defaults_to_empty() {
2565        assert_eq!("client.rack", ConsumerConfig::CLIENT_RACK_CONFIG);
2566        assert_eq!("", ConsumerConfig::DEFAULT_CLIENT_RACK);
2567        assert_eq!(
2568            ConsumerConfig::DEFAULT_CLIENT_RACK,
2569            ConsumerConfig::new(&props_with(&[])).unwrap().client_rack()
2570        );
2571        let padded = props_with(&[(ConsumerConfig::CLIENT_RACK_CONFIG, " rack-1 ")]);
2572        assert_eq!("rack-1", ConsumerConfig::new(&padded).unwrap().client_rack());
2573        let blank = props_with(&[(ConsumerConfig::CLIENT_RACK_CONFIG, "  ")]);
2574        assert_eq!("", ConsumerConfig::new(&blank).unwrap().client_rack());
2575    }
2576
2577    /// `group.instance.id` is trimmed before Java's `NonEmptyString` check, so
2578    /// a blank one is a config error for that key, not the instance-id
2579    /// validator's `InvalidConfiguration`.
2580    #[test]
2581    fn test_blank_group_instance_id_is_config_error() {
2582        let props = props_with(&[
2583            (ConsumerConfig::GROUP_ID_CONFIG, "test-group"),
2584            (ConsumerConfig::GROUP_INSTANCE_ID_CONFIG, " "),
2585        ]);
2586        let err = ConsumerConfig::new(&props).unwrap_err();
2587        assert!(matches!(err, Error::Config(_)), "got {err:?}");
2588        assert!(
2589            err.message().contains(ConsumerConfig::GROUP_INSTANCE_ID_CONFIG),
2590            "{}",
2591            err.message()
2592        );
2593        assert!(err.message().contains("String must be non-empty"), "{}", err.message());
2594
2595        let padded = props_with(&[(ConsumerConfig::GROUP_INSTANCE_ID_CONFIG, " inst ")]);
2596        assert_eq!(ConsumerConfig::new(&padded).unwrap().group_instance_id(), Some("inst"));
2597    }
2598
2599    /// `ConfigDef.parseType` trims `group.id`, so the padding reaches neither
2600    /// the group id nor the generated `client.id` embedding it.
2601    #[test]
2602    fn test_group_id_is_trimmed() {
2603        let config = ConsumerConfig::new(&props_with(&[(ConsumerConfig::GROUP_ID_CONFIG, " g ")])).unwrap();
2604        assert_eq!(config.group_id(), Some("g"));
2605        sequence_suffix(config.client_id(), "consumer-g-");
2606        // A whitespace-only id trims to the empty string, kept as `Some("")`
2607        // so the consumer constructor rejects it as Java does.
2608        let config = ConsumerConfig::new(&props_with(&[(ConsumerConfig::GROUP_ID_CONFIG, " ")])).unwrap();
2609        assert_eq!(config.group_id(), Some(""));
2610    }
2611
2612    // -------- Translated from `org.apache.kafka.clients.consumer.ConsumerConfigTest` --------
2613    //
2614    // Java reads values back through `AbstractConfig`'s keyed getters. Of
2615    // those, `getString` is translated as [`ConsumerConfig::get_string`]; the
2616    // others (`getList`, `getBoolean`, `getInt`, `originals()`) are not, and the
2617    // crate-private getters and fields stand in for them, which is why these
2618    // tests live in the crate rather than in `tests/consumer`. Java seeds `key.deserializer` / `value.deserializer` into
2619    // every property map (`org.apache.kafka.clients.consumer.ConsumerConfigTest`) because its `ConfigDef`
2620    // defines them with no default; Rust takes the deserializers as constructor
2621    // arguments to the consumer, and `ConsumerConfig` holds no deserializer state,
2622    // so the two keys are absent from `base_props`.
2623    //
2624    // Skipped tests:
2625    // - `testAppendDeserializerToConfig`, `testAppendDeserializerToConfigWithException`
2626    //   — exercise the Java static `appendDeserializerToConfig` helper that mutates a
2627    //   typed `Map<String, Object>`; there is no equivalent function to test.
2628    // - `testDefaultPartitionAssignor` — see `test_partition_assignment_strategy_is_unknown`.
2629    // - `testValidateConfigPropertiesFile` — reads `config/consumer.properties` from
2630    //   the Apache Kafka source tree; not part of the Rust client.
2631    // `testInvalidSecurityProtocol` and `testCaseInsensitiveSecurityProtocol` are
2632    // `test_invalid_security_protocol` and `test_case_insensitive_security_protocol`
2633    // above.
2634
2635    /// Translated from `ConsumerConfigTest.ensureDefaultThrowOnUnsupportedStableFlagToFalse`.
2636    #[test]
2637    #[doc(
2638        alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#ensureDefaultThrowOnUnsupportedStableFlagToFalse"
2639    )]
2640    fn ensure_default_return_on_unsupported_stable_flag_to_false() {
2641        let config = ConsumerConfig::new(&base_props()).unwrap();
2642        assert!(!config.throw_on_fetch_stable_offset_unsupported());
2643    }
2644
2645    /// `ConsumerConfigTest.testDefaultPartitionAssignor` is not translated: it
2646    /// checks Java's default `[RangeAssignor, CooperativeStickyAssignor]`, and
2647    /// the client-side assignors are out of scope (consumer-threading.md §20).
2648    /// The key is not a consumer config here: it is accepted and reported by
2649    /// [`ConsumerConfig::unused`].
2650    #[test]
2651    fn test_partition_assignment_strategy_is_unknown() {
2652        let mut props = base_props();
2653        props.insert(
2654            ConsumerConfig::PARTITION_ASSIGNMENT_STRATEGY_CONFIG.to_string(),
2655            "org.apache.kafka.clients.consumer.RangeAssignor".to_string(),
2656        );
2657        let config = ConsumerConfig::new(&props).unwrap();
2658        match config.get_string(ConsumerConfig::PARTITION_ASSIGNMENT_STRATEGY_CONFIG) {
2659            Err(Error::Config(e)) => {
2660                assert_eq!("Unknown configuration 'partition.assignment.strategy'", e.message())
2661            },
2662            other => panic!("expected a ConfigError, got {other:?}"),
2663        }
2664    }
2665
2666    /// `security.providers` names JSSE `SecurityProviderCreator` classes, which
2667    /// have no rustls counterpart: the key is not defined.
2668    #[test]
2669    fn test_security_providers_is_unknown() {
2670        let mut props = base_props();
2671        props.insert("security.providers".to_string(), "my.Provider".to_string());
2672        let config = ConsumerConfig::new(&props).unwrap();
2673        match config.get_string("security.providers") {
2674            Err(Error::Config(e)) => assert_eq!("Unknown configuration 'security.providers'", e.message()),
2675            other => panic!("expected a ConfigError, got {other:?}"),
2676        }
2677    }
2678
2679    /// Translated from `ConsumerConfigTest.testOverrideClientId`.
2680    #[test]
2681    #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testOverrideClientId")]
2682    fn test_override_client_id() {
2683        let mut props = base_props();
2684        props.insert(ConsumerConfig::GROUP_ID_CONFIG.to_string(), "test-group".to_string());
2685        let config = ConsumerConfig::new(&props).unwrap();
2686        assert!(!config.client_id().is_empty());
2687    }
2688
2689    /// Translated from `ConsumerConfigTest.testInvalidGroupInstanceId`: the
2690    /// `group.instance.id` empty-string check is enforced.
2691    #[test]
2692    #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testInvalidGroupInstanceId")]
2693    fn test_invalid_group_instance_id() {
2694        let mut props = base_props();
2695        props.insert(ConsumerConfig::GROUP_INSTANCE_ID_CONFIG.to_string(), String::new());
2696        let err = ConsumerConfig::new(&props).unwrap_err();
2697        assert!(
2698            err.message().contains(ConsumerConfig::GROUP_INSTANCE_ID_CONFIG),
2699            "error message should mention the failing config key, got: {}",
2700            err.message()
2701        );
2702    }
2703
2704    /// Translated from `ConsumerConfigTest.testDefaultConsumerGroupConfig`.
2705    ///
2706    /// Java's default for `group.protocol` is `"classic"`
2707    /// (`ConsumerConfig.DEFAULT_GROUP_PROTOCOL`); `group.remote.assignor` is `null`.
2708    #[test]
2709    #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testDefaultConsumerGroupConfig")]
2710    fn test_default_consumer_group_config() {
2711        let config = ConsumerConfig::new(&base_props()).unwrap();
2712        assert_eq!(config.group_protocol(), "classic");
2713        assert_eq!(config.group_remote_assignor(), None);
2714    }
2715
2716    /// Translated from `ConsumerConfigTest.testRemoteAssignorConfig`.
2717    #[test]
2718    #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testRemoteAssignorConfig")]
2719    fn test_remote_assignor_config() {
2720        let mut props = base_props();
2721        let remote_assignor_name = "SomeAssignor";
2722        let protocol = "consumer";
2723        props.insert(
2724            ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG.to_string(),
2725            remote_assignor_name.to_string(),
2726        );
2727        props.insert(ConsumerConfig::GROUP_PROTOCOL_CONFIG.to_string(), protocol.to_string());
2728        let config = ConsumerConfig::new(&props).unwrap();
2729        assert_eq!(config.group_protocol(), protocol);
2730        assert_eq!(config.group_remote_assignor(), Some(remote_assignor_name));
2731    }
2732
2733    /// Translated from `ConsumerConfigTest.testDefaultMetadataRecoveryStrategy`.
2734    #[test]
2735    #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testDefaultMetadataRecoveryStrategy")]
2736    fn test_default_metadata_recovery_strategy() {
2737        let config = ConsumerConfig::new(&base_props()).unwrap();
2738        assert_eq!(config.metadata_recovery_strategy(), "rebootstrap");
2739    }
2740
2741    /// Translated from `ConsumerConfigTest.testInvalidMetadataRecoveryStrategy`.
2742    #[test]
2743    #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testInvalidMetadataRecoveryStrategy")]
2744    fn test_invalid_metadata_recovery_strategy() {
2745        let mut props = base_props();
2746        props.insert(
2747            CommonClientConfigs::METADATA_RECOVERY_STRATEGY_CONFIG.to_string(),
2748            "abc".to_string(),
2749        );
2750        let err = ConsumerConfig::new(&props).unwrap_err();
2751        assert!(
2752            err.message().contains(CommonClientConfigs::METADATA_RECOVERY_STRATEGY_CONFIG),
2753            "error message should mention the failing config key, got: {}",
2754            err.message()
2755        );
2756    }
2757
2758    /// Translated from `ConsumerConfigTest.testProtocolConfigValidation`.
2759    ///
2760    /// Java uses `@ParameterizedTest @CsvSource(...)`; the same inputs are iterated.
2761    #[test]
2762    #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testProtocolConfigValidation")]
2763    fn test_protocol_config_validation() {
2764        let cases: &[(&str, bool)] = &[
2765            ("consumer", true),
2766            ("classic", true),
2767            ("Consumer", true),
2768            ("Classic", true),
2769            ("invalid", false),
2770        ];
2771        for (protocol, is_valid) in cases {
2772            let mut props = base_props();
2773            props.insert(ConsumerConfig::GROUP_PROTOCOL_CONFIG.to_string(), protocol.to_string());
2774            if *is_valid {
2775                let config = ConsumerConfig::new(&props).unwrap();
2776                assert_eq!(config.group_protocol(), *protocol);
2777            } else {
2778                let err = ConsumerConfig::new(&props).unwrap_err();
2779                assert!(
2780                    err.message().contains(ConsumerConfig::GROUP_PROTOCOL_CONFIG),
2781                    "case {protocol}: {}",
2782                    err.message()
2783                );
2784            }
2785        }
2786    }
2787    /// Translated from `ConsumerConfigTest.testOverrideEnableAutoCommit`.
2788    #[test]
2789    #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testOverrideEnableAutoCommit")]
2790    fn test_override_enable_auto_commit() {
2791        let enable_auto_commit =
2792            |config: &ConsumerConfig| config.get_boolean(ConsumerConfig::ENABLE_AUTO_COMMIT_CONFIG).unwrap();
2793        // Verify that our default properties (no 'enable.auto.commit' or 'group.id') are valid.
2794        let mut props = base_props();
2795        let config = ConsumerConfig::new(&props).unwrap();
2796        assert_eq!(Some(false), enable_auto_commit(&config));
2797        assert!(!config.enable_auto_commit());
2798
2799        // Verify that explicitly disabling 'enable.auto.commit' still works.
2800        props.insert(ConsumerConfig::ENABLE_AUTO_COMMIT_CONFIG.to_string(), "false".to_string());
2801        assert_eq!(Some(false), enable_auto_commit(&ConsumerConfig::new(&props).unwrap()));
2802
2803        // Verify that enabling 'enable.auto.commit' but without 'group.id' fails.
2804        props.insert(ConsumerConfig::ENABLE_AUTO_COMMIT_CONFIG.to_string(), "true".to_string());
2805        let error = ConsumerConfig::new(&props).unwrap_err();
2806        assert!(error.is_invalid_configuration_error(), "{error:?}");
2807        assert_eq!(
2808            "enable.auto.commit cannot be set to true when default group id (null) is used.",
2809            error.message()
2810        );
2811
2812        // Verify that then adding 'group.id' to the mix allows it to pass OK.
2813        props.insert(ConsumerConfig::GROUP_ID_CONFIG.to_string(), "test-group".to_string());
2814        let config = ConsumerConfig::new(&props).unwrap();
2815        assert_eq!(Some(true), enable_auto_commit(&config));
2816        assert!(config.enable_auto_commit());
2817
2818        // Now remove the 'enable.auto.commit' flag and verify that it is set to true (the default).
2819        props.remove(ConsumerConfig::ENABLE_AUTO_COMMIT_CONFIG);
2820        assert_eq!(Some(true), enable_auto_commit(&ConsumerConfig::new(&props).unwrap()));
2821    }
2822
2823    /// Translated from `ConsumerConfigTest.testRemoteAssignorWithClassicGroupProtocol`.
2824    #[test]
2825    #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testRemoteAssignorWithClassicGroupProtocol")]
2826    fn test_remote_assignor_with_classic_group_protocol() {
2827        let props = props_with(&[(ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG, "SomeAssignor")]);
2828        let error = ConsumerConfig::new(&props).unwrap_err();
2829        assert!(matches!(error, Error::Config(_)), "{error:?}");
2830        assert!(
2831            error
2832                .message()
2833                .contains("group.remote.assignor cannot be set when group.protocol=CLASSIC"),
2834            "{}",
2835            error.message()
2836        );
2837    }
2838
2839    /// Translated from `ConsumerConfigTest.testUnsupportedConfigsWithConsumerGroupProtocol`.
2840    #[test]
2841    #[doc(
2842        alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testUnsupportedConfigsWithConsumerGroupProtocol"
2843    )]
2844    fn test_unsupported_configs_with_consumer_group_protocol() {
2845        for (config_name, value) in [
2846            (ConsumerConfig::PARTITION_ASSIGNMENT_STRATEGY_CONFIG, "RoundRobinAssignor"),
2847            (ConsumerConfig::HEARTBEAT_INTERVAL_MS_CONFIG, "1000"),
2848            (ConsumerConfig::SESSION_TIMEOUT_MS_CONFIG, "30000"),
2849        ] {
2850            let props = props_with(&[
2851                (ConsumerConfig::GROUP_PROTOCOL_CONFIG, "CONSUMER"),
2852                (config_name, value),
2853            ]);
2854            let error = ConsumerConfig::new(&props).unwrap_err();
2855            assert!(matches!(error, Error::Config(_)), "{error:?}");
2856            assert_eq!(
2857                format!("{config_name} cannot be set when group.protocol=CONSUMER"),
2858                error.message()
2859            );
2860        }
2861    }
2862
2863    /// The share-consumer keys are not supported by either group protocol of
2864    /// `KafkaConsumer` (`CLASSIC_PROTOCOL_UNSUPPORTED_CONFIGS`,
2865    /// `CONSUMER_PROTOCOL_UNSUPPORTED_CONFIGS`); every unsupported key set is
2866    /// listed, in Java's order, and a blank value is not "set".
2867    #[test]
2868    fn test_unsupported_configs_are_listed_in_order() {
2869        let props = props_with(&[
2870            (ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG, "record_limit"),
2871            (ConsumerConfig::SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, "explicit"),
2872            (ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG, " "),
2873        ]);
2874        assert_eq!(
2875            "share.acknowledgement.mode, share.acquire.mode cannot be set when group.protocol=CLASSIC",
2876            ConsumerConfig::new(&props).unwrap_err().message()
2877        );
2878        let mut props = props;
2879        props.insert(ConsumerConfig::GROUP_PROTOCOL_CONFIG.to_string(), "consumer".to_string());
2880        props.insert(ConsumerConfig::SESSION_TIMEOUT_MS_CONFIG.to_string(), "30000".to_string());
2881        assert_eq!(
2882            "session.timeout.ms, share.acknowledgement.mode, share.acquire.mode cannot be set when \
2883             group.protocol=CONSUMER",
2884            ConsumerConfig::new(&props).unwrap_err().message()
2885        );
2886        // The checked keys are read through Java's `RecordingMap`, so they are used.
2887        let config = ConsumerConfig::new(&props_with(&[(ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG, " ")])).unwrap();
2888        assert!(!config.unused().contains(ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG));
2889    }
2890
2891    /// Java's `ShareAcquireMode.Validator` and `ShareAcknowledgementMode.Validator`
2892    /// reject a blank value at parse time, which the unsupported-config check
2893    /// would skip, and an unknown one, with their messages; the acquire mode
2894    /// is matched case-insensitively, the acknowledgement mode exactly.
2895    #[test]
2896    fn test_share_mode_validators() {
2897        for group_protocol in ["classic", "consumer"] {
2898            for (config_name, value, expected) in [
2899                (
2900                    ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG,
2901                    "",
2902                    "Invalid value  for configuration share.acquire.mode: Invalid value `` for configuration \
2903                     share.acquire.mode. The value must either be 'batch_optimized' or 'record_limit'.",
2904                ),
2905                (
2906                    ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG,
2907                    "  ",
2908                    "Invalid value  for configuration share.acquire.mode: Invalid value `` for configuration \
2909                     share.acquire.mode. The value must either be 'batch_optimized' or 'record_limit'.",
2910                ),
2911                (
2912                    ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG,
2913                    "invalid",
2914                    "Invalid value invalid for configuration share.acquire.mode: Invalid value `invalid` for \
2915                     configuration share.acquire.mode. The value must either be 'batch_optimized' or 'record_limit'.",
2916                ),
2917                (
2918                    ConsumerConfig::SHARE_ACKNOWLEDGEMENT_MODE_CONFIG,
2919                    "",
2920                    "Invalid value  for configuration share.acknowledgement.mode: Invalid value `` for \
2921                     configuration share.acknowledgement.mode. The value must either be 'implicit' or 'explicit'.",
2922                ),
2923                (
2924                    ConsumerConfig::SHARE_ACKNOWLEDGEMENT_MODE_CONFIG,
2925                    "IMPLICIT",
2926                    "Invalid value IMPLICIT for configuration share.acknowledgement.mode: Invalid value `IMPLICIT` \
2927                     for configuration share.acknowledgement.mode. The value must either be 'implicit' or \
2928                     'explicit'.",
2929                ),
2930            ] {
2931                let error = ConsumerConfig::new(&props_with(&[
2932                    (ConsumerConfig::GROUP_PROTOCOL_CONFIG, group_protocol),
2933                    (config_name, value),
2934                ]))
2935                .unwrap_err();
2936                assert!(matches!(error, Error::Config(_)), "{error:?}");
2937                assert_eq!(expected, error.message(), "{group_protocol} {config_name}={value:?}");
2938            }
2939        }
2940        // A valid value passes the validator (and is then rejected as unsupported).
2941        for value in ["record_limit", "Batch_Optimized", "BATCH_OPTIMIZED"] {
2942            assert_eq!(
2943                "share.acquire.mode cannot be set when group.protocol=CLASSIC",
2944                ConsumerConfig::new(&props_with(&[(ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG, value)]))
2945                    .unwrap_err()
2946                    .message()
2947            );
2948        }
2949    }
2950
2951    /// The `group.protocol` validator is Java's
2952    /// `CaseInsensitiveValidString.in(CLASSIC, CONSUMER)`, with its message,
2953    /// which lists the values in the order of Java's `HashSet`.
2954    #[test]
2955    fn test_group_protocol_validator_message() {
2956        let error =
2957            ConsumerConfig::new(&props_with(&[(ConsumerConfig::GROUP_PROTOCOL_CONFIG, "invalid")])).unwrap_err();
2958        assert_eq!(
2959            "Invalid value invalid for configuration group.protocol: String must be one of (case insensitive): \
2960             CONSUMER, CLASSIC",
2961            error.message()
2962        );
2963    }
2964
2965    /// Java's `atLeast` validators (`ConsumerConfig.CONFIG`), each with
2966    /// `ConfigDef.Range`'s message.
2967    #[test]
2968    fn test_at_least_validators() {
2969        let cases: &[(&str, &str, &str)] = &[
2970            (ConsumerConfig::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG, "-1", "0"),
2971            (ConsumerConfig::METADATA_MAX_AGE_CONFIG, "-1", "0"),
2972            (ConsumerConfig::AUTO_COMMIT_INTERVAL_MS_CONFIG, "-1", "0"),
2973            (ConsumerConfig::MAX_PARTITION_FETCH_BYTES_CONFIG, "-1", "0"),
2974            (ConsumerConfig::SEND_BUFFER_CONFIG, "-2", "-1"),
2975            (ConsumerConfig::RECEIVE_BUFFER_CONFIG, "-2", "-1"),
2976            (ConsumerConfig::FETCH_MIN_BYTES_CONFIG, "-1", "0"),
2977            (ConsumerConfig::FETCH_MAX_BYTES_CONFIG, "-1", "0"),
2978            (ConsumerConfig::FETCH_MAX_WAIT_MS_CONFIG, "-1", "0"),
2979            (ConsumerConfig::RECONNECT_BACKOFF_MS_CONFIG, "-1", "0"),
2980            (ConsumerConfig::RECONNECT_BACKOFF_MAX_MS_CONFIG, "-1", "0"),
2981            (ConsumerConfig::RETRY_BACKOFF_MS_CONFIG, "-1", "0"),
2982            (ConsumerConfig::RETRY_BACKOFF_MAX_MS_CONFIG, "-1", "0"),
2983            (ConsumerConfig::METRICS_SAMPLE_WINDOW_MS_CONFIG, "-1", "0"),
2984            (ConsumerConfig::METRICS_NUM_SAMPLES_CONFIG, "0", "1"),
2985            (ConsumerConfig::REQUEST_TIMEOUT_MS_CONFIG, "-1", "0"),
2986            (ConsumerConfig::DEFAULT_API_TIMEOUT_MS_CONFIG, "-1", "0"),
2987            (ConsumerConfig::MAX_POLL_RECORDS_CONFIG, "0", "1"),
2988            (ConsumerConfig::MAX_POLL_INTERVAL_MS_CONFIG, "0", "1"),
2989            (CommonClientConfigs::METADATA_RECOVERY_REBOOTSTRAP_TRIGGER_MS_CONFIG, "-1", "0"),
2990        ];
2991        for (key, value, bound) in cases {
2992            let error = ConsumerConfig::new(&props_with(&[(key, value)])).unwrap_err();
2993            assert!(matches!(error, Error::Config(_)), "{key}: {error:?}");
2994            assert_eq!(
2995                format!("Invalid value {value} for configuration {key}: Value must be at least {bound}"),
2996                error.message()
2997            );
2998            // The bound itself is accepted.
2999            ConsumerConfig::new(&props_with(&[(key, bound)])).unwrap_or_else(|e| panic!("{key}={bound}: {e:?}"));
3000        }
3001    }
3002
3003    /// `isolation.level` is Java's case-sensitive `ValidString.in`.
3004    #[test]
3005    fn test_isolation_level_is_case_sensitive() {
3006        let error = ConsumerConfig::new(&props_with(&[(ConsumerConfig::ISOLATION_LEVEL_CONFIG, "READ_COMMITTED")]))
3007            .unwrap_err();
3008        assert_eq!(
3009            "Invalid value READ_COMMITTED for configuration isolation.level: String must be one of: read_committed, \
3010             read_uncommitted",
3011            error.message()
3012        );
3013    }
3014
3015    /// The `connections.max.idle.ms` warning does not fail the configuration.
3016    #[test]
3017    fn test_connections_max_idle_ms_lower_than_max_poll_interval_ms_only_warns() {
3018        let config = ConsumerConfig::new(&props_with(&[
3019            (ConsumerConfig::CONNECTIONS_MAX_IDLE_MS_CONFIG, "1000"),
3020            (ConsumerConfig::MAX_POLL_INTERVAL_MS_CONFIG, "2000"),
3021        ]))
3022        .unwrap();
3023        assert_eq!(1000, config.connections_max_idle_ms);
3024    }
3025
3026    /// `AbstractConfig`'s typed getters answer every key's `ConfigDef` type.
3027    #[test]
3028    fn test_typed_getters() {
3029        let config = ConsumerConfig::new(&props_with(&[
3030            (ConsumerConfig::GROUP_ID_CONFIG, "g"),
3031            (ConsumerConfig::MAX_POLL_RECORDS_CONFIG, "7"),
3032        ]))
3033        .unwrap();
3034        assert_eq!(Some(7), config.get_int(ConsumerConfig::MAX_POLL_RECORDS_CONFIG).unwrap());
3035        assert_eq!(
3036            Some(540_000),
3037            config.get_long(ConsumerConfig::CONNECTIONS_MAX_IDLE_MS_CONFIG).unwrap()
3038        );
3039        assert_eq!(Some(true), config.get_boolean(ConsumerConfig::CHECK_CRCS_CONFIG).unwrap());
3040        assert_eq!(
3041            Some(&["localhost:9092".to_string()][..]),
3042            config.get_list(ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG).unwrap()
3043        );
3044        assert_eq!(Some("g"), config.get_string(ConsumerConfig::GROUP_ID_CONFIG).unwrap());
3045        assert_eq!(
3046            "Configuration 'max.poll.records' is of type INT, not LONG",
3047            config.get_long(ConsumerConfig::MAX_POLL_RECORDS_CONFIG).unwrap_err().message()
3048        );
3049        assert_eq!(
3050            "Configuration 'check.crcs' is of type BOOLEAN, not SHORT",
3051            config.get_short(ConsumerConfig::CHECK_CRCS_CONFIG).unwrap_err().message()
3052        );
3053        assert_eq!(
3054            "Configuration 'group.id' is of type STRING, not DOUBLE",
3055            config.get_double(ConsumerConfig::GROUP_ID_CONFIG).unwrap_err().message()
3056        );
3057        assert_eq!(
3058            "Unknown configuration 'no.such.key'",
3059            config.get_int("no.such.key").unwrap_err().message()
3060        );
3061    }
3062
3063    /// `configNames`.
3064    #[test]
3065    fn test_config_names() {
3066        let names = ConsumerConfig::config_names();
3067        for key in [
3068            ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG,
3069            ConsumerConfig::GROUP_PROTOCOL_CONFIG,
3070            ConsumerConfig::THROW_ON_FETCH_STABLE_OFFSET_UNSUPPORTED,
3071            ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG,
3072            SaslConfigs::SASL_MECHANISM,
3073            SslConfigs::SSL_KEYSTORE_TYPE_CONFIG,
3074            "ssl.cipher.suites",
3075        ] {
3076            assert!(names.contains(key), "{key}");
3077        }
3078        for key in [
3079            ConsumerConfig::PARTITION_ASSIGNMENT_STRATEGY_CONFIG,
3080            "security.providers",
3081            "config.providers",
3082            "enable.metrics.push",
3083            "ssl.keystore.password",
3084            "ssl.key.password",
3085            "ssl.truststore.password",
3086            "key.deserializer",
3087            "metric.reporters",
3088            "interceptor.classes",
3089        ] {
3090            assert!(!names.contains(key), "{key}");
3091        }
3092    }
3093
3094    /// `unused` reports the supplied keys nobody read; the getters, `ignore`
3095    /// and the consumer's own reads mark keys used.
3096    #[test]
3097    fn test_unused_and_ignore() {
3098        let config = ConsumerConfig::new(&props_with(&[
3099            (ConsumerConfig::GROUP_ID_CONFIG, "g"),
3100            (ConsumerConfig::FETCH_MIN_BYTES_CONFIG, "2"),
3101            (SslConfigs::SSL_KEYSTORE_TYPE_CONFIG, "PKCS12"),
3102            ("unknown.key", "x"),
3103        ]))
3104        .unwrap();
3105        // `postProcessParsedConfig` read `group.id`; nothing else yet.
3106        assert_eq!(
3107            HashSet::from([
3108                ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG.to_string(),
3109                ConsumerConfig::FETCH_MIN_BYTES_CONFIG.to_string(),
3110                SslConfigs::SSL_KEYSTORE_TYPE_CONFIG.to_string(),
3111                "unknown.key".to_string(),
3112            ]),
3113            config.unused()
3114        );
3115        config.get_int(ConsumerConfig::FETCH_MIN_BYTES_CONFIG).unwrap();
3116        config.ignore("unknown.key");
3117        // The consumer's reads: the `ssl.*` keys only for an SSL protocol.
3118        config.mark_read_keys_used();
3119        assert_eq!(
3120            HashSet::from([SslConfigs::SSL_KEYSTORE_TYPE_CONFIG.to_string()]),
3121            config.unused()
3122        );
3123        config.log_unused();
3124    }
3125
3126    /// `enable.metrics.push` is not defined until KIP-714 client telemetry is
3127    /// implemented, so a value set for it is only reported as unused.
3128    #[test]
3129    fn test_enable_metrics_push_is_unused() {
3130        let config = ConsumerConfig::new(&props_with(&[("enable.metrics.push", "true")])).unwrap();
3131        config.mark_read_keys_used();
3132        assert_eq!(HashSet::from(["enable.metrics.push".to_string()]), config.unused());
3133        assert_eq!(
3134            "Unknown configuration 'enable.metrics.push'",
3135            config.get_boolean("enable.metrics.push").unwrap_err().message()
3136        );
3137    }
3138
3139    /// Java's `equals` / `hashCode` compare the originals.
3140    #[test]
3141    fn test_equals_and_hash() {
3142        use std::collections::hash_map::DefaultHasher;
3143        let hash = |config: &ConsumerConfig| {
3144            let mut hasher = DefaultHasher::new();
3145            config.hash(&mut hasher);
3146            hasher.finish()
3147        };
3148        let a = ConsumerConfig::new(&props_with(&[(ConsumerConfig::CLIENT_ID_CONFIG, "c")])).unwrap();
3149        let b = ConsumerConfig::new(&props_with(&[(ConsumerConfig::CLIENT_ID_CONFIG, "c")])).unwrap();
3150        let c = ConsumerConfig::new(&props_with(&[(ConsumerConfig::CLIENT_ID_CONFIG, "d")])).unwrap();
3151        assert_eq!(a, b);
3152        assert_eq!(hash(&a), hash(&b));
3153        assert_ne!(a, c);
3154    }
3155}