### Question:
Contexts { public static <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( Context context, ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { Context previous = context.attach(); try { return new ContextualizedServerCallListener<ReqT>( next.startCall(call, headers), context); } finally { context.detach(previous); } } private  Contexts(); static ServerCall.Listener<ReqT> interceptCall(
        Context context,
        ServerCall<ReqT, RespT> call,
        Metadata headers,
        ServerCallHandler<ReqT, RespT> next); @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1975") static Status statusFromCancelled(Context context);  }### Answer:
@Test public void interceptCall_basic() { Context origContext = Context.current(); final Object message = new Object(); final List<Integer> methodCalls = new ArrayList<>(); final ServerCall.Listener<Object> listener = new ServerCall.Listener<Object>() { @Override public void onMessage(Object messageIn) { assertSame(message, messageIn); assertSame(uniqueContext, Context.current()); methodCalls.add(1); } @Override public void onHalfClose() { assertSame(uniqueContext, Context.current()); methodCalls.add(2); } @Override public void onCancel() { assertSame(uniqueContext, Context.current()); methodCalls.add(3); } @Override public void onComplete() { assertSame(uniqueContext, Context.current()); methodCalls.add(4); } @Override public void onReady() { assertSame(uniqueContext, Context.current()); methodCalls.add(5); } }; ServerCall.Listener<Object> wrapped = interceptCall(uniqueContext, call, headers, new ServerCallHandler<Object, Object>() { @Override public ServerCall.Listener<Object> startCall( ServerCall<Object, Object> call, Metadata headers) { assertSame(ContextsTest.this.call, call); assertSame(ContextsTest.this.headers, headers); assertSame(uniqueContext, Context.current()); return listener; } }); assertSame(origContext, Context.current()); wrapped.onMessage(message); wrapped.onHalfClose(); wrapped.onCancel(); wrapped.onComplete(); wrapped.onReady(); assertEquals(Arrays.asList(1, 2, 3, 4, 5), methodCalls); assertSame(origContext, Context.current()); }
@Test public void interceptCall_restoresIfNextThrows() { Context origContext = Context.current(); try { interceptCall(uniqueContext, call, headers, new ServerCallHandler<Object, Object>() { @Override public ServerCall.Listener<Object> startCall( ServerCall<Object, Object> call, Metadata headers) { throw new RuntimeException(); } }); fail("Expected exception"); } catch (RuntimeException expected) { } assertSame(origContext, Context.current()); }
@Test public void interceptCall_restoresIfListenerThrows() { Context origContext = Context.current(); final ServerCall.Listener<Object> listener = new ServerCall.Listener<Object>() { @Override public void onMessage(Object messageIn) { throw new RuntimeException(); } @Override public void onHalfClose() { throw new RuntimeException(); } @Override public void onCancel() { throw new RuntimeException(); } @Override public void onComplete() { throw new RuntimeException(); } @Override public void onReady() { throw new RuntimeException(); } }; ServerCall.Listener<Object> wrapped = interceptCall(uniqueContext, call, headers, new ServerCallHandler<Object, Object>() { @Override public ServerCall.Listener<Object> startCall( ServerCall<Object, Object> call, Metadata headers) { return listener; } }); try { wrapped.onMessage(new Object()); fail("Exception expected"); } catch (RuntimeException expected) { } try { wrapped.onHalfClose(); fail("Exception expected"); } catch (RuntimeException expected) { } try { wrapped.onCancel(); fail("Exception expected"); } catch (RuntimeException expected) { } try { wrapped.onComplete(); fail("Exception expected"); } catch (RuntimeException expected) { } try { wrapped.onReady(); fail("Exception expected"); } catch (RuntimeException expected) { } assertSame(origContext, Context.current()); }