@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()); }
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); } }
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); } } }
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(); }
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); }
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);  }