Implementation:Fede1024 Rust rdkafka MockCluster Bootstrap Servers
| Knowledge Sources | |
|---|---|
| Domains | Testing, Configuration |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete bootstrap server retrieval and client configuration for mock cluster testing provided by rust-rdkafka.
Description
MockCluster::bootstrap_servers returns a comma-separated string of localhost:port addresses for the mock brokers. It calls rd_kafka_mock_cluster_bootstraps via FFI and converts the C string to a Rust String.
This string is used directly with ClientConfig::set("bootstrap.servers", ...) to create standard Kafka clients that connect to the mock cluster.
Usage
Call bootstrap_servers() on a MockCluster and pass the result to client configuration.
Code Reference
Source Location
- Repository: rust-rdkafka
- File: src/mocking.rs
- Lines: L139-143
Signature
impl<'c, C: ClientContext> MockCluster<'c, C> {
pub fn bootstrap_servers(&self) -> String;
}
Import
use rdkafka::mocking::MockCluster;
use rdkafka::config::ClientConfig;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| &self | &MockCluster | Yes | The mock cluster to get bootstrap servers from |
Outputs
| Name | Type | Description |
|---|---|---|
| returns | String | Comma-separated localhost:port addresses |
Usage Examples
Configure Clients for Mock Cluster
use rdkafka::mocking::MockCluster;
use rdkafka::config::ClientConfig;
use rdkafka::producer::FutureProducer;
use rdkafka::consumer::StreamConsumer;
let mock_cluster = MockCluster::new(1).expect("mock cluster failed");
mock_cluster.create_topic("test", 1, 1).expect("topic failed");
let bootstrap = mock_cluster.bootstrap_servers();
let producer: FutureProducer = ClientConfig::new()
.set("bootstrap.servers", &bootstrap)
.create()
.expect("producer failed");
let consumer: StreamConsumer = ClientConfig::new()
.set("bootstrap.servers", &bootstrap)
.set("group.id", "test-group")
.create()
.expect("consumer failed");