Skip to main content

confluent_kafka/admin/
update_features_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::update_features`.
16
17use std::collections::HashMap;
18
19use crate::common::KafkaFuture;
20
21/// The result of the [`update_features_with_options`](crate::admin::Admin::update_features_with_options)
22/// call.
23#[derive(Clone, Debug)]
24#[doc(alias = "org.apache.kafka.clients.admin.UpdateFeaturesResult")]
25pub struct UpdateFeaturesResult {
26    futures: HashMap<String, KafkaFuture<()>>,
27}
28
29impl UpdateFeaturesResult {
30    /// Creates a result from the per-feature futures.
31    #[doc(alias = "org.apache.kafka.clients.admin.UpdateFeaturesResult#UpdateFeaturesResult")]
32    pub(crate) fn new(futures: HashMap<String, KafkaFuture<()>>) -> Self {
33        Self { futures }
34    }
35
36    /// Returns a map from feature name to future, which can be used to check the
37    /// status of individual feature updates.
38    #[doc(alias = "org.apache.kafka.clients.admin.UpdateFeaturesResult#values")]
39    pub fn values(&self) -> &HashMap<String, KafkaFuture<()>> {
40        &self.futures
41    }
42
43    /// Returns a future which succeeds if all the feature updates succeed.
44    #[doc(alias = "org.apache.kafka.clients.admin.UpdateFeaturesResult#all")]
45    pub fn all(&self) -> KafkaFuture<()> {
46        KafkaFuture::all_of(self.futures.values().cloned().collect())
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53    use crate::common::Error;
54    use crate::common::internals::KafkaFutureImpl;
55    use crate::common::protocol::Errors;
56
57    #[tokio::test]
58    async fn all_succeeds_when_each_completes() {
59        let h: KafkaFutureImpl<()> = KafkaFutureImpl::new();
60        let mut map = HashMap::new();
61        map.insert("f1".to_string(), h.future());
62        let result = UpdateFeaturesResult::new(map);
63        h.complete(());
64        result.all().get().await.unwrap();
65    }
66
67    #[tokio::test]
68    async fn all_fails_if_one_fails() {
69        let h: KafkaFutureImpl<()> = KafkaFutureImpl::new();
70        let mut map = HashMap::new();
71        map.insert("f1".to_string(), h.future());
72        let result = UpdateFeaturesResult::new(map);
73        h.complete_with_error(Error::new(Errors::InvalidRequest));
74        assert!(result.all().get().await.is_err());
75    }
76}