Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.3 KB
Raw Blame
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;

use async_trait::async_trait;

use super::batch_context::{ConsumerCommand, ConsumerCommandSender};
use super::rebalance_handler::RebalanceHandler;
use super::types::Partition;

use anyhow::Result;
use rdkafka::TopicPartitionList;

/// Test implementation of RebalanceHandler that tracks calls
#[derive(Default)]
pub struct TestRebalanceHandler {
    pub assigned_count: AtomicUsize,
    pub revoked_count: AtomicUsize,
    pub pre_rebalance_count: AtomicUsize,
    pub post_rebalance_count: AtomicUsize,
    pub assigned_partitions: Mutex<Vec<Partition>>,
    pub revoked_partitions: Mutex<Vec<Partition>>,
}

#[async_trait]
impl RebalanceHandler for TestRebalanceHandler {
    fn setup_assigned_partitions(&self, partitions: &TopicPartitionList) {
        self.assigned_count.fetch_add(1, Ordering::SeqCst);

        let mut assigned = self.assigned_partitions.lock().unwrap();
        for elem in partitions.elements() {
            assigned.push(Partition::from(elem));
        }
    }

    fn setup_revoked_partitions(&self, partitions: &TopicPartitionList) {
        self.revoked_count.fetch_add(1, Ordering::SeqCst);

        let mut revoked = self.revoked_partitions.lock().unwrap();
        for elem in partitions.elements() {
            revoked.push(Partition::from(elem));
        }
    }

    async fn async_setup_assigned_partitions(
        &self,
        consumer_command_tx: &ConsumerCommandSender,
    ) -> Result<()> {
        // Send Resume command to unblock paused partitions (required for tests to work)
        // Build TPL from tracked assigned partitions
        let mut tpl = TopicPartitionList::new();
        let assigned = self.assigned_partitions.lock().unwrap();
        for partition in assigned.iter() {
            tpl.add_partition(partition.topic(), partition.partition_number());
        }
        let _ = consumer_command_tx.send(ConsumerCommand::Resume(tpl));
        Ok(())
    }

    async fn cleanup_revoked_partitions(&self, _partitions: &TopicPartitionList) -> Result<()> {
        Ok(())
    }

    async fn on_pre_rebalance(&self) -> Result<()> {
        self.pre_rebalance_count.fetch_add(1, Ordering::SeqCst);
        Ok(())
    }

    async fn on_post_rebalance(&self) -> Result<()> {
        self.post_rebalance_count.fetch_add(1, Ordering::SeqCst);
        Ok(())
    }
}