mockforge_kafka/lib.rs
1//! # MockForge Kafka
2//!
3//! Kafka protocol support for MockForge.
4//!
5//! This crate provides Kafka-specific functionality for creating mock Kafka brokers,
6//! including topic management, partition handling, consumer groups, and fixture-driven message generation.
7//!
8//! ## Overview
9//!
10//! MockForge Kafka enables you to:
11//!
12//! - **Mock Kafka brokers**: Simulate Apache Kafka brokers for testing
13//! - **Topic and partition management**: Create and manage topics with configurable partitions
14//! - **Producer/consumer simulation**: Handle produce and fetch requests
15//! - **Consumer group coordination**: Simulate consumer group behavior and rebalancing
16//! - **Fixture-based messaging**: Generate messages using templates and patterns
17//! - **Auto-produce functionality**: Automatically generate messages at specified rates
18//!
19//! ## Quick Start
20//!
21//! ### Basic Kafka Broker
22//!
23//! ```rust,no_run
24//! use mockforge_kafka::KafkaMockBroker;
25//! use mockforge_core::config::KafkaConfig;
26//!
27//! #[tokio::main]
28//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
29//! let config = KafkaConfig::default();
30//! let broker = KafkaMockBroker::new(config).await?;
31//!
32//! broker.start().await?;
33//! Ok(())
34//! }
35//! ```
36//!
37//! ## Key Features
38//!
39//! ### Broker Simulation
40//! - **10+ Kafka APIs supported**: Produce, Fetch, Metadata, ListGroups, DescribeGroups, ApiVersions, CreateTopics, DeleteTopics, DescribeConfigs
41//! - **Protocol-compliant responses**: Full Kafka protocol implementation without external dependencies
42//! - **Connection handling**: TCP-based broker connections with proper error handling
43//! - **Topic and partition management**: Dynamic topic/partition creation and management
44//!
45//! ### Metrics and Monitoring
46//! - **Comprehensive metrics**: Request counts, error rates, connection tracking
47//! - **Prometheus integration**: Export metrics in Prometheus format
48//! - **Real-time monitoring**: Live metrics collection during broker operation
49//!
50//! ### Fixture System
51//! - **YAML-based fixtures**: Define message templates and auto-production rules
52//! - **Template engine integration**: Use MockForge's templating system for dynamic content
53//! - **Auto-produce functionality**: Automatically generate messages at specified rates
54//! - **Key and value templating**: Flexible message generation with context variables
55//!
56//! ### Consumer Group Management
57//! - **Group coordination**: Simulate consumer group joins and synchronization
58//! - **Partition assignment**: Automatic partition distribution among consumers
59//! - **Offset management**: Track and manage consumer offsets
60//! - **Rebalancing simulation**: Test consumer group rebalancing scenarios
61//!
62//! ### Testing Features
63//! - **Protocol validation**: Ensure requests conform to Kafka protocol specifications
64//! - **Error simulation**: Configurable error responses for testing error handling
65//! - **Performance testing**: Built-in benchmarking support
66//! - **Integration testing**: Compatible with standard Kafka client libraries
67//!
68//! ## Supported Kafka APIs
69//!
70//! - **Produce** (API Key 0): Message production with acknowledgments
71//! - **Fetch** (API Key 1): Message consumption with offset management
72//! - **Metadata** (API Key 3): Topic and broker metadata discovery
73//! - **ListGroups** (API Key 9): Consumer group listing
74//! - **DescribeGroups** (API Key 15): Consumer group details and member information
75//! - **ApiVersions** (API Key 18): Protocol version negotiation
76//! - **CreateTopics** (API Key 19): Dynamic topic creation
77//! - **DeleteTopics** (API Key 20): Topic deletion
78//! - **DescribeConfigs** (API Key 32): Configuration retrieval
79//!
80//! ## Example Usage
81//!
82//! ### Basic Broker Setup
83//!
84//! ```rust,no_run
85//! use mockforge_kafka::KafkaMockBroker;
86//! use mockforge_core::config::KafkaConfig;
87//!
88//! #[tokio::main]
89//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
90//! let config = KafkaConfig {
91//! port: 9092,
92//! ..Default::default()
93//! };
94//!
95//! let broker = KafkaMockBroker::new(config).await?;
96//! broker.start().await?;
97//!
98//! Ok(())
99//! }
100//! ```
101//!
102//! ### Metrics Export
103//!
104//! ```rust,no_run
105//! use mockforge_kafka::MetricsExporter;
106//! use mockforge_kafka::KafkaMetrics;
107//! use std::sync::Arc;
108//!
109//! #[tokio::main]
110//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
111//! let metrics = Arc::new(KafkaMetrics::default());
112//! let exporter = MetricsExporter::new(metrics.clone());
113//!
114//! // Export metrics in Prometheus format
115//! let snapshot = exporter.export_prometheus();
116//! println!("{}", snapshot);
117//!
118//! Ok(())
119//! }
120//! ```
121//!
122//! ## Related Crates
123//!
124//! - [`mockforge-core`](https://docs.rs/mockforge-core): Core mocking functionality and configuration
125//! - [`rdkafka`](https://docs.rs/rdkafka): Kafka client library for testing integration
126
127pub mod broker;
128pub mod consumer_groups;
129pub mod fixtures;
130pub mod metrics;
131pub mod partitions;
132pub mod protocol;
133pub mod spec_registry;
134pub mod topics;
135
136// Re-export main types
137pub use broker::KafkaMockBroker;
138pub use consumer_groups::{ConsumerGroup, ConsumerGroupManager};
139pub use fixtures::{AutoProduceConfig, KafkaFixture};
140pub use metrics::{KafkaMetrics, MetricsExporter, MetricsSnapshot};
141pub use partitions::{KafkaMessage, Partition};
142pub use spec_registry::KafkaSpecRegistry;
143pub use topics::{Topic, TopicConfig};