kafkit-client 0.1.4

Kafka 4.0+ pure Rust client.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//! Builder-style entry points for the public client API.
//!
//! ```no_run
//! # async fn example() -> kafkit_client::Result<()> {
//! use kafkit_client::KafkaClient;
//!
//! let client = KafkaClient::new("localhost:9092");
//! let producer = client.topic("orders").producer().connect().await?;
//! producer.send_value("created").await?;
//! producer.shutdown().await?;
//! # Ok(())
//! # }
//! ```
//!
use std::sync::Arc;
use std::time::Duration;

use anyhow::anyhow;

use crate::admin::KafkaAdmin;
use crate::consumer::KafkaConsumer;
use crate::network::TcpConnector;
use crate::producer::KafkaProducer;
use crate::{
    AdminConfig, AutoOffsetReset, ConsumerConfig, IsolationLevel, ProducerCompression,
    ProducerConfig, Result, SaslConfig, SecurityProtocol, TlsConfig,
};
use crate::{ConsumerRebalanceEvent, ConsumerRebalanceListener};

const DEFAULT_POLL_TIMEOUT: Duration = Duration::from_secs(1);

#[derive(Debug, Clone)]
/// A small builder facade around one or more bootstrap servers.
pub struct KafkaClient {
    bootstrap_servers: Vec<String>,
}

impl KafkaClient {
    /// Creates a client facade from one bootstrap server.
    pub fn new(bootstrap_server: impl Into<String>) -> Self {
        Self {
            bootstrap_servers: vec![bootstrap_server.into()],
        }
    }

    /// Sets bootstrap servers and returns the updated value.
    pub fn with_bootstrap_servers(
        mut self,
        servers: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.bootstrap_servers = servers.into_iter().map(Into::into).collect();
        self
    }

    /// Returns a topic-scoped builder facade.
    pub fn topic(&self, topic: impl Into<String>) -> KafkaTopic {
        KafkaTopic {
            bootstrap_servers: self.bootstrap_servers.clone(),
            topic: topic.into(),
        }
    }

    /// Starts building a producer.
    pub fn producer(&self) -> ProducerBuilder {
        ProducerBuilder::from_servers(self.bootstrap_servers.clone())
    }

    /// Starts building an admin client.
    pub fn admin(&self) -> AdminBuilder {
        AdminBuilder::from_servers(self.bootstrap_servers.clone())
    }

    /// Starts building a group consumer.
    pub fn consumer(&self, group_id: impl Into<String>) -> ConsumerBuilder {
        ConsumerBuilder::from_servers(self.bootstrap_servers.clone(), group_id)
    }
}

#[derive(Debug, Clone)]
/// A topic-scoped facade for producers and consumers.
pub struct KafkaTopic {
    bootstrap_servers: Vec<String>,
    topic: String,
}

impl KafkaTopic {
    /// Starts building a producer with this topic as the default.
    pub fn producer(&self) -> ProducerBuilder {
        ProducerBuilder::from_servers(self.bootstrap_servers.clone())
            .with_default_topic(self.topic.clone())
    }

    /// Starts building a consumer already subscribed to this topic.
    pub fn consumer(&self, group_id: impl Into<String>) -> ConsumerBuilder {
        ConsumerBuilder::from_servers(self.bootstrap_servers.clone(), group_id)
            .with_topic(self.topic.clone())
    }
}

#[derive(Debug, Clone)]
/// Builder for a [`KafkaProducer`].
pub struct ProducerBuilder {
    config: ProducerConfig,
    default_topic: Option<String>,
    default_partition: Option<i32>,
}

impl ProducerBuilder {
    fn new(bootstrap_server: impl Into<String>) -> Self {
        Self {
            config: ProducerConfig::new(bootstrap_server),
            default_topic: None,
            default_partition: None,
        }
    }

    fn from_servers(servers: Vec<String>) -> Self {
        let mut builder = Self::new(servers.first().cloned().unwrap_or_default());
        builder.config = builder.config.with_bootstrap_servers(servers);
        builder
    }

    /// Sets bootstrap servers and returns the updated value.
    pub fn with_bootstrap_servers(
        mut self,
        servers: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.config = self.config.with_bootstrap_servers(servers);
        self
    }

    /// Sets client id and returns the updated value.
    pub fn with_client_id(mut self, client_id: impl Into<String>) -> Self {
        self.config = self.config.with_client_id(client_id);
        self
    }

