Skip to content

Commit 47c7da6

Browse files
authored
Enforce downsampling config id unicity (#1785)
* Enforce downsampling id unicity * Add downsampling config validation test * Fix incorrect config field name in test
1 parent d24e33b commit 47c7da6

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

zenoh/src/net/routing/interceptor/downsampling.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! [Click here for Zenoh's documentation](https://docs.rs/zenoh/latest/zenoh)
2020
2121
use std::{
22-
collections::HashMap,
22+
collections::{HashMap, HashSet},
2323
sync::{Arc, Mutex},
2424
};
2525

@@ -38,7 +38,14 @@ pub(crate) fn downsampling_interceptor_factories(
3838
) -> ZResult<Vec<InterceptorFactory>> {
3939
let mut res: Vec<InterceptorFactory> = vec![];
4040

41+
let mut id_set = HashSet::new();
4142
for ds in config {
43+
// check unicity of rule id
44+
if let Some(id) = &ds.id {
45+
if !id_set.insert(id.clone()) {
46+
bail!("Invalid Downsampling config: id '{id}' is repeated");
47+
}
48+
}
4249
res.push(Box::new(DownsamplingInterceptorFactory::new(ds.clone())));
4350
}
4451

zenoh/tests/interceptors.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,41 @@ fn downsampling_config_error_wrong_strategy() {
265265
{
266266
flow: "down",
267267
rules: [
268-
{ keyexpr: "test/downsamples_by_keyexp/r100", freq: 10, },
269-
{ keyexpr: "test/downsamples_by_keyexp/r50", freq: 20, }
268+
{ key_expr: "test/downsamples_by_keyexp/r100", freq: 10, },
269+
{ key_expr: "test/downsamples_by_keyexp/r50", freq: 20, }
270+
],
271+
},
272+
]
273+
"#,
274+
)
275+
.unwrap();
276+
277+
zenoh::open(config).wait().unwrap();
278+
}
279+
280+
#[test]
281+
#[should_panic(expected = "Invalid Downsampling config: id 'REPEATED' is repeated")]
282+
fn downsampling_config_error_repeated_id() {
283+
zenoh::init_log_from_env_or("error");
284+
285+
let mut config = Config::default();
286+
config
287+
.insert_json5(
288+
"downsampling",
289+
r#"
290+
[
291+
{
292+
id: "REPEATED",
293+
flow: "egress",
294+
rules: [
295+
{ key_expr: "test/downsamples_by_keyexp/r100", freq: 10, },
296+
],
297+
},
298+
{
299+
id: "REPEATED",
300+
flow: "ingress",
301+
rules: [
302+
{ key_expr: "test/downsamples_by_keyexp/r50", freq: 20, }
270303
],
271304
},
272305
]

0 commit comments

Comments
 (0)