Skip to content

Commit 7de312f

Browse files
add test for anyhow bailout
1 parent 2e9b6ef commit 7de312f

File tree

1 file changed

+57
-0
lines changed
  • libdd-profiling/src/internal/profile

1 file changed

+57
-0
lines changed

libdd-profiling/src/internal/profile/mod.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2605,4 +2605,61 @@ mod api_tests {
26052605
.iter()
26062606
.any(|s| s == "world"));
26072607
}
2608+
2609+
#[test]
2610+
fn reproduce_crash_with_anyhow_bailout() {
2611+
let sample_types = [api::ValueType::new("samples", "count")];
2612+
let mapping = api::Mapping {
2613+
filename: "test.php",
2614+
..Default::default()
2615+
};
2616+
2617+
let mut profile = Profile::new(&sample_types, None);
2618+
2619+
let locations = vec![api::Location {
2620+
mapping,
2621+
function: api::Function {
2622+
name: "test_function",
2623+
system_name: "test_function",
2624+
filename: "test.php",
2625+
},
2626+
line: 0,
2627+
..Default::default()
2628+
}];
2629+
2630+
let sample = api::Sample {
2631+
locations,
2632+
values: &[1],
2633+
labels: vec![api::Label {
2634+
key: "iteration",
2635+
num: 1,
2636+
..Default::default()
2637+
}],
2638+
};
2639+
2640+
profile
2641+
.try_add_sample(sample, None) // No timestamp = aggregated
2642+
.expect("profile to not be full");
2643+
2644+
// We want to trigger an error inside the loop over observations.
2645+
// By clearing the locations, the location IDs referenced by the samples/stacktraces
2646+
// will become invalid (checking against len=0), causing check_location_ids_are_valid to
2647+
// fail.
2648+
profile.locations.clear();
2649+
2650+
let result = profile.serialize_into_compressed_pprof(None, None);
2651+
2652+
match result {
2653+
Ok(_) => panic!(
2654+
"Expected serialization to fail due to invalid location IDs, but it succeeded"
2655+
),
2656+
Err(err) => {
2657+
assert!(
2658+
err.to_string().contains("invalid location id"),
2659+
"Expected error about invalid location id, got: {}",
2660+
err
2661+
);
2662+
}
2663+
}
2664+
}
26082665
}

0 commit comments

Comments
 (0)