    /// Sets security protocol and returns the updated value.
    pub fn with_security_protocol(mut self, security_protocol: SecurityProtocol) -> Self {
        self.config = self.config.with_security_protocol(security_protocol);
        self
    }

    /// Sets tls and returns the updated value.
    pub fn with_tls(mut self, tls: TlsConfig) -> Self {
        self.config = self.config.with_tls(tls);
        self
    }

    /// Sets sasl and returns the updated value.
    pub fn with_sasl(mut self, sasl: SaslConfig) -> Self {
        self.config = self.config.with_sasl(sasl);
        self
    }

    /// Sets sasl plain and returns the updated value.
    pub fn with_sasl_plain(
        mut self,
        username: impl Into<String>,
        password: impl Into<String>,
    ) -> Self {
        self.config = self.config.with_sasl_plain(username, password);
        self
    }

    /// Sets sasl scram sha 256 and returns the updated value.
    pub fn with_sasl_scram_sha_256(
        mut self,
        username: impl Into<String>,
        password: impl Into<String>,
    ) -> Self {
        self.config = self.config.with_sasl_scram_sha_256(username, password);
        self
    }

    /// Sets sasl scram sha 512 and returns the updated value.
    pub fn with_sasl_scram_sha_512(
        mut self,
        username: impl Into<String>,
        password: impl Into<String>,
    ) -> Self {
        self.config = self.config.with_sasl_scram_sha_512(username, password);
        self
    }

    /// Sets compression and returns the updated value.
    pub fn with_compression(mut self, compression: ProducerCompression) -> Self {
        self.config = self.config.with_compression(compression);
        self
    }

    /// Sets default topic and returns the updated value.
    pub fn with_default_topic(mut self, topic: impl Into<String>) -> Self {
        self.default_topic = Some(topic.into());
        self
    }

    /// Sets default partition and returns the updated value.
    pub fn with_default_partition(mut self, partition: i32) -> Self {
        self.default_partition = Some(partition);
        self
    }

    /// Sets enable idempotence and returns the updated value.
    pub fn with_enable_idempotence(mut self, enable_idempotence: bool) -> Self {
        self.config = self.config.with_enable_idempotence(enable_idempotence);
        self
    }

    /// Sets batch size and returns the updated value.
    pub fn with_batch_size(mut self, batch_size: usize) -> Self {
        self.config = self.config.with_batch_size(batch_size);
        self
    }

    /// Sets linger and returns the updated value.
    pub fn with_linger(mut self, linger: Duration) -> Self {
        self.config = self.config.with_linger(linger);
        self
    }

    /// Sets delivery timeout and returns the updated value.
    pub fn with_delivery_timeout(mut self, delivery_timeout: Duration) -> Self {
        self.config = self.config.with_delivery_timeout(delivery_timeout);
        self
    }

    /// Sets request timeout and returns the updated value.
    pub fn with_request_timeout(mut self, request_timeout: Duration) -> Self {
        self.config = self.config.with_request_timeout(request_timeout);
        self
    }

    /// Sets retry backoff and returns the updated value.
    pub fn with_retry_backoff(mut self, retry_backoff: Duration) -> Self {
        self.config = self.config.with_retry_backoff(retry_backoff);
        self
    }

    /// Sets max retries and returns the updated value.
    pub fn with_max_retries(mut self, max_retries: usize) -> Self {
        self.config = self.config.with_max_retries(max_retries);
        self
    }

    /// Sets max in-flight requests per broker connection and returns the updated value.
    pub fn with_max_in_flight_requests_per_connection(mut self, max_in_flight: usize) -> Self {
        self.config = self
            .config
            .with_max_in_flight_requests_per_connection(max_in_flight);
        self
    }

    /// Sets transactional id and returns the updated value.
    pub fn with_transactional_id(mut self, transactional_id: impl Into<String>) -> Self {
        self.config = self.config.with_transactional_id(transactional_id);
        self
    }

    /// Sets TCP connector and returns the updated value.
    pub fn with_tcp_connector(mut self, tcp_connector: Arc<dyn TcpConnector>) -> Self {
        self.config = self.config.with_tcp_connector(tcp_connector);
        self
    }

    /// Connects and returns a producer.
    pub async fn connect(self) -> Result<KafkaProducer> {
        let producer = KafkaProducer::connect(self.config).await?;
        Ok(producer.with_defaults(self.default_topic, self.default_partition))
    }
}

#[derive(Debug, Clone)]
/// Builder for a [`KafkaAdmin`].
pub struct AdminBuilder {
    config: AdminConfig,
}

