Skip to content

Commit 2e081cf

Browse files
committed
test: Add test for overlapping contexts
1 parent 88cae2c commit 2e081cf

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

opentelemetry/src/context.rs

+42
Original file line numberDiff line numberDiff line change
@@ -460,4 +460,46 @@ mod tests {
460460
true
461461
}));
462462
}
463+
464+
#[test]
465+
#[ignore = "overlapping contexts are not supported yet"]
466+
fn overlapping_contexts() {
467+
#[derive(Debug, PartialEq)]
468+
struct ValueA(&'static str);
469+
#[derive(Debug, PartialEq)]
470+
struct ValueB(u64);
471+
472+
let outer_guard = Context::new().with_value(ValueA("a")).attach();
473+
474+
// Only value `a` is set
475+
let current = Context::current();
476+
assert_eq!(current.get(), Some(&ValueA("a")));
477+
assert_eq!(current.get::<ValueB>(), None);
478+
479+
let inner_guard = Context::current_with_value(ValueB(42)).attach();
480+
// Both values are set in inner context
481+
let current = Context::current();
482+
assert_eq!(current.get(), Some(&ValueA("a")));
483+
assert_eq!(current.get(), Some(&ValueB(42)));
484+
485+
assert!(Context::map_current(|cx| {
486+
assert_eq!(cx.get(), Some(&ValueA("a")));
487+
assert_eq!(cx.get(), Some(&ValueB(42)));
488+
true
489+
}));
490+
491+
drop(outer_guard);
492+
493+
// `inner_guard` is still alive so both `ValueA` and `ValueB` should still be accessible
494+
let current = Context::current();
495+
assert_eq!(current.get(), Some(&ValueA("a")));
496+
assert_eq!(current.get(), Some(&ValueB(42)));
497+
498+
drop(inner_guard);
499+
500+
// Both guards are dropped and neither value should be accessible.
501+
let current = Context::current();
502+
assert_eq!(current.get::<ValueA>(), None);
503+
assert_eq!(current.get::<ValueB>(), None);
504+
}
463505
}

0 commit comments

Comments
 (0)