Skip to main content

confluent_kafka/admin/
delete_consumer_groups_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 the `Admin::delete_consumer_groups` call for a
16//! `Collection<String>` (group id) input.
17
18use std::collections::HashMap;
19
20use crate::common::KafkaFuture;
21
22/// The result of the `Admin::delete_consumer_groups` call for a
23/// `Collection<String>` (group id) input.
24#[derive(Clone, Debug)]
25#[doc(alias = "org.apache.kafka.clients.admin.DeleteConsumerGroupsResult")]
26pub struct DeleteConsumerGroupsResult {
27    futures: HashMap<String, KafkaFuture<()>>,
28}
29
30impl DeleteConsumerGroupsResult {
31    /// Creates a result from a per-group-id future map.
32    #[doc(alias = "org.apache.kafka.clients.admin.DeleteConsumerGroupsResult#DeleteConsumerGroupsResult")]
33    pub(crate) fn new(futures: HashMap<String, KafkaFuture<()>>) -> Self {
34        Self { futures }
35    }
36
37    /// Returns a map from group id to futures which can be used to check the
38    /// status of individual deletions.
39    #[doc(alias = "org.apache.kafka.clients.admin.DeleteConsumerGroupsResult#deletedGroups")]
40    pub fn deleted_groups(&self) -> HashMap<String, KafkaFuture<()>> {
41        self.futures.clone()
42    }
43
44    /// Returns a future which succeeds only if all the consumer group deletions
45    /// succeed.
46    #[doc(alias = "org.apache.kafka.clients.admin.DeleteConsumerGroupsResult#all")]
47    pub fn all(&self) -> KafkaFuture<()> {
48        KafkaFuture::all_of(self.futures.values().cloned().collect())
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55    use crate::common::Error;
56    use crate::common::internals::KafkaFutureImpl;
57
58    #[tokio::test]
59    async fn all_succeeds_when_every_group_succeeds() {
60        let h1: KafkaFutureImpl<()> = KafkaFutureImpl::new();
61        let h2: KafkaFutureImpl<()> = KafkaFutureImpl::new();
62        h1.complete(());
63        h2.complete(());
64        let result = DeleteConsumerGroupsResult::new(HashMap::from([
65            ("g1".to_string(), h1.future()),
66            ("g2".to_string(), h2.future()),
67        ]));
68        assert_eq!(result.all().get().await.unwrap(), ());
69        assert_eq!(result.deleted_groups()["g1"].get().await.unwrap(), ());
70    }
71
72    #[tokio::test]
73    async fn all_fails_when_any_group_fails() {
74        let h1: KafkaFutureImpl<()> = KafkaFutureImpl::new();
75        let h2: KafkaFutureImpl<()> = KafkaFutureImpl::new();
76        h1.complete(());
77        h2.complete_with_error(Error::group_authorization("g2"));
78        let result = DeleteConsumerGroupsResult::new(HashMap::from([
79            ("g1".to_string(), h1.future()),
80            ("g2".to_string(), h2.future()),
81        ]));
82        assert!(matches!(result.all().get().await.unwrap_err(), Error::GroupAuthorization(_)));
83        assert_eq!(result.deleted_groups()["g1"].get().await.unwrap(), ());
84    }
85}