impl AdminBuilder {
    fn new(bootstrap_server: impl Into<String>) -> Self {
        Self {
            config: AdminConfig::new(bootstrap_server),
        }
    }

    fn from_servers(servers: Vec<String>) -> Self {
        let mut builder = Self::new(servers.first().cloned().unwrap_or_default());
        builder.config = builder.config.with_bootstrap_servers(servers);
        builder
    }

    /// Sets bootstrap servers and returns the updated value.
    pub fn with_bootstrap_servers(
        mut self,
        servers: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.config = self.config.with_bootstrap_servers(servers);
        self
    }

    /// Sets client id and returns the updated value.
    pub fn with_client_id(mut self, client_id: impl Into<String>) -> Self {
        self.config = self.config.with_client_id(client_id);
        self
    }

    /// Sets request timeout and returns the updated value.
    pub fn with_request_timeout(mut self, request_timeout: Duration) -> Self {
        self.config = self.config.with_request_timeout(request_timeout);
        self
    }

    /// Sets security protocol and returns the updated value.
    pub fn with_security_protocol(mut self, security_protocol: SecurityProtocol) -> Self {
        self.config = self.config.with_security_protocol(security_protocol);
        self
    }

    /// Sets tls and returns the updated value.
    pub fn with_tls(mut self, tls: TlsConfig) -> Self {
        self.config = self.config.with_tls(tls);
        self
    }

    /// Sets sasl and returns the updated value.
    pub fn with_sasl(mut self, sasl: SaslConfig) -> Self {
        self.config = self.config.with_sasl(sasl);
        self
    }

    /// Sets sasl plain and returns the updated value.
    pub fn with_sasl_plain(
        mut self,
        username: impl Into<String>,
        password: impl Into<String>,
    ) -> Self {
        self.config = self.config.with_sasl_plain(username, password);
        self
    }

    /// Sets sasl scram sha 256 and returns the updated value.
    pub fn with_sasl_scram_sha_256(
        mut self,
        username: impl Into<String>,
        password: impl Into<String>,
    ) -> Self {
        self.config = self.config.with_sasl_scram_sha_256(username, password);
        self
    }

    /// Sets sasl scram sha 512 and returns the updated value.
    pub fn with_sasl_scram_sha_512(
        mut self,
        username: impl Into<String>,
        password: impl Into<String>,
    ) -> Self {
        self.config = self.config.with_sasl_scram_sha_512(username, password);
        self
    }

    /// Sets TCP connector and returns the updated value.
    pub fn with_tcp_connector(mut self, tcp_connector: Arc<dyn TcpConnector>) -> Self {
        self.config = self.config.with_tcp_connector(tcp_connector);
        self
    }

    /// Connects and returns an admin client.
    pub async fn connect(self) -> Result<KafkaAdmin> {
        KafkaAdmin::connect(self.config).await
    }
}

#[derive(Debug, Clone)]
/// Builder for a [`KafkaConsumer`].
pub struct ConsumerBuilder {
    config: ConsumerConfig,
    topics: Vec<String>,
    poll_timeout: Duration,
}

impl ConsumerBuilder {
    fn new(bootstrap_server: impl Into<String>, group_id: impl Into<String>) -> Self {
        Self {
            config: ConsumerConfig::new(bootstrap_server, group_id),
            topics: Vec::new(),
            poll_timeout: DEFAULT_POLL_TIMEOUT,
        }
    }

    fn from_servers(servers: Vec<String>, group_id: impl Into<String>) -> Self {
        let first = servers.first().cloned().unwrap_or_default();
        let mut builder = Self::new(first, group_id);
        builder.config = builder.config.with_bootstrap_servers(servers);
        builder
    }

    /// Sets bootstrap servers and returns the updated value.
    pub fn with_bootstrap_servers(
        mut self,
        servers: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.config = self.config.with_bootstrap_servers(servers);
        self
    }

    /// Sets client id and returns the updated value.
    pub fn with_client_id(mut self, client_id: impl Into<String>) -> Self {
        self.config = self.config.with_client_id(client_id);
        self
    }

    /// Sets security protocol and returns the updated value.
    pub fn with_security_protocol(mut self, security_protocol: SecurityProtocol) -> Self {
        self.config = self.config.with_security_protocol(security_protocol);
        self
    }

    /// Sets tls and returns the updated value.
    pub fn with_tls(mut self, tls: TlsConfig) -> Self {
        self.config = self.config.with_tls(tls);
        self
    }

