Skip to content

Commit

Permalink
fix(descriptors): valid flow must have a Source and a Sink (#192)
Browse files Browse the repository at this point in the history
Conceptually speaking, a data flow that has no Source will never perform any
transformation on any data as it will never receive any.

Similarly, a data flow that has no Sink will never output anything, making it a
close loop.

This commit ensures that a valid (flattened) data flow has at least one Source
and one Sink.

Signed-off-by: Julien Loudet <[email protected]>
  • Loading branch information
J-Loudet authored Feb 23, 2024
1 parent 08ebd1c commit c1c3a70
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
8 changes: 8 additions & 0 deletions zenoh-flow-descriptors/src/flattened/validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ impl<'a> Validator<'a> {
pub(crate) fn validate(data_flow: &FlattenedDataFlowDescriptor) -> Result<()> {
let mut this = Validator::default();

if data_flow.sources.is_empty() {
bail!("A data flow must specify at least ONE Source.");
}

if data_flow.sinks.is_empty() {
bail!("A data flow must specify at least ONE Sink.");
}

for flat_source in &data_flow.sources {
this.validate_node_id(&flat_source.id)?;

Expand Down
92 changes: 92 additions & 0 deletions zenoh-flow-descriptors/src/flattened/validator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,98 @@ links:
.is_ok());
}

#[test]
fn test_no_source() {
let yaml_no_source = r#"
name: invalid data flow no source
sources:
operators:
- id: operator
library: file:///home/zenoh-flow/liboperator.so
inputs:
- in-op
outputs:
- out-op
sinks:
- id: sink
library: file:///home/zenoh-flow/libsink.so
inputs:
- in-si
links:
- from:
node: operator
output: out-op
to:
node: operator
input: in-op
- from:
node: operator
output: out-op
to:
node: sink-1
input: sink-in
"#;

let res = FlattenedDataFlowDescriptor::try_flatten(
serde_yaml::from_str(yaml_no_source).unwrap(),
Vars::default(),
);

assert!(res.is_err());
assert!(format!("{:?}", res).contains("A data flow must specify at least ONE Source."));
}

#[test]
fn test_no_sink() {
let yaml_no_sink = r#"
name: invalid data flow no sink
sources:
- id: source
library: file:///home/zenoh-flow/libsource.so
outputs:
- out-so
operators:
- id: operator
library: file:///home/zenoh-flow/liboperator.so
inputs:
- in-op
outputs:
- out-op
sinks:
links:
- from:
node: source
output: out-so
to:
node: operator
input: in-op
- from:
node: operator
output: out-op
to:
node: operator
input: in-op
"#;

let res = FlattenedDataFlowDescriptor::try_flatten(
serde_yaml::from_str(yaml_no_sink).unwrap(),
Vars::default(),
);

assert!(res.is_err());
assert!(format!("{:?}", res).contains("A data flow must specify at least ONE Sink."));
}

#[test]
fn test_duplicate_ids() {
let yaml_duplicate_same_section = r#"
Expand Down

0 comments on commit c1c3a70

Please sign in to comment.