Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyCpp committed Oct 30, 2023
1 parent 5ce9a8c commit 1639599
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
144 changes: 144 additions & 0 deletions opentelemetry-jaeger/src/exporter/config/collector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,4 +577,148 @@ mod tests {
.build_collector_exporter::<Tokio>();
assert!(exporter.is_ok());
}

#[test]
fn test_resolve_endpoint() {
struct TestCase<'a> {
description: &'a str,
env_var: &'a str,
builder_endpoint: Option<&'a str>,
expected_result: Result<Uri, crate::Error>,
}
let test_cases = vec![
TestCase {
description: "Positive: Endpoint from environment variable exists",
env_var: "http://example.com",
builder_endpoint: None,
expected_result: Ok(Uri::try_from("http://example.com").unwrap()),
},
TestCase {
description: "Positive: Endpoint from builder",
env_var: "",
builder_endpoint: Some("http://example.com"),
expected_result: Ok(Uri::try_from("http://example.com").unwrap()),
},
TestCase {
description: "Negative: Invalid URI from environment variable",
env_var: "invalid random uri",
builder_endpoint: None,
expected_result: Err(crate::Error::ConfigError {
pipeline_name: "collector",
config_name: "collector_endpoint",
reason: "invalid uri from environment variable, invalid uri character"
.to_string(),
}),
},
TestCase {
description: "Negative: Invalid URI from builder",
env_var: "",
builder_endpoint: Some("invalid random uri"),
expected_result: Err(crate::Error::ConfigError {
pipeline_name: "collector",
config_name: "collector_endpoint",
reason: "invalid uri from the builder, invalid uri character".to_string(),
}),
},
TestCase {
description: "Positive: Default endpoint (no environment variable set)",
env_var: "",
builder_endpoint: None,
expected_result: Ok(Uri::try_from(DEFAULT_ENDPOINT).unwrap()),
},
];
for test_case in test_cases {
env::set_var(ENV_ENDPOINT, test_case.env_var);
let builder = CollectorPipeline {
collector_endpoint: test_case.builder_endpoint.map(|s| s.to_string()),
..Default::default()
};
let result = builder.resolve_endpoint();
match test_case.expected_result {
Ok(expected) => {
assert_eq!(result.unwrap(), expected, "{}", test_case.description);
}
Err(expected_err) => {
assert!(
result.is_err(),
"{}, expected error, get {}",
test_case.description,
result.unwrap()

Check warning on line 646 in opentelemetry-jaeger/src/exporter/config/collector/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-jaeger/src/exporter/config/collector/mod.rs#L644-L646

Added lines #L644 - L646 were not covered by tests
);
match (result.unwrap_err(), expected_err) {
(
crate::Error::ConfigError {
pipeline_name: result_pipeline_name,
config_name: result_config_name,
reason: result_reason,
},
crate::Error::ConfigError {
pipeline_name: expected_pipeline_name,
config_name: expected_config_name,
reason: expected_reason,
},
) => {
assert_eq!(
result_pipeline_name, expected_pipeline_name,
"{}",

Check warning on line 663 in opentelemetry-jaeger/src/exporter/config/collector/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-jaeger/src/exporter/config/collector/mod.rs#L663

Added line #L663 was not covered by tests
test_case.description
);
assert_eq!(
result_config_name, expected_config_name,
"{}",

Check warning on line 668 in opentelemetry-jaeger/src/exporter/config/collector/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-jaeger/src/exporter/config/collector/mod.rs#L668

Added line #L668 was not covered by tests
test_case.description
);
assert_eq!(result_reason, expected_reason, "{}", test_case.description);
}
_ => panic!("we don't expect collector to return other error"),

Check warning on line 673 in opentelemetry-jaeger/src/exporter/config/collector/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-jaeger/src/exporter/config/collector/mod.rs#L673

Added line #L673 was not covered by tests
}
}
}
env::remove_var(ENV_ENDPOINT);
}
}

#[test]
fn test_resolve_timeout() {
struct TestCase<'a> {
description: &'a str,
env_var: &'a str,
builder_var: Option<Duration>,
expected_duration: Duration,
}
let test_cases = vec![
TestCase {
description: "Valid environment variable",
env_var: "5000",
builder_var: None,
expected_duration: Duration::from_millis(5000),
},
TestCase {
description: "Invalid environment variable",
env_var: "invalid",
builder_var: None,
expected_duration: DEFAULT_COLLECTOR_TIMEOUT,
},
TestCase {
description: "Missing environment variable",
env_var: "",
builder_var: Some(Duration::from_millis(5000)),
expected_duration: Duration::from_millis(5000),
},
];
for test_case in test_cases {
env::set_var(ENV_TIMEOUT, test_case.env_var);
let mut builder = CollectorPipeline::default();
if let Some(timeout) = test_case.builder_var {
builder = builder.with_timeout(timeout);
}
let result = builder.resolve_timeout();
assert_eq!(
result, test_case.expected_duration,
"{}",

Check warning on line 718 in opentelemetry-jaeger/src/exporter/config/collector/mod.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-jaeger/src/exporter/config/collector/mod.rs#L718

Added line #L718 was not covered by tests
test_case.description
);
env::remove_var(ENV_TIMEOUT);
}
}
}
6 changes: 6 additions & 0 deletions opentelemetry-jaeger/src/exporter/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
feature = "rt-tokio",
feature = "rt-tokio-current-thread"
))]
use crate::exporter::address_family;
#[cfg(any(
feature = "rt-async-std",
feature = "rt-tokio",
feature = "rt-tokio-current-thread"
))]
use async_trait::async_trait;
use opentelemetry_sdk::runtime::RuntimeChannel;
use std::net::ToSocketAddrs;
Expand Down

0 comments on commit 1639599

Please sign in to comment.