    /// Sets sasl and returns the updated value.
    pub fn with_sasl(mut self, sasl: SaslConfig) -> Self {
        self.config = self.config.with_sasl(sasl);
        self
    }

    /// Sets sasl plain and returns the updated value.
    pub fn with_sasl_plain(
        mut self,
        username: impl Into<String>,
        password: impl Into<String>,
    ) -> Self {
        self.config = self.config.with_sasl_plain(username, password);
        self
    }

    /// Sets sasl scram sha 256 and returns the updated value.
    pub fn with_sasl_scram_sha_256(
        mut self,
        username: impl Into<String>,
        password: impl Into<String>,
    ) -> Self {
        self.config = self.config.with_sasl_scram_sha_256(username, password);
        self
    }

    /// Sets sasl scram sha 512 and returns the updated value.
    pub fn with_sasl_scram_sha_512(
        mut self,
        username: impl Into<String>,
        password: impl Into<String>,
    ) -> Self {
        self.config = self.config.with_sasl_scram_sha_512(username, password);
        self
    }

    /// Sets topic and returns the updated value.
    pub fn with_topic(mut self, topic: impl Into<String>) -> Self {
        self.topics.push(topic.into());
        self
    }

    /// Sets topics and returns the updated value.
    pub fn with_topics<I, S>(mut self, topics: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.topics.extend(topics.into_iter().map(Into::into));
        self
    }

    /// Sets auto offset reset and returns the updated value.
    pub fn with_auto_offset_reset(mut self, auto_offset_reset: AutoOffsetReset) -> Self {
        self.config = self.config.with_auto_offset_reset(auto_offset_reset);
        self
    }

    /// Sets isolation level and returns the updated value.
    pub fn with_isolation_level(mut self, isolation_level: IsolationLevel) -> Self {
        self.config = self.config.with_isolation_level(isolation_level);
        self
    }

    /// Sets auto commit and returns the updated value.
    pub fn with_auto_commit(mut self, enable_auto_commit: bool) -> Self {
        self.config = self.config.with_enable_auto_commit(enable_auto_commit);
        self
    }

    /// Sets request timeout and returns the updated value.
    pub fn with_request_timeout(mut self, request_timeout: Duration) -> Self {
        self.config = self.config.with_request_timeout(request_timeout);
        self
    }

    /// Sets retry backoff and returns the updated value.
    pub fn with_retry_backoff(mut self, retry_backoff: Duration) -> Self {
        self.config = self.config.with_retry_backoff(retry_backoff);
        self
    }

    /// Sets max retries and returns the updated value.
    pub fn with_max_retries(mut self, max_retries: usize) -> Self {
        self.config = self.config.with_max_retries(max_retries);
        self
    }

    /// Sets instance id and returns the updated value.
    pub fn with_instance_id(mut self, instance_id: impl Into<String>) -> Self {
        self.config = self.config.with_instance_id(instance_id);
        self
    }

    /// Sets rebalance listener and returns the updated value.
    pub fn with_rebalance_listener(mut self, listener: ConsumerRebalanceListener) -> Self {
        self.config = self.config.with_rebalance_listener(listener);
        self
    }

    /// Sets rebalance callback and returns the updated value.
    pub fn with_rebalance_callback(
        mut self,
        callback: impl Fn(ConsumerRebalanceEvent) + Send + Sync + 'static,
    ) -> Self {
        self.config = self.config.with_rebalance_callback(callback);
        self
    }

    /// Sets TCP connector and returns the updated value.
    pub fn with_tcp_connector(mut self, tcp_connector: Arc<dyn TcpConnector>) -> Self {
        self.config = self.config.with_tcp_connector(tcp_connector);
        self
    }

    /// Sets poll timeout and returns the updated value.
    pub fn with_poll_timeout(mut self, poll_timeout: Duration) -> Self {
        self.poll_timeout = poll_timeout;
        self
    }

    /// Connects and returns a consumer.
    pub async fn connect(self) -> Result<KafkaConsumer> {
        let topics = self
            .topics
            .into_iter()
            .map(validate_topic_name)
            .collect::<Result<Vec<_>>>()?;
        let consumer = KafkaConsumer::connect(self.config).await?;
        if !topics.is_empty()
            && let Err(error) = consumer.subscribe(topics).await
        {
            let _ = consumer.shutdown().await;
            return Err(error);
        }
        Ok(consumer.with_default_poll_timeout(self.poll_timeout))
    }
}

