Skip to content

Commit

Permalink
Verify view collectors at startup
Browse files Browse the repository at this point in the history
Closes #70
  • Loading branch information
patriksvensson committed Apr 5, 2020
1 parent b07750e commit d9f20d6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ impl Configuration {
}
result
}

pub fn collector_exist(&self, id: &str) -> bool {
for collector in self.collectors.iter() {
if collector.get_id() == id {
return true;
}
}
false
}
}

#[derive(Serialize, Deserialize, JsonSchema, Debug, Clone)]
Expand Down
33 changes: 33 additions & 0 deletions src/config/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ fn validate_views(configuration: &Configuration) -> DuckResult<()> {
view.id
));
}
for view_collector in view.collectors.iter() {
if !configuration.collector_exist(&view_collector) {
return Err(format_err!(
"The view '{}' depends on collector '{}' which does not exist",
view.id,
view_collector
));
}
}
known_ids.insert(view.id.clone());
}
};
Expand Down Expand Up @@ -167,6 +176,30 @@ mod tests {
config.validate().unwrap();
}

#[test]
#[should_panic(
expected = "The view \\'foo\\' depends on collector \\'bar\\' which does not exist"
)]
fn should_return_error_if_a_view_depends_on_collector_that_does_not_exist() {
let config = Configuration::from_json(
&TestVariableProvider::new(),
r#"
{
"collectors": [ ],
"views": [
{
"id": "foo",
"name": "Foo",
"collectors": [ "bar" ]
}
]
}
"#,
)
.unwrap();
config.validate().unwrap();
}

#[test]
#[should_panic(expected = "Found duplicate id \\'foo\\' in configuration")]
fn should_return_error_if_two_collectors_have_the_same_id() {
Expand Down
7 changes: 7 additions & 0 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ fn run_accumulator(
accumulator::ConfigurationResult::Updated => break,
},
};
if handle.wait(Duration::from_millis(500)).unwrap() {
break;
}
}

while !handle.check().unwrap() {
Expand Down Expand Up @@ -263,6 +266,7 @@ fn run_aggregator(
// Utilities

fn try_get_updated_configuration(
handle: &WaitHandleListener,
receiver: &Receiver<EngineThreadMessage>,
) -> Option<Configuration> {
let mut result: Option<Configuration> = None;
Expand All @@ -272,6 +276,9 @@ fn try_get_updated_configuration(
result = Some(config);
}
}
if handle.wait(Duration::from_millis(500)).unwrap() {
return None;
}
}
result
}
4 changes: 3 additions & 1 deletion src/engine/accumulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ pub enum ConfigurationResult {
}

pub fn check_for_updated_configuration(context: &mut Context) -> DuckResult<ConfigurationResult> {
if let Some(config) = super::try_get_updated_configuration(&context.engine_receiver) {
if let Some(config) =
super::try_get_updated_configuration(&context.listener, &context.engine_receiver)
{
trace!("Applying new configuration...");
match providers::create_collectors(&config) {
Ok(collectors) => {
Expand Down
4 changes: 3 additions & 1 deletion src/engine/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ pub fn aggregate(context: &mut Context) -> AggregateResult {
}

fn check_for_updated_configuration(context: &mut Context) -> DuckResult<()> {
if let Some(config) = super::try_get_updated_configuration(&context.engine_receiver) {
if let Some(config) =
super::try_get_updated_configuration(&context.listener, &context.engine_receiver)
{
trace!("Applying new configuration...");
match crate::providers::create_observers(&config) {
Ok(collectors) => {
Expand Down
12 changes: 11 additions & 1 deletion src/engine/state/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,17 @@ mod tests {
&TestVariableProvider::new(),
r#"
{
"collectors": [ ],
"collectors": [
{ "duck": { "id": "a1", "serverUrl": "http://localhost/a1" } },
{ "duck": { "id": "a2", "serverUrl": "http://localhost/a2" } },
{ "duck": { "id": "b1", "serverUrl": "http://localhost/b1" } },
{ "duck": { "id": "b2", "serverUrl": "http://localhost/b2" } },
{ "duck": { "id": "b3", "serverUrl": "http://localhost/b3" } },
{ "duck": { "id": "c1", "serverUrl": "http://localhost/c1" } },
{ "duck": { "id": "c2", "serverUrl": "http://localhost/c2" } },
{ "duck": { "id": "c3", "serverUrl": "http://localhost/c3" } },
{ "duck": { "id": "c4", "serverUrl": "http://localhost/c4" } }
],
"views": [
{
"id": "foo",
Expand Down

0 comments on commit d9f20d6

Please sign in to comment.