Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Fede1024 Rust rdkafka MockCluster New

From Leeroopedia


Knowledge Sources
Domains Testing, Messaging
Last Updated 2026-02-07 19:00 GMT

Overview

Concrete mock Kafka cluster constructor provided by rust-rdkafka's mocking module.

Description

MockCluster::new creates an in-process mock Kafka cluster with the specified number of brokers. Internally, it creates a Client with a default producer context, then calls rd_kafka_mock_cluster_new via FFI to instantiate the mock cluster. Each mock broker listens on a localhost port.

The MockCluster struct owns the underlying client and mock cluster pointer. When dropped, the mock cluster and all its resources are cleaned up.

Usage

Call MockCluster::new(broker_count) at the start of a test to create a mock cluster. Then use create_topic() and bootstrap_servers() to set up topics and configure clients.

Code Reference

Source Location

  • Repository: rust-rdkafka
  • File: src/mocking.rs
  • Lines: L94-119

Signature

impl MockCluster<'static, DefaultProducerContext> {
    pub fn new(broker_count: i32) -> KafkaResult<Self>;
}

Import

use rdkafka::mocking::MockCluster;

I/O Contract

Inputs

Name Type Required Description
broker_count i32 Yes Number of simulated brokers in the mock cluster

Outputs

Name Type Description
returns KafkaResult<MockCluster> In-process mock Kafka cluster or error

Usage Examples

Create Mock Cluster for Testing

use rdkafka::mocking::MockCluster;

let mock_cluster = MockCluster::new(3)
    .expect("Failed to create mock cluster");

// Create a topic
mock_cluster
    .create_topic("test-topic", 1, 1)
    .expect("Failed to create topic");

// Get bootstrap servers for client configuration
let bootstrap = mock_cluster.bootstrap_servers();
println!("Mock cluster at: {}", bootstrap);

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment