Skip to content

Commit

Permalink
Add test for overlapping contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
bantonsson committed Dec 3, 2024
1 parent 5d43929 commit ceeb909
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions opentelemetry/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,46 @@ mod tests {
true
}));
}

#[test]
#[ignore = "overlapping contexts are not supported yet"]
fn overlapping_contexts() {
#[derive(Debug, PartialEq)]
struct ValueA(&'static str);
#[derive(Debug, PartialEq)]
struct ValueB(u64);

let outer_guard = Context::new().with_value(ValueA("a")).attach();

// Only value `a` is set
let current = Context::current();
assert_eq!(current.get(), Some(&ValueA("a")));
assert_eq!(current.get::<ValueB>(), None);

let inner_guard = Context::current_with_value(ValueB(42)).attach();
// Both values are set in inner context
let current = Context::current();
assert_eq!(current.get(), Some(&ValueA("a")));
assert_eq!(current.get(), Some(&ValueB(42)));

assert!(Context::map_current(|cx| {
assert_eq!(cx.get(), Some(&ValueA("a")));
assert_eq!(cx.get(), Some(&ValueB(42)));
true
}));

drop(outer_guard);

// `inner_guard` is still alive so both `ValueA` and `ValueB` should still be accessible
let current = Context::current();
assert_eq!(current.get(), Some(&ValueA("a")));
assert_eq!(current.get(), Some(&ValueB(42)));

drop(inner_guard);

// Both guards are dropped and neither value should be accessible.
let current = Context::current();
assert_eq!(current.get::<ValueA>(), None);
assert_eq!(current.get::<ValueB>(), None);
}
}

0 comments on commit ceeb909

Please sign in to comment.