Skip to main content

confluent_kafka/admin/
abort_transaction_result.rs

1// Copyright 2025 Confluent Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! The result of `Admin::abort_transaction`.
16
17use std::collections::HashMap;
18
19use crate::common::{KafkaFuture, TopicPartition};
20
21/// The result of `Admin::abort_transaction`.
22#[derive(Clone, Debug)]
23#[doc(alias = "org.apache.kafka.clients.admin.AbortTransactionResult")]
24pub struct AbortTransactionResult {
25    futures: HashMap<TopicPartition, KafkaFuture<()>>,
26}
27
28impl AbortTransactionResult {
29    /// Creates a result from the per-partition futures.
30    #[doc(alias = "org.apache.kafka.clients.admin.AbortTransactionResult#AbortTransactionResult")]
31    pub(crate) fn new(futures: HashMap<TopicPartition, KafkaFuture<()>>) -> Self {
32        Self { futures }
33    }
34
35    /// Returns a future that completes when the transaction abort completes (or
36    /// fails on error / timeout).
37    #[doc(alias = "org.apache.kafka.clients.admin.AbortTransactionResult#all")]
38    pub fn all(&self) -> KafkaFuture<()> {
39        KafkaFuture::all_of(self.futures.values().cloned().collect())
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46    use crate::common::Error;
47    use crate::common::internals::KafkaFutureImpl;
48
49    #[tokio::test]
50    async fn all_succeeds_when_partition_completes() {
51        let h: KafkaFutureImpl<()> = KafkaFutureImpl::new();
52        let mut map = HashMap::new();
53        map.insert(TopicPartition::new("t", 0), h.future());
54        let result = AbortTransactionResult::new(map);
55        h.complete(());
56        assert_eq!(result.all().get().await.unwrap(), ());
57    }
58
59    #[tokio::test]
60    async fn all_fails_when_partition_fails() {
61        let h: KafkaFutureImpl<()> = KafkaFutureImpl::new();
62        let mut map = HashMap::new();
63        map.insert(TopicPartition::new("t", 0), h.future());
64        let result = AbortTransactionResult::new(map);
65        h.complete_with_error(Error::local_illegal_state("boom".to_string()));
66        assert!(result.all().get().await.is_err());
67    }
68}