fn validate_topic_name(topic: String) -> Result<String> {
    let topic = topic.trim();
    if topic.is_empty() {
        return Err(anyhow!("topic must be non-empty").into());
    }
    Ok(topic.to_owned())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::SaslMechanism;

    #[test]
    fn client_builders_preserve_bootstrap_servers_and_topic_defaults() {
        let client =
            KafkaClient::new("host-a:9092").with_bootstrap_servers(["host-a:9092", "host-b:9092"]);

        let producer = client
            .topic("orders")
            .producer()
            .with_client_id("producer-a")
            .with_default_partition(2)
            .with_compression(ProducerCompression::Lz4)
            .with_batch_size(32)
            .with_linger(Duration::from_millis(10))
            .with_delivery_timeout(Duration::from_secs(3))
            .with_transactional_id("tx-a");

        assert_eq!(
            producer.config.bootstrap_servers,
            vec!["host-a:9092", "host-b:9092"]
        );
        assert_eq!(producer.config.client_id, "producer-a");
        assert_eq!(producer.config.compression, ProducerCompression::Lz4);
        assert_eq!(producer.config.batch_size, 32);
        assert_eq!(producer.config.linger, Duration::from_millis(10));
        assert_eq!(producer.config.delivery_timeout, Duration::from_secs(3));
        assert_eq!(producer.config.transactional_id.as_deref(), Some("tx-a"));
        assert_eq!(producer.default_topic.as_deref(), Some("orders"));
        assert_eq!(producer.default_partition, Some(2));
    }

    #[test]
    fn admin_builder_forwards_security_and_timeout_options() {
        let builder = KafkaClient::new("host-a:9092")
            .admin()
            .with_bootstrap_servers(["host-b:9092", "host-c:9092"])
            .with_client_id("admin-a")
            .with_request_timeout(Duration::from_secs(9))
            .with_security_protocol(SecurityProtocol::Ssl)
            .with_tls(TlsConfig::new().with_server_name("kafka.internal"))
            .with_sasl_scram_sha_512("user-a", "secret-a");

        assert_eq!(
            builder.config.bootstrap_servers,
            vec!["host-b:9092", "host-c:9092"]
        );
        assert_eq!(builder.config.client_id, "admin-a");
        assert_eq!(builder.config.request_timeout, Duration::from_secs(9));
        assert_eq!(builder.config.security_protocol, SecurityProtocol::SaslSsl);
        assert_eq!(
            builder.config.tls.server_name.as_deref(),
            Some("kafka.internal")
        );
        assert_eq!(builder.config.sasl.mechanism, SaslMechanism::ScramSha512);
    }

    #[test]
    fn consumer_builder_collects_topics_and_group_options() {
        let builder = KafkaClient::new("host-a:9092")
            .topic("orders")
            .consumer("group-a")
            .with_bootstrap_servers(["host-b:9092"])
            .with_client_id("consumer-a")
            .with_topic("payments")
            .with_topics(["shipments", "invoices"])
            .with_auto_offset_reset(AutoOffsetReset::Latest)
            .with_isolation_level(IsolationLevel::ReadCommitted)
            .with_auto_commit(true)
            .with_instance_id("instance-a")
            .with_poll_timeout(Duration::from_millis(250))
            .with_sasl_plain("user-a", "secret-a");

        assert_eq!(builder.config.bootstrap_servers, vec!["host-b:9092"]);
        assert_eq!(builder.config.client_id, "consumer-a");
        assert_eq!(builder.config.group_id, "group-a");
        assert_eq!(
            builder.topics,
            vec!["orders", "payments", "shipments", "invoices"]
        );
        assert_eq!(builder.config.auto_offset_reset, AutoOffsetReset::Latest);
        assert_eq!(
            builder.config.isolation_level,
            IsolationLevel::ReadCommitted
        );
        assert!(builder.config.enable_auto_commit);
        assert_eq!(builder.config.instance_id.as_deref(), Some("instance-a"));
        assert_eq!(builder.poll_timeout, Duration::from_millis(250));
        assert_eq!(
            builder.config.security_protocol,
            SecurityProtocol::SaslPlaintext
        );
    }

    #[test]
    fn validate_topic_name_trims_and_rejects_empty_names() {
        assert_eq!(
            validate_topic_name("  topic-a  ".to_owned()).unwrap(),
            "topic-a"
        );
        assert!(validate_topic_name("   ".to_owned()).is_err());
    }
}