MCPcopy Index your code
hub / github.com/grpc/grpc-java / ContextTest

Class ContextTest

api/src/test/java/io/grpc/ContextTest.java:66–1057  ·  view source on GitHub ↗

Tests for Context.

Source from the content-addressed store, hash-verified

64 * Tests for {@link Context}.
65 */
66@RunWith(JUnit4.class)
67@SuppressWarnings("CheckReturnValue") // false-positive in test for current ver errorprone plugin
68public class ContextTest {
69
70 private static final Context.Key<String> PET = Context.key("pet");
71 private static final Context.Key<String> FOOD = Context.keyWithDefault("food", "lasagna");
72 private static final Context.Key<String> COLOR = Context.key("color");
73 private static final Context.Key<Object> FAVORITE = Context.key("favorite");
74 private static final Context.Key<Integer> LUCKY = Context.key("lucky");
75
76 private Context listenerNotifedContext;
77 private CountDownLatch deadlineLatch = new CountDownLatch(1);
78 private final Context.CancellationListener cancellationListener =
79 new Context.CancellationListener() {
80 @Override
81 public void cancelled(Context context) {
82 listenerNotifedContext = context;
83 deadlineLatch.countDown();
84 }
85 };
86
87 private Context observed;
88 private final Runnable runner = new Runnable() {
89 @Override
90 public void run() {
91 observed = Context.current();
92 }
93 };
94 private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
95
96 @Before
97 public void setUp() {
98 Context.ROOT.attach();
99 }
100
101 @After
102 public void tearDown() {
103 scheduler.shutdown();
104 assertEquals(Context.ROOT, Context.current());
105 }
106
107 @Test
108 public void defaultContext() throws Exception {
109 final SettableFuture<Context> contextOfNewThread = SettableFuture.create();
110 Context contextOfThisThread = Context.ROOT.withValue(PET, "dog");
111 Context toRestore = contextOfThisThread.attach();
112 new Thread(new Runnable() {
113 @Override
114 public void run() {
115 contextOfNewThread.set(Context.current());
116 }
117 }).start();
118 assertNotNull(contextOfNewThread.get(5, TimeUnit.SECONDS));
119 assertNotSame(contextOfThisThread, contextOfNewThread.get());
120 assertSame(contextOfThisThread, Context.current());
121 contextOfThisThread.detach(toRestore);
122 }
123

Callers

nothing calls this directly

Calls 2

keyMethod · 0.95
keyWithDefaultMethod · 0.95

Tested by

no test coverage detected