kafka-client-engine 0.0.1

Execution and driver-integration layer for the native Kafka client
Documentation
//! Bounded ownership tests for one partition transaction-abort host.

use std::sync::Arc;

use kafka_client_core::{AbortPartitionTransactionPlan, Moment};

use crate::{
    admin::{
        ABORT_PARTITION_TRANSACTION_CAPACITY, AbortPartitionTransactionAdmissionErrorKind,
        AbortPartitionTransactionDeliveryStatus, AbortPartitionTransactionFailureKind,
        AbortPartitionTransactionOutcome, AdminCompletionNotifier,
        abort_partition_transaction::host::ABORT_PARTITION_TRANSACTION_RETAINED_BYTES,
    },
    clock::{MonotonicClock, OperationDeadline},
};

use super::AbortPartitionTransactionHost;

fn deadline() -> OperationDeadline {
    Arc::new(MonotonicClock::new())
        .capture_deadline_after(std::time::Duration::from_secs(1))
        .unwrap_or_else(|error| panic!("deadline: {error:?}"))
        .operation_deadline()
}

fn plan(topic: &str) -> AbortPartitionTransactionPlan {
    AbortPartitionTransactionPlan::new(topic.to_owned(), 3, 41, 7, 11)
        .unwrap_or_else(|error| panic!("valid plan: {error:?}"))
}

#[test]
fn admission_reserves_capacity_and_retained_bytes() {
    let (mut notifier, ports) = AdminCompletionNotifier::start()
        .unwrap_or_else(|error| panic!("completion notifier: {error:?}"));
    let mut host = AbortPartitionTransactionHost::new(ports.abort_partition_transaction);

    let admission = host
        .try_admit(Moment::from_tick(0), deadline(), plan("orders"))
        .unwrap_or_else(|error| panic!("admit: {error:?}"));

    assert!(admission.fault.is_none());
    assert_eq!(host.unsettled(), 1);
    assert!(host.retained_bytes_for_test() > 0);
    assert!(host.retained_bytes_for_test() < ABORT_PARTITION_TRANSACTION_RETAINED_BYTES);
    drop(admission.observer);
    host.recover_after_driver_shutdown()
        .unwrap_or_else(|error| panic!("recover host: {error:?}"));
    drop(host);
    stop_notifier(&mut notifier);
}

#[test]
fn elapsed_at_start_publishes_not_sent_deadline_terminal() {
    let (mut notifier, ports) = AdminCompletionNotifier::start()
        .unwrap_or_else(|error| panic!("completion notifier: {error:?}"));
    let mut host = AbortPartitionTransactionHost::new(ports.abort_partition_transaction);
    let deadline = deadline();
    let now = Moment::from_tick(deadline.core().tick());

    let admission = host
        .try_admit(now, deadline, plan("orders"))
        .unwrap_or_else(|error| panic!("accepted terminal: {error:?}"));
    let outcome = admission
        .observer
        .wait()
        .unwrap_or_else(|error| panic!("observe: {error:?}"));
    let AbortPartitionTransactionOutcome::Failed(failure) = outcome else {
        panic!("expected deadline failure");
    };
    assert_eq!(
        failure.kind(),
        super::AbortPartitionTransactionFailureKind::DeadlineElapsed
    );
    assert_eq!(
        failure.delivery(),
        super::AbortPartitionTransactionDeliveryStatus::NotSent
    );
    let _turn = host
        .turn(now)
        .unwrap_or_else(|error| panic!("reclaim terminal: {error:?}"));
    drop(host);
    stop_notifier(&mut notifier);
}

#[test]
fn operation_count_is_bounded_at_sixteen() {
    let (mut notifier, ports) = AdminCompletionNotifier::start()
        .unwrap_or_else(|error| panic!("completion notifier: {error:?}"));
    let mut host = AbortPartitionTransactionHost::new(ports.abort_partition_transaction);
    let deadline = deadline();
    let mut observers = Vec::new();

    for producer_id in 0..ABORT_PARTITION_TRANSACTION_CAPACITY {
        let plan = AbortPartitionTransactionPlan::new(
            "orders".to_owned(),
            3,
            i64::try_from(producer_id)
                .unwrap_or_else(|error| panic!("bounded producer: {error:?}")),
            7,
            11,
        )
        .unwrap_or_else(|error| panic!("valid plan: {error:?}"));
        observers.push(
            host.try_admit(Moment::from_tick(0), deadline, plan)
                .unwrap_or_else(|error| panic!("bounded admission: {error:?}"))
                .observer,
        );
    }
    assert!(matches!(
        host.try_admit(Moment::from_tick(0), deadline, plan("overflow")),
        Err(AbortPartitionTransactionAdmissionErrorKind::Capacity)
    ));

    drop(observers);
    host.recover_after_driver_shutdown()
        .unwrap_or_else(|error| panic!("recover host: {error:?}"));
    drop(host);
    stop_notifier(&mut notifier);
}

#[test]
fn untouched_recovery_is_definitely_unsent() {
    let (mut notifier, ports) = AdminCompletionNotifier::start()
        .unwrap_or_else(|error| panic!("completion notifier: {error:?}"));
    let mut host = AbortPartitionTransactionHost::new(ports.abort_partition_transaction);
    let deadline = deadline();
    let admission = host
        .try_admit(Moment::from_tick(0), deadline, plan("orders"))
        .unwrap_or_else(|error| panic!("admit: {error:?}"));

    host.recover_after_driver_shutdown()
        .unwrap_or_else(|error| panic!("recover host: {error:?}"));
    let AbortPartitionTransactionOutcome::Failed(failure) = admission
        .observer
        .wait()
        .unwrap_or_else(|error| panic!("observe: {error:?}"))
    else {
        panic!("failure expected");
    };
    assert_eq!(
        (failure.kind(), failure.delivery()),
        (
            AbortPartitionTransactionFailureKind::DriverRejected,
            AbortPartitionTransactionDeliveryStatus::NotSent,
        )
    );

    let _turn = host
        .turn(Moment::from_tick(0))
        .unwrap_or_else(|error| panic!("reclaim terminal: {error:?}"));
    drop(host);
    stop_notifier(&mut notifier);
}

fn stop_notifier(notifier: &mut AdminCompletionNotifier) {
    notifier
        .stop()
        .unwrap_or_else(|error| panic!("stop notifier: {error:?}"))
        .join_off_notifier()
        .unwrap_or_else(|_| panic!("join notifier"));
}