Skip to content

Commit

Permalink
revive is_well_formed
Browse files Browse the repository at this point in the history
  • Loading branch information
taegyunkim committed Jun 10, 2024
1 parent 2441563 commit 7de94c0
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions profiling/src/internal/profile/fuzz_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ pub struct Sample {
pub labels: Vec<Label>,
}

#[cfg(test)]
impl Sample {
/// Checks if the sample is well formed. Useful in testing.
pub fn is_well_formed(&self) -> bool {
let labels_are_unique = {
let mut uniq = std::collections::HashSet::new();
self.labels.iter().map(|l| &l.key).all(|x| uniq.insert(x))
};
labels_are_unique
}
}

impl<'a> From<&'a Sample> for api::Sample<'a> {
fn from(value: &'a Sample) -> Self {
Self {
Expand Down Expand Up @@ -306,15 +318,13 @@ fn assert_samples_eq(
continue;
} else if *key == *"trace endpoint" {
let actual_str = profile.string_table_fetch(label.str);

let prev_label: &Label = owned_labels
.last()
.expect("Previous label to exist for endpoint label");
let num = prev_label.num as u64;
let expected_str = endpoint_mappings
.get(&num)
.expect("Endpoint mapping to exist");

assert_eq!(actual_str, *expected_str);
continue;
}
Expand Down Expand Up @@ -371,7 +381,7 @@ fn fuzz_add_sample<'a>(
samples_without_timestamps: &mut HashMap<(&'a [Location], &'a [Label]), Vec<i64>>,
) {
let r = profile.add_sample(sample.into(), *timestamp);
if expected_sample_types.len() == sample.values.len() {
if expected_sample_types.len() == sample.values.len() && sample.is_well_formed() {
assert!(r.is_ok());
if timestamp.is_some() {
samples_with_timestamps.push(sample);
Expand Down

0 comments on commit 7de94c0

Please sign in to comment.