Skip to content

Commit 1909cb7

Browse files
committed
Add test for overlapping contexts
1 parent 3e724b4 commit 1909cb7

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
@@ -423,4 +423,46 @@ mod tests {
423423
true
424424
}));
425425
}
426+
427+
#[test]
428+
#[ignore = "overlapping contexts are not supported yet"]
429+
fn overlapping_contexts() {
430+
#[derive(Debug, PartialEq)]
431+
struct ValueA(&'static str);
432+
#[derive(Debug, PartialEq)]
433+
struct ValueB(u64);
434+
435+
let outer_guard = Context::new().with_value(ValueA("a")).attach();
436+
437+
// Only value `a` is set
438+
let current = Context::current();
439+
assert_eq!(current.get(), Some(&ValueA("a")));
440+
assert_eq!(current.get::<ValueB>(), None);
441+
442+
let inner_guard = Context::current_with_value(ValueB(42)).attach();
443+
// Both values are set in inner context
444+
let current = Context::current();
445+
assert_eq!(current.get(), Some(&ValueA("a")));
446+
assert_eq!(current.get(), Some(&ValueB(42)));
447+
448+
assert!(Context::map_current(|cx| {
449+
assert_eq!(cx.get(), Some(&ValueA("a")));
450+
assert_eq!(cx.get(), Some(&ValueB(42)));
451+
true
452+
}));
453+
454+
drop(outer_guard);
455+
456+
// `inner_guard` is still alive so both `ValueA` and `ValueB` should still be accessible
457+
let current = Context::current();
458+
assert_eq!(current.get(), Some(&ValueA("a")));
459+
assert_eq!(current.get(), Some(&ValueB(42)));
460+
461+
drop(inner_guard);
462+
463+
// Both guards are dropped and neither value should be accessible.
464+
let current = Context::current();
465+
assert_eq!(current.get::<ValueA>(), None);
466+
assert_eq!(current.get::<ValueB>(), None);
467+
}
426468
}

0 commit comments

Comments
 (0)