33import static datadog .context .Context .root ;
44import static datadog .context .ContextTest .STRING_KEY ;
55import static org .junit .jupiter .api .Assertions .assertEquals ;
6+ import static org .junit .jupiter .api .Assertions .assertNotEquals ;
7+ import static org .junit .jupiter .api .Assertions .assertTrue ;
68
79import javax .annotation .Nonnull ;
810import org .junit .jupiter .api .Test ;
911
10- class ContextProviderForkedTest {
12+ class ContextProvidersForkedTest {
1113 @ Test
1214 void testCustomBinder () {
13- // register a NOOP context binder
15+ assertTrue (ContextBinder .allowTesting ());
16+
17+ Context context = root ().with (STRING_KEY , "value" );
18+ Object carrier = new Object ();
19+
20+ // should delegate to the default binder
21+ context .attachTo (carrier );
22+ assertNotEquals (root (), Context .from (carrier ));
23+ assertEquals (context , Context .detachFrom (carrier ));
24+ assertEquals (root (), Context .from (carrier ));
25+
26+ // now register a NOOP context binder
1427 ContextBinder .register (
1528 new ContextBinder () {
1629 @ Override
@@ -29,17 +42,28 @@ public Context detachFrom(@Nonnull Object carrier) {
2942 }
3043 });
3144
32- Context context = root ().with (STRING_KEY , "value" );
33-
3445 // NOOP binder, context will always be root
35- Object carrier = new Object ();
3646 context .attachTo (carrier );
3747 assertEquals (root (), Context .from (carrier ));
48+ assertEquals (root (), Context .detachFrom (carrier ));
3849 }
3950
4051 @ Test
4152 void testCustomManager () {
42- // register a NOOP context manager
53+ assertTrue (ContextManager .allowTesting ());
54+
55+ Context context = root ().with (STRING_KEY , "value" );
56+
57+ // should delegate to the default manager
58+ try (ContextScope ignored = context .attach ()) {
59+ assertNotEquals (root (), Context .current ());
60+ }
61+
62+ Context swapped = context .swap ();
63+ assertNotEquals (root (), Context .current ());
64+ swapped .swap ();
65+
66+ // now register a NOOP context manager
4367 ContextManager .register (
4468 new ContextManager () {
4569 @ Override
@@ -68,11 +92,14 @@ public Context swap(Context context) {
6892 }
6993 });
7094
71- Context context = root ().with (STRING_KEY , "value" );
72-
7395 // NOOP manager, context will always be root
7496 try (ContextScope ignored = context .attach ()) {
7597 assertEquals (root (), Context .current ());
7698 }
99+
100+ // NOOP manager, context will always be root
101+ swapped = context .swap ();
102+ assertEquals (root (), Context .current ());
103+ swapped .swap ();
77104 }
78105}
0 commit comments