KafkaMockBroker

Struct KafkaMockBroker 

Source
pub struct KafkaMockBroker {
    pub topics: Arc<RwLock<HashMap<String, Topic>>>,
    pub consumer_groups: Arc<RwLock<ConsumerGroupManager>>,
    /* private fields */
}
Expand description

Mock Kafka broker implementation

The KafkaMockBroker simulates a complete Apache Kafka broker, handling TCP connections and responding to Kafka protocol requests. It supports multiple concurrent connections and provides comprehensive metrics collection.

§Architecture

The broker maintains several key components:

  • Topics: Managed topic and partition storage
  • Consumer Groups: Consumer group coordination and partition assignment
  • Spec Registry: Fixture-based request/response handling
  • Metrics: Real-time performance and usage statistics

§Supported Operations

  • Produce: Message production with acknowledgments
  • Fetch: Message consumption with offset tracking
  • Metadata: Topic and broker discovery
  • ListGroups/DescribeGroups: Consumer group management
  • ApiVersions: Protocol version negotiation
  • CreateTopics/DeleteTopics: Dynamic topic management
  • DescribeConfigs: Configuration retrieval

§Example

use mockforge_kafka::KafkaMockBroker;
use mockforge_core::config::KafkaConfig;

let config = KafkaConfig {
    port: 9092,
    ..Default::default()
};

let broker = KafkaMockBroker::new(config).await?;
broker.start().await?;

Fields§

§topics: Arc<RwLock<HashMap<String, Topic>>>

Topic storage with thread-safe access

§consumer_groups: Arc<RwLock<ConsumerGroupManager>>

Consumer group manager

Implementations§

Source§

impl KafkaMockBroker

Source

pub async fn new(config: KafkaConfig) -> Result<Self>

Create a new Kafka mock broker

Initializes the broker with the provided configuration, setting up internal data structures for topics, consumer groups, and metrics.

§Arguments
  • config - Kafka broker configuration including port, timeouts, and fixture paths
§Returns

Returns a Result containing the initialized broker or an error if initialization fails.

§Example
use mockforge_kafka::KafkaMockBroker;
use mockforge_core::config::KafkaConfig;

let config = KafkaConfig::default();
let broker = KafkaMockBroker::new(config).await?;
Source

pub async fn start(&self) -> Result<()>

Start the Kafka broker server

Binds to the configured host and port, then begins accepting TCP connections. Each connection is handled in a separate async task, allowing concurrent client connections.

The broker will run indefinitely until the task is cancelled or an error occurs.

§Returns

Returns a Result that indicates whether the broker started successfully. The method only returns on error, as it runs an infinite accept loop.

§Example
use mockforge_kafka::KafkaMockBroker;
use mockforge_core::config::KafkaConfig;

let config = KafkaConfig::default();
let broker = KafkaMockBroker::new(config).await?;

// Start the broker (this will run indefinitely)
broker.start().await?;
Source

pub async fn test_commit_offsets( &self, group_id: &str, offsets: HashMap<(String, i32), i64>, ) -> Result<()>

Test helper: Commit offsets for a consumer group (only available in tests)

Source

pub async fn test_get_committed_offsets( &self, group_id: &str, ) -> HashMap<(String, i32), i64>

Test helper: Get committed offsets for a consumer group (only available in tests)

Source

pub async fn test_create_topic(&self, name: &str, config: TopicConfig)

Test helper: Create a topic (only available in tests)

Source

pub async fn test_join_group( &self, group_id: &str, member_id: &str, client_id: &str, ) -> Result<()>

Test helper: Join a consumer group (only available in tests)

Source

pub async fn test_sync_group( &self, group_id: &str, assignments: Vec<PartitionAssignment>, ) -> Result<()>

Test helper: Sync group assignment (only available in tests)

Source

pub async fn test_get_assignments( &self, group_id: &str, member_id: &str, ) -> Vec<PartitionAssignment>

Test helper: Get group member assignments (only available in tests)

Source

pub async fn test_simulate_lag( &self, group_id: &str, topic: &str, lag: i64, ) -> Result<()>

Test helper: Simulate consumer lag (only available in tests)

Source

pub async fn test_reset_offsets( &self, group_id: &str, topic: &str, to: &str, ) -> Result<()>

Test helper: Reset consumer offsets (only available in tests)

Trait Implementations§

Source§

impl Clone for KafkaMockBroker

Source§

fn clone(&self) -> KafkaMockBroker

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ParallelSend for T