Skip to content

Commit aa0a62f

Browse files
committed
refactor(aggregator-discovery): 'AggregatorDiscoverer' returns an iterator
1 parent c0b074c commit aa0a62f

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

internal/mithril-aggregator-discovery/src/http_config.rs renamed to internal/mithril-aggregator-discovery/src/http_config_discoverer.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl AggregatorDiscoverer for HttpConfigAggregatorDiscoverer {
7272
async fn get_available_aggregators(
7373
&self,
7474
network: MithrilNetwork,
75-
) -> StdResult<Vec<AggregatorEndpoint>> {
75+
) -> StdResult<Box<dyn Iterator<Item = AggregatorEndpoint>>> {
7676
let client = self.build_client()?;
7777
let networks_configuration_response = client
7878
.get(&self.configuration_file_url)
@@ -92,16 +92,17 @@ impl AggregatorDiscoverer for HttpConfigAggregatorDiscoverer {
9292
&self.configuration_file_url
9393
)
9494
})?;
95-
96-
Ok(networks_configuration_response
95+
let aggregator_endpoints = networks_configuration_response
9796
.networks
9897
.values()
9998
.flat_map(|env| &env.mithril_networks)
10099
.flat_map(|network_map| network_map.iter())
101100
.filter(|(name, _)| *name == network.name())
102101
.flat_map(|(_, network)| &network.aggregators)
103102
.map(|aggregator_msg| AggregatorEndpoint::new(aggregator_msg.url.clone()))
104-
.collect())
103+
.collect::<Vec<_>>();
104+
105+
Ok(Box::new(aggregator_endpoints.into_iter()))
105106
}
106107
}
107108

@@ -173,24 +174,27 @@ mod tests {
173174
AggregatorEndpoint::new("https://release-devnet-aggregator1".into()),
174175
AggregatorEndpoint::new("https://release-devnet-aggregator2".into()),
175176
],
176-
aggregators
177+
aggregators.collect::<Vec<_>>()
177178
);
178179

179-
let aggregators = discoverer
180+
let mut aggregators = discoverer
180181
.get_available_aggregators(MithrilNetwork::new("unknown".into()))
181182
.await
182183
.unwrap();
183184

184-
assert!(aggregators.is_empty());
185+
assert!(aggregators.next().is_none());
185186
}
186187

187188
#[tokio::test]
188189
async fn get_available_aggregators_failure() {
189190
let content = TEST_NETWORKS_CONFIG_JSON_FAILURE;
190191
let (_server, discoverer) = create_server_and_discoverer(content);
191-
discoverer
192+
let result = discoverer
192193
.get_available_aggregators(MithrilNetwork::new("release-devnet".into()))
193-
.await
194-
.expect_err("The retrieval of the aggregators should fail");
194+
.await;
195+
assert!(
196+
result.is_err(),
197+
"The retrieval of the aggregators should fail"
198+
);
195199
}
196200
}

internal/mithril-aggregator-discovery/src/interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ pub trait AggregatorDiscoverer: Sync + Send {
1414
async fn get_available_aggregators(
1515
&self,
1616
network: MithrilNetwork,
17-
) -> StdResult<Vec<AggregatorEndpoint>>;
17+
) -> StdResult<Box<dyn Iterator<Item = AggregatorEndpoint>>>;
1818
}

internal/mithril-aggregator-discovery/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![warn(missing_docs)]
22
//! This crate provides mechanisms to discover aggregators in a Mithril network.
33
4-
mod http_config;
4+
mod http_config_discoverer;
55
mod interface;
66
mod model;
77
pub mod test;

internal/mithril-aggregator-discovery/src/test/double/discoverer.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ impl AggregatorDiscoverer for AggregatorDiscovererFake {
2727
async fn get_available_aggregators(
2828
&self,
2929
_network: MithrilNetwork,
30-
) -> StdResult<Vec<AggregatorEndpoint>> {
30+
) -> StdResult<Box<dyn Iterator<Item = AggregatorEndpoint>>> {
3131
let mut results = self.results.lock().await;
3232

3333
let endpoints = results.pop_front().ok_or_else(|| {
3434
anyhow::anyhow!("No more results available in AggregatorDiscovererFake")
3535
})??;
3636

37-
Ok(endpoints)
37+
Ok(Box::new(endpoints.into_iter()))
3838
}
3939
}
4040

@@ -57,7 +57,7 @@ mod tests {
5757

5858
assert_eq!(
5959
vec![AggregatorEndpoint::new("test-1".to_string())],
60-
messages
60+
messages.collect::<Vec<_>>()
6161
);
6262
}
6363

@@ -68,9 +68,11 @@ mod tests {
6868
Ok(vec![AggregatorEndpoint::new("test-2".to_string())]),
6969
]);
7070

71-
consumer
72-
.get_available_aggregators(MithrilNetwork::dummy())
73-
.await
74-
.expect_err("AggregatorDiscovererFake should return an error");
71+
let result = consumer.get_available_aggregators(MithrilNetwork::dummy()).await;
72+
73+
assert!(
74+
result.is_err(),
75+
"AggregatorDiscovererFake should return an error"
76+
);
7577
}
7678
}

0 commit comments

Comments
 (0)